Thread: I read about memory, stack, etc but I didn't understand something!

  1. #1
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246

    I read about memory, stack, etc but I didn't understand something!

    This is the memory model
    Code:
    ----------------------
    Code segment
    "Execution code"
    
    -----------------------
    Data segment
    "Global variables and constants"
    
    -----------------------------------
    Heap
    "Dynamically allocated memory"
       .
       .
       .
    
    ---------------------------------
    Stack
    "Holds function arguments and local variables and constants"
     
                      ^
    --------------------------------
    Right?

    Now my question is when heap is between data segment and stack, how there is no limitation in using new? We can allocate memory as much as we want (With an unlimited swap file :-P).
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    They're 4 separate areas of memory, there is no "between" as such. Not all systems lay out memory in the same order.

    > how there is no limitation in using new?
    The OS will not allocate overlapping memory, so at some point asking the OS for more memory will fail, and at that point calling new will throw an exception.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Are you sure you can allocate infinite memory? There are still limits somewhere? new can fail too.

    Then, are you sure this is how memory must always be laid out?
    With a little test program, if I allocate a small array of ints with new I get the following addresses:
    global int: 0x43f000
    local int: 0x22ff74
    new int: 0x3d3850

    However, if I allocate a large amount of ints (1 million), I get
    global int: 0x43f000
    local int: 0x22ff74
    new int: 0x510020
    Dynamically allocated array is not between the globals and stack.

    After all, why worry about it?

  4. #4
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    The OS will not allocate overlapping memory, so at some point asking the OS for more memory will fail, and at that point calling new will throw an exception.
    But it seems it never overlaps. It makes swap file bigger and bigger until it gets to its defined limitation.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  5. #5
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    That is because memory is randomly accessable... the execution code is given a pointer where to find the variable when it wants to read/write to it. There is no defined order to things, something to keep in mind is that programs grow and shrink in the amount of memory they use... and they have to share the envirorment with other programs who the OS can decide that it wants to give a slice to, both just would write to the next avaiable slot of memory (afaik).

    Then again I could have interpreted things wrong when I read up on this. (I think I read it in one of my CS books.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory allocation on the stack
    By R.Stiltskin in forum C++ Programming
    Replies: 7
    Last Post: 04-01-2009, 12:18 PM
  2. Replies: 7
    Last Post: 02-06-2009, 12:27 PM
  3. Need Help with Stack
    By trongsi in forum C++ Programming
    Replies: 9
    Last Post: 05-23-2006, 04:14 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. stack memory overflow checker
    By rotis23 in forum C Programming
    Replies: 3
    Last Post: 08-21-2002, 07:37 AM