Saturday 2 June, 2007

Static & Dynamic memory allocations...

I thought of writing this article since it was quite interesting even though it sounds as basic and primitive.

Last night when I was coming back to home on Company's shuttle, my mate Debendra had few C++ questions for me.

He asked "If I create a variable on stack say, int i; and if I say char *p = new char;. When will be the memory gets allocated? What time? Is it run-time or compilation time?"

I had to say "All allocations happen only when you run the program, compiler will just compile the C++ code to instructions which will allocate the memory required on heap/stack"

Then I got an unexpected question from him - "Then why do we say int i; as static allocation and char * p = new char as dynamic allocation, even though all allocations are happening at run-time?"

A crazy question!

Such a primitive and basic doubt every one may get and get confused.

All allocations whether it may be dynamic or static happens at run-time only. But how the allocation is done matters.

Let’s take the static allocation,

int i;

when compiler compiles this code, compiler knows what the size of int is and it knows what number of bytes needs to be allocate at compilation time only. It generates instructions to allocate those many bytes, so that at run-time those many bytes are allocated.

In case of dynamic allocation,

char *p = new char(n);

Compiler wont have any idea what is n, it may be a function parameter or a value evaluated by an expression or even it can be a constant value, but at compilation time it has no idea what may be the number of bytes allocated. This can be dynamically configured and controlled at run-time.

Importantly, in case of dynamic allocation, the amount of memory allocated is determined by the program at the time of allocation and need not be known in advance.

Hence we say this as dynamic allocation.

Hope Debendra had got his doubt cleared.

3 comments:

Anand Patil said...

In addition to above description I would like to add some more lines. When we declare variable on stack, the address (relative?) of the variable is already known. But when the variable is created on heap the exact address of the variable is calculated dynamically. Right?

Anonymous said...

It seems your comment is wrong, the actual allocation is happening for statically allocated variables only during the run-time (as per Prashanth's comments). After this allocation only the address will be visible to the program. The compiler just assumes for stack (local) variables how much memory needs to be allocated.

Mahesh said...

yeah.. address wont be known at compile time. If anand is talking about relative address, then we can always calculate one :) say,

func_name()
{
int i;
int j;
char* name = new char;
}

relative address of i is
i = address of func_name
j = address of func_name + 4

and in case of pointer variable also,

name = address of func_name + 8.

but name pointing to some other address in heap which will be known only at runtime. right prashanth?