Find the answer to your Linux question:
Results 1 to 3 of 3
I'm a little confused as to where C stores string constants. One of the books I've been reading says that they are stored in the string table. Take this for ...
  1. #1
    Just Joined! KPolulak's Avatar
    Join Date
    Feb 2009
    Location
    New Jersey, USA
    Posts
    42

    String Table in C

    I'm a little confused as to where C stores string constants.

    One of the books I've been reading says that they are stored in the string table.

    Take this for example:
    Code:
    char *ptr = "Hello World!";
    According to my book, ptr contains the address of the string constant and the string constant is stored in the string table. What is this string table? I can't seem to find any information on the string table. Could somebody please elaborate on this topic for me?

  2. #2
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    Quote Originally Posted by KPolulak View Post
    I'm a little confused as to where C stores string constants.

    One of the books I've been reading says that they are stored in the string table.

    Take this for example:
    Code:
    char *ptr = "Hello World!";
    According to my book, ptr contains the address of the string constant and the string constant is stored in the string table. What is this string table? I can't seem to find any information on the string table. Could somebody please elaborate on this topic for me?
    If you look at the results of gcc filename.c -S it stores the const string in the rodata section which is the read only data section

    Code:
    .globl ch
    	.section	.rodata
    .LC0:
    	.string	"Hello, World!\n"
    	.data
    	.align 8
    	.type	ch, @object
    	.size	ch, 8
    ch:
    	.quad	.LC0
    Make mine Arch Linux

  3. #3
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    This is not really a feature of C, but instead a feature of assembly language.

    In this case, "Hello World!" is an example of static data. The string will always be the same, and more importantly, we know exactly how long it is. Therefore, it may be stored in memory directly, and does not need to be computed at runtime. Compare this to:
    Code:
    char *str = malloc(13 * sizeof(char));
    str[0] = 'H';
    str[1] = 'e';
    str[2] = 'l';
    str[3] = 'l';
    str[4] = 'o';
    str[5] = ' ';
    str[6] = 'W';
    str[7] = 'o';
    str[8] = 'r';
    str[9] = 'l';
    str[10] = 'd';
    str[11] = '!';
    str[12] = '\0';
    This produces the same string, but this string is allocated dynamically. Therefore, this string will appear on either the stack or the heap, and requires more complicated logic to avoid fragmentation.

    I suspect that most C books will not discuss this, because again, this is a feature of the actual processor and the executable format, not a feature of the C language. For more information, you could read about x86 assembly or the ELF executable file format.
    DISTRO=Arch
    Registered Linux User #388732

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...