What is String Table in C?

Solution for What is String Table in C?
is Given Below:

char *p = "one two three";

Apparently, the string constant in this code is stored in the string table. What is “string table”? Is it in the heap or on the stack?

Information Background: I found this info in
Teach Yourself C, by Hilbert Schildt, at chapter 6 (Using Pointers).

As you know, C allows string constants enclosed between double quotes to be used in a program. When compiler encounters such a string, it stores it in the program’s string table and generates a pointer to the string. For this reason, the following program is correct and prints – one two three.

int main(void){
  char*p = "one two three";
  return 0;

What the cited paragraph means, is a separated section of your program. These string constants are neither in the stack nor on the heap.

Think about these kinds of sections a program may use to store stuff (common names of sections defined in the executable’s file format in parentheses):

  • The machine code to execute (.text);
  • Static read-only values, for example static const variables (.rodata);
  • Static read-write variables with non-zero initial values (.data);
  • Static read-write variables with zero’ed initial values (.bss);
  • The stack to hold return addresses, function parameters, local variables, and so on; sometimes these are separated;
  • The heap, if there is one at all.

String constants commonly fall in the category “static read-only values”. Some compiler systems separate them from non-string values.

Any compiler system may have these sections or not, but they are quite common.

Anyway, abstract from this implementation detail. It will only help you if you need to go that deep, and you need to know how your specific compiler system works.

On the abstract level, we can differentiate two cases:

const char* p = "one two three";

The string constant is stored somewhere, and its address is assigned to p. If you overwrite this variable with another value, you will lose the address.

An extra space is needed for the variable p. Where this is, depends on the variable.

const char a[] = "one two three";

The string constant is stored somewhere, too. The array is allocated at this place. As long as a is visible, you can access its characters.

Final note: Characters of a string constant are read-only. Always declare your respective variable with const char.

There are two possible levels of answer here:

  1. what is mandated by the language
  2. how it is commonly implemented

@thebusybee’s answer focuses on the second point, I will focus on the first.

Stack and heap do not exist on a language point of view: they are only implementation details, and some old implementations used more than one heap (local heap # global heap). The language only defines duration:

  • static duration: the lifetime is the lifetime of the program
  • dynamic duration: the lifetime starts after allocation with malloc and ends with free
  • automatic: the lifetime starts at the beginning of the bloc where the variable is declared and ends at the end of the bloc.

A string litteral (enclosed in double quotes) is a constant. That means that trying to modify it invokes Undefined Behaviour and that the constant lifetime is the duration of the program. If you use more than once the same string litteral, it is unspecified whether they will have same address or not:

const char *p = "abc";
const char *q = "abc"; // p == q may be true or false

Beware, a string litteral can also be used to initialize an array:

char s[] = "abc";    // array initialization

Here s is a true array (and can be modified because it is not declared to be const), and depending how it is declared it can have automatic or static duration. Whether the string litteral has an existance per se (independant of the array) is again an implementation detail.