[RESOLVED] What is meant by "Compiler allocates memory for an array"
I have read that compiler allocates memory for an array.What does it exactly mean?For example,if compiler allocates memory at compilation-time,let's say it allocates memory address 189 for an array at compilation-time.But when I run the executable file and if that memory address(189) is not free,what happens?
Re: What is meant by "Compiler allocates memory for an array"
Quote:
Originally Posted by
AwArEnEsS
But when I run the executable file and if that memory address(189) is not free,what happens?
Most addresses in a program are relative to something (the most common exception is addresses to hardware units which are at fixed locations).
In your example 189 would not be an absolute address but rather an offset from some base register. This base register would be set to a defined address when the program is started, say the address of the start position of the program. In this way it doesn't matter where the program is loaded into memory. It's relocatable.
Re: What is meant by "Compiler allocates memory for an array"
Quote:
Originally Posted by
AwArEnEsS
I have read that compiler allocates memory for an array.What does it exactly mean?For example,if compiler allocates memory at compilation-time,let's say it allocates memory address 189 for an array at compilation-time.But when I run the executable file and if that memory address(189) is not free,what happens?
An array is allocated on the stack, so all the compiler has to do to allocate that memory is change the stack pointer. If the stack is not big enough to hold the array, your program will crash. The compiler does not have to consider this.
That's exactly why you should not use an array to store large amounts of data; it may not fit. It is also why stack allocation is faster than heap allocation (i.e. using new or some other allocation function).
Re: What is meant by "Compiler allocates memory for an array"
In most cases (in Windows/Linux and similar OSes) the address is a virtual address instead of a physical one. See http://en.wikipedia.org/wiki/Virtual_memory
Re: What is meant by "Compiler allocates memory for an array"
Quote:
Originally Posted by
D_Drmmr
An array is allocated on the stack,
Not necessarily. An array is allocated like any other variable. It doesn't have to be on the stack (in automatic memory) but can be in static memory or on the heap too. The programmer decides.
But regardless of how the array is allocated it will be at a position relative to the program and not at an absolute position in total memory (as I explained in my previous reply).
Although stack allocation is usually faster than heap allocation in practice and although the stack is often smaller than the heap these are not eternal truths cut in stone and shouldn't be presented as such.
Re: What is meant by "Compiler allocates memory for an array"
Quote:
Originally Posted by
S_M_A
That's something else.
You have physical vs. virtual memory and you have absolute vs. relative addressing.
a) Relative addressing is how virtual memory is implemented. It makes physical memory seem larger.
b) Relative addressing is how programs are made relocatable. It makes them independent of where they run in memory.
These are two different uses of relative addressing. They're not the same and they're independent of each other. Both relocatable and non-relocatable programs can run in virtual and physical memory.
Re: What is meant by "Compiler allocates memory for an array"