-
October 28th, 2012, 09:44 PM
#1
[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?
-
October 29th, 2012, 01:20 AM
#2
Re: What is meant by "Compiler allocates memory for an array"
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.
Last edited by nuzzle; October 31st, 2012 at 12:16 AM.
-
October 29th, 2012, 04:52 AM
#3
Re: What is meant by "Compiler allocates memory for an array"
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).
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
October 29th, 2012, 06:34 AM
#4
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
-
October 29th, 2012, 06:57 AM
#5
Re: What is meant by "Compiler allocates memory for an array"
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.
Last edited by nuzzle; October 30th, 2012 at 12:24 AM.
-
October 29th, 2012, 07:42 AM
#6
Re: What is meant by "Compiler allocates memory for an array"
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.
Last edited by nuzzle; October 30th, 2012 at 12:25 AM.
-
October 30th, 2012, 05:47 PM
#7
Re: What is meant by "Compiler allocates memory for an array"
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|