CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2009
    Posts
    40

    [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?

  2. #2
    Join Date
    May 2009
    Posts
    2,413

    Re: What is meant by "Compiler allocates memory for an array"

    Quote Originally Posted by AwArEnEsS View Post
    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.

  3. #3
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: What is meant by "Compiler allocates memory for an array"

    Quote Originally Posted by AwArEnEsS View Post
    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

  4. #4
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    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
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  5. #5
    Join Date
    May 2009
    Posts
    2,413

    Re: What is meant by "Compiler allocates memory for an array"

    Quote Originally Posted by D_Drmmr View Post
    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.

  6. #6
    Join Date
    May 2009
    Posts
    2,413

    Re: What is meant by "Compiler allocates memory for an array"

    Quote Originally Posted by S_M_A View Post
    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
    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.

  7. #7
    Join Date
    Oct 2009
    Posts
    40

    Re: What is meant by "Compiler allocates memory for an array"

    Thanks for your answers

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured