You are here:

C/compile,run time mem allocation

Advertisement


Question


hello sir

i would like to know what is the difference between memory allocation at compile time or memory allocation at run time ?

could you explain this with any example ?

thank you
monika

Answer
Memory allocation at compile time is also called static allocation.
Memory allocation at run time is also called dynamic memory allocation.
When you define a variable:
int a[10];
The space for a is allocated during compile time.
The reason is, the storage size is already known for int variable and (10 * sizeof(int)) is the total size needed.
If you declare
int *a;
The size of storage needed is not known at compile time.
So, you can call malloc() and pass a variable to it as parameter. And this allocation will be done during runtime.
Since the parameter passed to malloc() is a variable, everytime a different sized memory chunk can be allocated.

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Narendra

Expertise

I can answer questions in C related to programming, data structures, pointers and file manipulation. I use Solaris for doing C code and if you have questions related to C programming on Solaris, I will be able to help better.

Experience

6.5

Organizations belong to
Sun Microsystems

Awards and Honors
Brain Bench Certified Expert C programmer.
Advanced System Software Certified

©2012 About.com, a part of The New York Times Company. All rights reserved.