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 ...
- 02-08-2010 #1
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:
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?Code:char *ptr = "Hello World!";
- 02-08-2010 #2Make mine Arch Linux
- 02-10-2010 #3
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:
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.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';
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


Reply With Quote
