views:

427

answers:

7
void f()
{
    char *c = "Hello World!"
}

Where is the string stored? What's the property of it? I just know it is a constant, what else? Can I return it from inside of the function body?

+12  A: 

it is packaged with your binary -- by packaged I mean hard-wired, so yes you can return it and use it elsewhere -- you won't be able to alter it though, and I strongly suggest you declare it as:

const char * x = "hello world";
Hassan Syed
+1. I doubt it will even compile without the const qualifier.
Billy ONeal
@BillyONeal : It does.
missingfaktor
I vaguely recall oldschool C compiler profiles refusing to compile without `const`.
Hassan Syed
@Rahul G: I learned something today :) (upvote on comment) That's really stupid though...
Billy ONeal
@BillyONeal: Like I mentioned below, since the C++ spec AFAIK doesn't state that these have to be read-only, there's no reason it has to be language-enforced as const.
David Pfeffer
@Rahul, it does *now*. But C++0x is going to forbid assigning to `char*`. It removed this deprecated conversion from the const literal array to `char*`
Johannes Schaub - litb
+4  A: 

The string is stored in the data area of the program. This is completely compiler, executable format, and platform dependent. For example, an ELF binary places this in a different location than a Windows executable, and if you were compiling for an embedded platform this data might be stored in ROM instead of RAM.

Here's an illustration of the layout of the ELF format:

ELF Layout

Your string data would most likely be found in the .data or .text sections, depending on compiler.

You can certainly return it from inside the function body. Just check with your implementation to verify that it is random access, as many implementations won't let you overwrite it.

David Pfeffer
Executable format does not define how string literals are stored.
Billy ONeal
It absolutely does. Several executable standards specify where to put static data in the file.
David Pfeffer
Hmm.. I'm not aware of any. ELF, PE, and NE certainly don't. PE and ELF formats provide a facility of a string table for localization purposes, but string constants aren't (generally) put there because that's not their purpose.
Billy ONeal
While you are correct in your first paragraph (sort of...) about what you are saying, I would not take such a strong stance on things -- "checking with implementation to see weather you can change the const" is very bad advice to give someone. You are suggesting that there might be a C implementation where doing this is OK ---- there isn't and any way of doing it will be a HACK. (I won't -1 though).
Hassan Syed
@David: Yes, ELF has .text and .data sections -- but neither section says anything about storing constant string data. A compiler is free to do so, but it's not a standardized part of the format.
Billy ONeal
I'm not 100% on this, but I don't think the C++ mention anywhere that string literals are by definition constant. I think that is just the most common implementation. I think that a compiler that puts string literals into RAM would be just as valid. If I'm wrong though, someone show me where the specs says it -- I can't find it.
David Pfeffer
@BillyONeal That's why I said that it is also compiler and platform dependent.
David Pfeffer
+1  A: 

It's implementation defined. Most of the time that would be stored in a string table with all the other strings in your program. Generally you can treat it like a global static const variable except it's not accessible outside your function.

Billy ONeal
+3  A: 

It has static storage duration, so it exists throughout the life of the program. Exactly where the compiler/linker put initialized data varies. Returning a pointer to it from a function is fine, but be sure you return a char const * -- writing to the string causes undefined behavior.

Jerry Coffin
+!. Access violation, anyone? :) EDIT: That's +1! Damn shift key...
Billy ONeal
+8  A: 

It is generally stored in the read only section of memory and has static storage allocation.

Performing operations like c[0] = 'k' etc invokes Undefined Behavior.

Can I return it from inside of the function body?

Yes!

Prasoon Saurav
+4  A: 

§2.14.15 String Literals, Section 7

A narrow string literal has type “array of n const char”, where n is the size of the string as defined below, and has static storage duration.

FredOverflow
A: 

It's been a while since I played with C++, but I remember I (self taught) had a load of problems with strings (well, ok, character arrays...).

If you're going to be modifying their value at all, be sure to use the new and delete keywords... Something along these lines...

char *strText = new char[10];
/* Do something
...
...
...
*/
delete [] strText;

Martin

Martin Milan
You must match `new[]` with `delete[]`, not `delete`. Or just use `std::string`.
FredOverflow
My bad then - couldn't remember having to do that though... Then again, C++ was a long time ago for me - Borland Turbo C++ for MSDOS...
Martin Milan
Some compilers get it right without the square brackets added to `delete`, but I would not rely on it.
Ton van den Heuvel
-1 for not fixing your answer after 20 hours.
Johannes Schaub - litb
If everyone immediately fixes their answer in the light of comments on SO, the trouble is that the comments will remain, and will cast doubt of the credibility of what would then be the correct answer. Better in my view to just accept that you're wrong (see above) and move on...
Martin Milan
Or if people would fix the answer, it wouldn't be wrong, and then they could add a comment saying "hey thanks for pointing out the mistake, it has been corrected..."
gnarf
Gnarf - I have already pointed out that my original answer was wrong - but you have a point. So, anyone reading it now, the answer has been amended accordingly...
Martin Milan