Read-only string

R

Rk

Andrew said:
Well, being as the compiler is allowed to put "test string" wherever it
wants, it could elect to put it within the executable code.

Directly not related to the main topic, but while we are at it I feel
like clarifying this question of mine. I and my friend the other day
were discussing, where "test string" would be stored. In which part of
the memory.
After going through few tutorials on internet, I concluded, if it is
local declaration, the "test string" should resides in STACK. If it is
global or static declaration then, it should reside in "HEAP". In case
we are not using "heap" (in embedded systems we do not), then I could
just say that it stays in RAM.
Now you said it could be in executable code, which was what my friend
guessed.
Could you please clarify.
Thanks.
======
Rk
I am logged in therefore I am.
 
C

Chris Dollin

Rk said:
Directly not related to the main topic, but while we are at it I feel
like clarifying this question of mine. I and my friend the other day
were discussing, where "test string" would be stored. In which part of
the memory.
After going through few tutorials on internet, I concluded, if it is
local declaration, the "test string" should resides in STACK. If it is
global or static declaration then, it should reside in "HEAP". In case
we are not using "heap" (in embedded systems we do not), then I could
just say that it stays in RAM.

The compiler can put the string "test string" wherever it likes. All
that is required is that there is a single location, it has the
required contents, and so long as the program exists (or at least
has a reference to that location or a character within it) the
location exists too - "static storage".

Note that if the declaration

char *str = "test string";

is function-local, it's /str/ that's function-local, not "test string".
In particular it would generally be unsound to store the characters
"test string" [plus terminating 0] on /that function call's/ "stack
frame" (if the implementation has one).
Now you said it could be in executable code, which was what my friend
guessed.

Yes. It /could/ be. Or it /could/ be in some chunk consisting of all
the initialised data from the program. Or it /could/ be in a chunk
consisting of all the initialised and non-writeable data from the
program. Such chunks might be allocated from the same part of the
address space as the "heap" (if there is one) or the "stack" (ditto).
 
P

pete

Chris said:
The compiler can put the string "test string" wherever it likes. All
that is required is that there is a single location,

That depends on what you mean by "single location".
("test string" == "test string") is not guaranteed to be true.

it has the
required contents, and so long as the program exists (or at least
has a reference to that location or a character within it) the
location exists too - "static storage".

Note that if the declaration

char *str = "test string";

is function-local, it's /str/ that's function-local,
not "test string".

Yes.
The return values of func1 and func2 are defined.
The return value of func3 is not defined.


char *func1(void)
{
char *str = "test string";
return str;
}

char *func2(void)
{
return "test string";
}

char *func3(void)
{
char str[] = "test string";
return str;
}
 
C

Chris Dollin

pete said:
Chris Dollin wrote:

That depends on what you mean by "single location".
("test string" == "test string") is not guaranteed to be true.

Indeed. But in

char *spoo(void) { return "fresh spoo"; }

each call of `spoo` returns the same value: that particular string
`fresh spoo` has only one location during the lifetime of the
program, which is what I was trying to say.

I agree that I hadn't said clearly what I meant.
 
K

Kenneth Brody

Richard said:
Kenneth Brody said: [...]
I assume that it could also merge "Hello" and "Hello World", _if_ the
"Hello" were defined without the trailing nul?

Now, how does one define a pointer to 5 chars?

cdecl says:

cdecl> explain char (*foo)[5];
declare foo as pointer to array 5 of char
Yes.


but gcc complaint on this:

char (*foo)[5] = "Hello";

Yes, because char (*)[5] is not the same as char * (which is what you get
when you evaluate a char[6] such as "Hello").

This works, though:

char bar[5] = "Hello"; /* caveat - bar does not contain a string */
char (*foo)[5] = &bar;

True, but that won't allow the compiler to merge "Hello" with "Hello
World" (as it can with char *foo="World",*bar="Hello World"), which
is what I was wondering about.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,184
Messages
2,570,973
Members
47,529
Latest member
JaclynShum

Latest Threads

Top