Thread: where the "String literals" store in ?

  1. #1
    Registered User
    Join Date
    Nov 2008
    Location
    China
    Posts
    17

    Smile where the "String literals" store in ?

    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

  2. #2

  3. #3
    Registered User
    Join Date
    Nov 2008
    Location
    China
    Posts
    17
    Thanks

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    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.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by liyanhong View Post
    EX:
    char string[50] = "liyanhong";

    I konw 'string' will store in where called the Stack, but the String literals "liyanhong" where does it store ?
    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
    Code:
    char *s = "liyanhong";
    will be stored in the text segment, tho' some compilers have switches to modify that behavior.
    That's also the reason why a string literal can't be altered since the text segment is readonly.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    nonoob has a good point though: the answer to your question is implementation defined.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Nov 2008
    Location
    China
    Posts
    17
    Thank you, I will always come to study here later.

  8. #8
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    In GCC:
    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*/
    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.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.

  10. #10
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    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.

  11. #11
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    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.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by User Name: View Post
    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.)
    Turbo C has writable literals. "But that's old!" -- Tell that to the people every week who show up here using it.

    My point is, the standard doesn't say that string literals cannot be writable.


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    Quote Originally Posted by quzah
    My point is, the standard doesn't say that string literals cannot be writable.
    Touche. Good point, I didn't think to consult the standards.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If I remember correctly, the standard does say that attempting to modify a string literal results in undefined, not just implementation defined, behaviour.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 03:55 AM
  2. Free Store of memory
    By George2 in forum C++ Programming
    Replies: 6
    Last Post: 11-12-2007, 02:27 PM
  3. store data from ifstream and store in link list
    By peter_hii in forum C++ Programming
    Replies: 2
    Last Post: 10-26-2006, 08:50 AM
  4. Do you store store one off data arrays in a class?
    By blood.angel in forum C++ Programming
    Replies: 5
    Last Post: 06-24-2002, 12:05 PM
  5. how to store a node on hard disk
    By ALLRIGHT in forum C Programming
    Replies: 3
    Last Post: 05-13-2002, 10:11 AM