1 2
|
char foo [N];
char bar [N];
|
Two distinct blocks of memory, allocated from stack. Cannot be overlapping.
1 2 3 4
|
{
char foo [N];
}
char bar [N];
|
Two arrays, allocated from stack.
However, they might not exist at the same time.
If bar is allocated
after foo has been deallocated (and stack "unrolled"), then same memory might be reused.
1 2
|
const char * foo = "1234";
const char * bar = "34";
|
More than two distinct blocks of memory.
Both foo and bar are separate pointer objects allocated from stack.
Literal constants are stored in separate, read-only memory resource. At least 5 bytes of it will be used, for the values
{ '1', '2', '3', '4', 0 }
It is up to the compiler to decide whether it assigns the address of that literal '3' into the bar, or will it add the three bytes { '3', '4', 0 } separately to the read-only memory area.
Edit: it is quite likely that the compiler does compress the list of literals.
Easy to test too:
lit.cpp
1 2 3 4 5 6 7 8
|
#include <iostream>
int main()
{
const char * foo = "Slartibartfast";
const char * bar = "bartfast";
std::cout << foo << bar;
}
|
$ g++ -O2 -Wall -Wextra lit.cpp -o lit
$ strings lit | grep bart
Slartibartfast |