7

This is a question based on answers from question:

const char myVar* vs. const char myVar[]

const char* x = "Hello World!";
const char  x[] = "Hello World!";

I understand the difference now, but my new questions are:

(1) What happens to the "Hello World" string in the first line if I reassign x? Nothing will be pointing to it by that point - would it be destroyed when the scope ended?

(2) Aside from the const-ness, how are the values in the two examples differently stored in memory by the compiler?

3 Answers 3

10

Placing "Hello World!" in your code causes the compiler to include that string in the compiled executable. When the program is executed that string is created in memory before the call to main and, I believe, even before the assembly call to __start (which is when static initializers begin running). The contents of char * x are not allocated using new or malloc, or in the stack frame of main, and therefore cannot be unallocated.

However, a char x[20] = "Hello World" declared within a function or method is allocated on the stack, and while in scope, there will actually be two copies of that "Hello World" in memory - one pre-loaded with the executable, one in the stack-allocated buffer.

1
  • const char x[] is supposed to copy the data to the stack however. Aug 16, 2011 at 18:24
4

The compiler stores the first in a section of memory called RODATA (read-only data). As long as the program is still running, the memory still holds its initial value.

The second is stored just like any other array--on the stack. Just like any other local variable, it could be overwritten once its scope ends.

1
  • As long as the program is still running, the memory still holds its initial value. That is to say, the scope of the string is the program scope, not that of the function. Aug 16, 2011 at 18:23
-1
  1. Formally in both cases the "Hello World!" string is allocated in static memory as a contiguous sequence of char, so there is no dynamically allocated memory to reclaim nor any class instance to destroy;
  2. Both xs will be allocated either in static memory or on the stack, depending on where they are defined. However the pointer will be initialized so as to point to the corresponding "Hello World!" string, while the array will be initialized by copying the string literal directly into it.

In theory the compiler is free to reclaim the memory of both string literals when there are no ways to access them; in practice the first one is unlikely to be reclaimed, as usually static memory remains allocated to the program until its termination; on the other hand if the array is allocated on the stack the second one might even not be allocated at all, as the compiler may use other means to ensure the array is properly initialized, and the array memory will be reclaimed when it goes out of scope.

5
  • In addition to this, doing sizeof(x) with the pointer will result in 4 (or 8 in 64bit code), while sizeof(x) with the array will result in the number of bytes the array contains.
    – Matthew
    Aug 16, 2011 at 18:10
  • 2
    -1: They are not allocated in the same way. The first example is a pointer to static memory. The second example is a non-static array whose contents are initialized with "Hello world!". Aug 16, 2011 at 18:13
  • 2
    I think the idea behind const char x[] is that the array is on the stack, not simply a pointer, and "Hello World!" is copied to it. This may be optimized out, but the idea is quite different from a simple pointer. Aug 16, 2011 at 18:22
  • Nitpick for nitpick, nothing I wrote is wrong, even though it may be misleading. That said, I'll clarify. Aug 16, 2011 at 19:57
  • Much better now, so -1 removed. Aug 16, 2011 at 21:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.