15

Possible Duplicate:
C String literals: Where do they go?

As far as I know,

generally, pointer have to be allocated by malloc(), and will be allocated to heap, then unallocated by free();

and

non pointer(int,char,float,etc..) will be allocated automatically to stack, and unallocated as long as the function go to return

but, from following code :

#include <stdio.h>

int main()
{
char *a;

a = "tesaja";

return 0;
}

where will a allocated to ? stack or heap ?

trincot
  • 317,000
  • 35
  • 244
  • 286
capede
  • 945
  • 1
  • 13
  • 26
  • 3
    Possible duplicate of http://stackoverflow.com/questions/2589949/c-string-literals-where-do-they-go – Alexander Gessler Feb 11 '11 at 15:31
  • In your example, "tesaja" is called a string literal. The text is constant, a.k.a. read-only, and could be placed anywhere. It could be placed in the executable area *and copied* to writeable memory. The actual location depends on the compiler settings and the platform. – Thomas Matthews Feb 11 '11 at 19:10

2 Answers2

20

The string literal will be allocated in data segment. The pointer to it, a, will be allocated on the stack.

Your code will eventually get transformed by the compiler into something like this:

#include <stdio.h>

const static char literal_constant_34562[7] = {'t', 'e', 's', 'a', 'j', 'a', '\0'};

int main()
{
    char *a;

    a = &literal_constant_34562[0];

    return 0;
}

Therefore, the exact answer to your question is: neither. Stack, data, bss and heap are all different regions of memory. Const static initialized variables will be in data.

ulidtko
  • 14,740
  • 10
  • 56
  • 88
  • 2
    Maybe. If binaries on your platform have one. – Flexo Feb 11 '11 at 15:31
  • 2
    @capede And *non pointer(int,char,float,etc..) will allocated automatically to stack* is wrong. A pointer is a variable, a container, and its size depends on the size of a memory address. Its allocation (the pointer, not the address to which it points to) follows the same rule as the other variables, depending on where they are declared. – Déjà vu Feb 11 '11 at 15:56
  • @ring0 : what do you mean of `where they're declared` ? inside function and outside function ? – capede Feb 11 '11 at 16:42
  • @capede yes for instance. Inside a function, a block, as a parameter, as global variable..., but also as static, as volatile... The compiler decides in each case how it has to handle the variable space allocation. – Déjà vu Feb 11 '11 at 16:52
  • do you have any idea where i can get documentation about `variable in space allocation` ? – capede Feb 11 '11 at 17:43
  • There is another region of memory: *executable*. This is where the processor instructions are stored. Some compilers may place data into the executable region, especially constants. There is no standard that requires string literals placed into a *data* segment. – Thomas Matthews Feb 11 '11 at 19:07
  • what is `executable region of memory` called ? is that `stack` ? or there is no specific name, just `executable region of memory` ? – capede May 16 '11 at 18:55
  • 1
    @capede, you really *need* to write a program in assembler. This way you will understand memory segmentation very good. – ulidtko May 17 '11 at 04:01
  • i will entering world of assembler soon after being monk on C :D – capede May 17 '11 at 04:23
11

a itself (the pointer) is defined as a local variable (implicitly) using the auto storage class, so it's allocated on the stack (or whatever memory the implementation uses for stack-like allocation -- some machines, such as IBM mainframes and the first Crays, don't have a "stack" in the normal sense).

The string literal "tesaja" is allocated statically. Exactly where that will be depends on the implementation -- some put it with other data, and some put it in a read-only data segment. A few treat all data as read/write and all code as read-only. Since they want they string literal to be read-only, they put it in the code segment.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111