Reputation: 8772
Apart from global and static data what else are allocated on Data segment ?
I remember reading somewhere that constant strings are also allocated on Data segment and same memory is used when a reference to same string constant is made
ex:
char* returnPointer()
{
char *p = "hello world"
//some code
return p;
}
void foo()
{
char *s = "hello world"
//some code
}
In the above code,
Does the memory for constant "hello world" allocated on Data segement or stack (just like any other local variable) ?
If allocated on Data segment, does both p and s point to same location ?
Upvotes: 0
Views: 1240
Reputation: 500933
Does the memory for constant "hello world" allocated on Data segement or stack (just like any other local variable) ?
This is compiler-specific, but typically string literals live on the initialized data segment (often a special read-only portion of the initialized data segment). I don't know of any architecture where string literals could plausibly live on the stack.
If allocated on Data segment, does both p and s point to same location ?
That's up to the compiler.
When I compile your code using gcc 4.4.6, I see the following:
.section .rodata
.LC0:
.string "hello world"
.text
.globl returnPointer
.type returnPointer, @function
returnPointer:
.LFB0:
.cfi_startproc
...
movq $.LC0, -8(%rbp)
...
ret
.cfi_endproc
.LFE0:
.size returnPointer, .-returnPointer
.globl foo
.type foo, @function
foo:
.LFB1:
.cfi_startproc
...
movq $.LC0, -8(%rbp)
...
ret
.cfi_endproc
.LFE1:
.size foo, .-foo
.ident "GCC: (GNU) 4.4.6 20110731 (Red Hat 4.4.6-3)"
Here, the string literal is stored in a read-only data (.rodata
) section. The compiler is smart enough to realize that exactly the same literal is being used twice in the compilation unit, and it puts only one copy of it in .rodata
.
Upvotes: 2
Reputation: 24915
For your first question, the answer is that, "hello world" will be stored in a read-only memory area.
For your second question, the answer is it may or may not. It cannot be guaranteed that they will have the same address.
Some more Info below:
Initialized Data Segment:
Initialized data segment, usually called simply the Data Segment. A data segment is a portion of virtual address space of a program, which contains the global variables and static variables that are initialized by the programmer.
Note that, data segment is not read-only, since the values of the variables can be altered at run time.
This segment can be further classified into initialized read-only area and initialized read-write area.
For instance the global string defined by char s[] = “hello world” in C and a C statement like int debug=1 outside the main (i.e. global) would be stored in initialized read-write area. And a global C statement like const char* string = “hello world” makes the string literal “hello world” to be stored in initialized read-only area and the character pointer variable string in initialized read-write area.
Ex: static int i = 10 will be stored in data segment and global int i = 10 will also be stored in data segment
Uninitialized Data Segment:
Uninitialized data segment, often called the “bss” segment, named after an ancient assembler operator that stood for “block started by symbol.” Data in this segment is initialized by the kernel to arithmetic 0 before the program starts executing
uninitialized data starts at the end of the data segment and contains all global variables and static variables that are initialized to zero or do not have explicit initialization in source code.
For instance a variable declared static int i; would be contained in the BSS segment. For instance a global variable declared int j; would be contained in the BSS segment.
Upvotes: 0
Reputation: 206646
Does the memory for constant "hello world" allocated on Data segement or stack (just like any other local variable) ?
No, it gets allocated in some read only implementation defined memory area. The C standard does not exactly define where it should be stored. It only guarantees that the string literal will have a static storage duration and it should not be modified by an user program.
If allocated on Data segment, does both
p
ands
point to same location ?
This solely depends on how efficient the compiler being used is. An efficient compiler might optimize and allocate only a single string, while another compiler might not to do so.
Anyhow, You should rely on these behaviors as they have nothing to do with user of the language, they are implementation details that an user program should not rely on.
Upvotes: 2