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 ?

2
  • 3
    Possible duplicate of stackoverflow.com/questions/2589949/… Commented Feb 11, 2011 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. Commented Feb 11, 2011 at 19:10

2 Answers 2

22

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.

Sign up to request clarification or add additional context in comments.

9 Comments

Maybe. If binaries on your platform have one.
@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.
@ring0 : what do you mean of where they're declared ? inside function and outside function ?
@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.
@capede, you really need to write a program in assembler. This way you will understand memory segmentation very good.
|
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.