somenath wrote:
I have a question regarding the memory allocation of array.
For example
int array[10];
Now according to my understanding 10 subsequent memory location will
be allocated of size sizeof(int) *10 and this is done at compile
time.
Does it mean C compiler reserve some amount of memory for the array
at compile time?
The C standard doesn't specify how memory is allocated, so the answer is
likely to vary between implementations .
Having said that, if the array is a local object it is most likely
allocated on a hardware stack provided by the system. In this case
the "space" for the array doesn't exist until the machine instructions
that allocate the space on the stack are encountered. This will
typically be some type of SUB SP,NNN or a series of PUSH instructions.
They will be executed each time the block (or function) is entered and
the opposite clean-up instructions (POP or more likely ADD SP,NNN) will
be executed when the block (or function) is about to be left.
I suppose that allocating memory for local objects from a software stack
would be considerably different. I have not encountered any C compiler
do such a thing, but an interpreter is likely to employ this method.
If your array is a static or file scope object, then the "space" for it
most likely "coded" into the executable itself in some way, either by
actually reserving the necessary bytes or through special instructions
to the program loader which then allocates the memory before the
program is started. The details are likely to vary quite a lot between
systems.
That means with out the line of code being executed some amount of
memory is getting exhausted.
I think you might want to reconsider this.
I think I am misinterpreting the idea of compile time memory
allocation. Please provide some inputs to correct my understanding.
It's hard to provide more input without actually considering an
implementation, and that is off-topic here. Perhaps you might want to
consult a group for your system, or look-up the online version of the
book /Linkers and Loaders/ by Levine, which deals with some aspects of
your question. Also read about stack frames.