EX:
char string[50] = "liyanhong";
I konw 'string' will store in where called the Stack, but the String literals "liyanhong" where does it store ?
My English is bad, please don't mind
EX:
char string[50] = "liyanhong";
I konw 'string' will store in where called the Stack, but the String literals "liyanhong" where does it store ?
My English is bad, please don't mind
Thanks
String literals could be stored in the data segment or code segment. It's up to the compiler. As code-segment is at least read-access, I don't see a problem if during execution data is copied from there.
The answer depends on where the array string[] is defined.
If it's defined inside a function, then "liyanhong" will be stored in the stack segment.
If it's defined outside of any function, then "liyanhong" will be stored in the data segment.
However, a string literal defined as
will be stored in the text segment, tho' some compilers have switches to modify that behavior.Code:char *s = "liyanhong";
That's also the reason why a string literal can't be altered since the text segment is readonly.
nonoob has a good point though: the answer to your question is implementation defined.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Thank you, I will always come to study here later.
In GCC:
GCC used to accept the -fwritable-strings option to specify that all strings should be writable, as of 4.0, this is no longer maintained.Code:char writablestring[]="string"; /* This goes in the data seg and is writable */ char *nonwritablestring="string"; /* This goes before the function in the code seg and is not writable, writing to it will cause a seg fault*/
Actually it's probably not writable. There's a reason people in this thread keep saying it's implementation specific.
Quzah.
Hope is the first step on the road to disappointment.
Thanks, User Name:. I didn't have the means to test it out but I did remember it right - that CS or DS is picked in some circumstances.
Interesting that it depends on whether it is moved to an array or simply referred to by pointer. I guess that in the array move case, keeping the literal in data segment means the CS register need not be set up. But that would be a minor inconvenience / time-penalty in any case. So, not sure why they chose to pick one over the other. Maybe it has to do with automatic exception trapping in case the programmer tried a "write".
It shouldn't be writable in either case though. A literal is not meant for that.
Last edited by nonoob; 06-14-2010 at 06:46 PM.
quzah: In any cases you, I, or OP are likely to encounter, without directly searching for exceptions, what I said will hold true. Of coarse there are going to be exceptions, for example, one could mark the segment as writable and executable(I don't know how to specify to the linker that this is desired, but it is possible. Fasm has a directive to make your '.text' or any other segment executable.)
nonoob: No compiler I've ever heard of, including GCC, supports far pointers(pointers that use segments). All pointers used by GCC use the DS register(the one used when none is specified). The term seg fault is an outdated term from the stone age when segmentation was used(Windows and Linux use a flat memory model, with no segmentation, just one big segment). A seg fault is actually when a process accesses a page incorrectly. Pages can be marked writable, readable, and/or executable. Paging is the main form of memory protection in ALL modern operating systems. You can allocate a page in Windows with VirtualAlloc. Pages allocated can be even be marked in a way that would allow you to write to all strings and code! SMC is still possible.
Touche. Good point, I didn't think to consult the standards.Originally Posted by quzah
If I remember correctly, the standard does say that attempting to modify a string literal results in undefined, not just implementation defined, behaviour.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)