where the "String literals" store in ?

This is a discussion on where the "String literals" store in ? within the C Programming forums, part of the General Programming Boards category; EX: char string[50] = "liyanhong"; I konw 'string' will store in where called the Stack, but the String literals "liyanhong" ...

  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
    Registered User
    Join Date
    Mar 2009
    Posts
    399

  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,832
    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,047
    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
    21,893
    nonoob has a good point though: the answer to your question is implementation defined.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,832
    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
    21,893
    If I remember correctly, the standard does say that attempting to modify a string literal results in undefined, not just implementation defined, behaviour.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 02:55 AM
  2. Free Store of memory
    By George2 in forum C++ Programming
    Replies: 6
    Last Post: 11-12-2007, 01: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21