Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Is it possible sentence?

Reply
Thread Tools

Is it possible sentence?

 
 
Alex
Guest
Posts: n/a
 
      06-22-2004
Hi,

I have a question regarding to string variable.

Please look at below example.
------------------------------
#define STR "ABC"

char *testStr
testStr = STR;
------------------------------

I think above example have no problem.
But I don't know where "ABC" is exist.
Does C compiler make a memory for STR?

-Alex-
 
Reply With Quote
 
 
 
 
Chris Dollin
Guest
Posts: n/a
 
      06-22-2004
Alex wrote:

> I have a question regarding to string variable.
>
> Please look at below example.
> ------------------------------
> #define STR "ABC"
>
> char *testStr


(missing

> testStr = STR;


(illegal at top level; assume we rewrite to

chat *testStr = STR;
)

> ------------------------------
>
> I think above example have no problem.
> But I don't know where "ABC" is exist.


Somewhere. Somewhere where it will be valid for the
entire program run. You don't need to know where.

> Does C compiler make a memory for STR?


Yes, but irrelevant; STR is a preprocessor symbol and is not visible
at run-time (and need not and usually is not in compiled object code).

--
Chris "electric hedgehog" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambl...me_to_clc.html
 
Reply With Quote
 
 
 
 
Vijay Kumar R Zanvar
Guest
Posts: n/a
 
      06-22-2004

"Alex" <(E-Mail Removed)> wrote in message news:(E-Mail Removed) om...
> Hi,
>
> I have a question regarding to string variable.
>
> Please look at below example.
> ------------------------------
> #define STR "ABC"
>
> char *testStr
> testStr = STR;


This is perfectly correct if it appears inside a function. A statement
in C must always be within a compound block.

> ------------------------------
>
> I think above example have no problem.
> But I don't know where "ABC" is exist.
> Does C compiler make a memory for STR?
>


Your question, in general, is about storage of string literals. The answer
varies under the following situations:

* The Standard recommends the string literal to be a read-only; thus,
allows sharing copies of string with identical text. In this
case, the implementation may store the sting in the code section,
or any other read-only section. This method helps perform some
optimizations.

* Many implementation provide extensions which are, generally, non
portable. One such extension is writable string literals. The
string is, then, stored in the program's static region. Thus, there
would be different copies of string literal of identical text.

Many compilers provide option to control the behaviour of character string
literals. For example, gcc provide a compiler switch, -fwritable-strings, to
store strings in writable data section. By default, GCC merges duplicate
strings, whereas, Borland's Turbo C/C++ doesn't. It provides the following
switches:

-d merge duplicate string on
-d- merge duplicate string off

Many compilers provide pragmas to control, but these are non portable.
The Standard(C99) specifies only few pragmas that are portable, and is out of
scope here.

> -Alex-


--
Vijay Kumar R Zanvar
My Home Page - http://www.geocities.com/vijoeyz/


 
Reply With Quote
 
Vijay Kumar R Zanvar
Guest
Posts: n/a
 
      06-22-2004

"Vijay Kumar R Zanvar" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
>
> "Alex" <(E-Mail Removed)> wrote in message news:(E-Mail Removed) om...
> > Hi,
> >
> > I have a question regarding to string variable.
> >
> > Please look at below example.
> > ------------------------------
> > #define STR "ABC"
> >
> > char *testStr


Ah..missing semicolon.

> > testStr = STR;

>



 
Reply With Quote
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      06-22-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (Alex) wrote:
>Hi,
>
>I have a question regarding to string variable.
>
>Please look at below example.
>------------------------------
>#define STR "ABC"
>
>char *testStr
>testStr = STR;
>------------------------------
>
>I think above example have no problem.
>But I don't know where "ABC" is exist.
>Does C compiler make a memory for STR?


No. First, every occurrence of STR is literally replaced with "ABC"
by the C preprocessor. Later in translation "ABC", like every source
code string literal, is translated into an anonymous array object
(in C the term 'object' means 'region of data storage which can hold
values').

Regards
--
Irrwahn Grausewitz ((E-Mail Removed))
welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      06-22-2004
In <(E-Mail Removed) > (E-Mail Removed) (Alex) writes:

>I have a question regarding to string variable.
>
>Please look at below example.
>------------------------------
>#define STR "ABC"
>
>char *testStr;
>testStr = STR;
>------------------------------
>
>I think above example have no problem.


Indeed, it is correct (after fixing an obvious typo).

>But I don't know where "ABC" is exist.


It is typically stored in either the data segment or the text segment
of the program. If you can change it at runtime without crashing the
program, then it's probably in the data segment. If the program crashes,
it's in the text segment. Let's see:

fangorn:~/tmp 192> cat test.c
#define STR "ABC"

int main()
{
char *testStr;
testStr = STR;
*testStr = 0;
return *testStr;
}
fangorn:~/tmp 193> gcc test.c
fangorn:~/tmp 194> ./a.out
Segmentation fault

So, it was in the text segment on my system/compiler combo.
Some compilers allow the user to control this aspect:

fangorn:~/tmp 195> gcc -fwritable-strings test.c
fangorn:~/tmp 196> ./a.out
fangorn:~/tmp 197> echo $status
0

>Does C compiler make a memory for STR?


The STR identifier exists only during the preprocessing phase. By the
time the proper compilation starts, it has already been replaced by the
"ABC" string literal, so the compiler doesn't even get to "see" it.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: (E-Mail Removed)
 
Reply With Quote
 
Ralmin
Guest
Posts: n/a
 
      06-23-2004
"Dan Pop" <(E-Mail Removed)> wrote:
> (E-Mail Removed) (Alex) writes:
> > #define STR "ABC"
> >
> > char *testStr;


Note: the semicolon in the above line was added by Dan, and was not in
Alex's original post.

Dan, lest anyone accuse you of turning into another ERT, please strictly
uphold the rules of quoting. The most one should do to quoted text is to
snip out parts or to reflow the text for a consistent line length. It is not
acceptable to silently make additions, changes or improvements to someone's
code. At the least, the quote symbol on that line (>) should be removed to
indicate changes were made.

--
Simon.


 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      06-23-2004
On Tue, 22 Jun 2004 17:30:17 +0530, "Vijay Kumar R Zanvar"
<(E-Mail Removed)> wrote in comp.lang.c:

>
> "Alex" <(E-Mail Removed)> wrote in message news:(E-Mail Removed) om...
> > Hi,
> >
> > I have a question regarding to string variable.
> >
> > Please look at below example.
> > ------------------------------
> > #define STR "ABC"
> >
> > char *testStr
> > testStr = STR;

>
> This is perfectly correct if it appears inside a function. A statement
> in C must always be within a compound block.
>
> > ------------------------------
> >
> > I think above example have no problem.
> > But I don't know where "ABC" is exist.
> > Does C compiler make a memory for STR?
> >

>
> Your question, in general, is about storage of string literals. The answer
> varies under the following situations:
>
> * The Standard recommends the string literal to be a read-only; thus,


The Standard makes no recommendations at all as to whether string
literals should be read-only or not. C++ defines string literals as
having type array of const char, but C does not. A C string literal
has the type of array of char, no const.

Modifying a string literal in C is undefined behavior because the
standard specifically states that it is, not because the characters
may or may not be read-only.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      06-23-2004
On 22 Jun 2004 13:25:48 GMT, (E-Mail Removed) (Dan Pop) wrote in
comp.lang.c:

> In <(E-Mail Removed) > (E-Mail Removed) (Alex) writes:
>
> >I have a question regarding to string variable.
> >
> >Please look at below example.
> >------------------------------
> >#define STR "ABC"
> >
> >char *testStr;
> >testStr = STR;
> >------------------------------
> >
> >I think above example have no problem.

>
> Indeed, it is correct (after fixing an obvious typo).
>
> >But I don't know where "ABC" is exist.

>
> It is typically stored in either the data segment or the text segment


Who says C programs have data segments and text segments? Chapter and
verse.

> of the program. If you can change it at runtime without crashing the
> program, then it's probably in the data segment. If the program crashes,
> it's in the text segment. Let's see:


Let's not. A strictly conforming program can't tell what happens when
a string literal is changed, since it ceases to be strictly conforming
when it tries to perform the change.

In most of the environments where the code I get paid to write
executes, the undefined behavior of attempting to modify a string
literal is ... nothing at all. The EPROM or Flash device in which the
string literal is stored is totally unaffected by anything the
processor can do to it in normal execution.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      06-23-2004
In <(E-Mail Removed) m> "Ralmin" <(E-Mail Removed)> writes:

>"Dan Pop" <(E-Mail Removed)> wrote:
>> (E-Mail Removed) (Alex) writes:
>> > #define STR "ABC"
>> >
>> > char *testStr;

>
>Note: the semicolon in the above line was added by Dan, and was not in
>Alex's original post.


Why would anyone bother to note such an irrelevant detail? Does it make
any difference to the OP question or to my reply?

>Dan, lest anyone accuse you of turning into another ERT, please strictly
>uphold the rules of quoting. The most one should do to quoted text is to
>snip out parts or to reflow the text for a consistent line length. It is not
>acceptable to silently make additions, changes or improvements to someone's
>code. At the least, the quote symbol on that line (>) should be removed to
>indicate changes were made.


Did you read the rest of my post with your brain engaged? Here's the
relevant part:

>I have a question regarding to string variable.
>
>Please look at below example.
>------------------------------
>#define STR "ABC"
>
>char *testStr;
>testStr = STR;
>------------------------------
>
>I think above example have no problem.


Indeed, it is correct (after fixing an obvious typo).
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
In your humble opinion, what was the purpose of the parenthetical
remark underlined above?

The intent of the OP was perfectly clear, despite the missing semicolon
in the original code. If I had any good reason to suspect that the OP
omitted it *by ignorance*, I would have pointed out the syntax error.
In the absence of any such reason, it was more constructive to fix the
code AND mention that I've fixed it, rather than waste time uselessly
pointing out the missing semicolon. I keep forgetting semicolons myself,
on a regular basis, because my brain is focused on more important things
when typing C code.

It is not the first time I'm correcting trivial typos in posted code
and expect a much harsher reply the next time you choose to make a
fuss out of it.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: (E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
efficient data loading with Python, is that possible possible? igor.tatarinov@gmail.com Python 10 12-14-2007 04:44 PM
is it possible to possible to create an iterator from a callback interace? aninnymouse@gmail.com C Programming 4 02-21-2006 02:10 PM
XML + XSD: Is it possible to get all possible Values for an Element? Markus Java 1 11-22-2005 02:51 PM
Possible to connect 2 computers via USB? Michael Giroux Wireless Networking 2 09-02-2004 03:27 AM
Possible domain related wireless problem =?Utf-8?B?QWxpIEQ=?= Wireless Networking 0 08-10-2004 01:59 AM



Advertisments