"New" Operator question

This is a discussion on "New" Operator question within the C++ Programming forums, part of the General Programming Boards category; I know that the new operator allocates a variable in a portion of the called the heap. Now I was ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    26

    Question "New" Operator question

    I know that the new operator allocates a variable in a portion of the called the heap. Now I was wondering if the heap consists only of the unused ram, or if it also includes the swap/page file? I checked MSDN but their explanation was not exceedingly clear, so I thought that I would ask here, hence my question.

    -JLBShecky
    System
    OS - Microsoft Windows XP Pro
    CPU - AMD Athlon XP 2600+
    Mother Board - Abit KV7
    RAM - 512 Mb DDR (333)

    C++
    Microsoft Visual Studio Pro. 6.0
    MSDN July 2001

  2. #2
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    The new operator assigns a chunk of memory for use by you program and returns a pointer to it. How the heap is implemented is operating system dependent. On most operatoring systems, a swap file (virtual memory) is used to make disk space appear as RAM, BUT this should be totally transparent to your program - it just makes it appear that you actually have more RAM than you do.

    I have my virtually memory turned off because it speeds things up, but I do have 768MB of physical RAM.

  3. #3
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Technically, new allocates from the free store, malloc allocates from the heap. However, new is allowed to use malloc() to get its memory, so the free store and the heap are not always different.

    In any event, these calls allocate memory, but how they do so is not defined by the standard, and may depend on the operating system involved. E.g. to a Win32 machine, it allocates in the virtual memory space of a process. The same allocated object might move from RAM into swap file, back into RAM as it is paged in and out. A program under windows only has a pointer to a virtual address, and knows nothing about how the operating system is translating virtual addresses into physical ones.

    Under DOS, your pointers were physical pointers -- you could allocate ONLY into RAM, and the address you got was the actual address in memory.

    Generally, when you use new (or malloc), you get no guarantees as to where your object will reside. If there is a reason you need to remain ONLY in physical RAM, and not allow yourself to be paged out, you'll need to use an operating-system specific means to do this.

    But the language itself is system-independant, so it's left to compiler writers to decide how to implement these features. You could use C++ on a machine that had no ability to have a swap file. You could use C++ on a machine that didn't have any RAM at all (although I don't know offhand of any machines that truly have zero RAM).
    Last edited by Cat; 06-08-2003 at 01:50 PM.

  4. #4
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    801
    I don't know for sure but I would think it would be OS specific. I mean your app will require the memory regardless of the swap file capabilities of your OS. I use XP and a while back I was messing around trying to get a bad_alloc thrown. I allocated a gig of memory (I have 256MB) using 'new' and windows managed it ok increasing my swap file accordingly, though my PC slowed for ages. Of course if the OS didn't have that kind of capability it might just crash and burn.

    EDIT: A bit late on that one.

  5. #5
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    >However, new is allowed to use malloc() to get its memory, so the free store and the heap are not always different.

    Not sure about this. 'new' is the C++ keyword and 'malloc' is C. They are exclusive, how they are implemented is not part of the standard. If you use the C++ new to create memory, use the C++ 'delete' to destroy it.

  6. #6
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Originally posted by Davros
    >However, new is allowed to use malloc() to get its memory, so the free store and the heap are not always different.

    Not sure about this. 'new' is the C++ keyword and 'malloc' is C. They are exclusive, how they are implemented is not part of the standard. If you use the C++ new to create memory, use the C++ 'delete' to destroy it.
    What I mean is that internally, a call to new() may contain a call to malloc(). It would never be ONLY a call to malloc(). There are other things that must be done, too, as part of new().

    New, essentially, does more than just allocate memory, it also constructs an object in that space.

    So yes, new() can never be implemented ONLY as a call to malloc(), but it can contain one. Similarly, delete and delete[] can never be implemented ONLY as calls to free(), but they may contain them.
    Last edited by Cat; 06-08-2003 at 01:55 PM.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    26
    Thanks for the input folks, I had suspected that the swap/page file was included in the heap, but I wanted to check to make sure of this due to the fact that I am wanting to make a program that is able to open a file that has the possibility of taking up more space than the user may have ram, and I did not want to write that may run on my computer but not on a user's computer that has less ram than mine. Thanks for the input.

    -JLBShecky
    System
    OS - Microsoft Windows XP Pro
    CPU - AMD Athlon XP 2600+
    Mother Board - Abit KV7
    RAM - 512 Mb DDR (333)

    C++
    Microsoft Visual Studio Pro. 6.0
    MSDN July 2001

  8. #8
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Originally posted by JLBSchreck
    Thanks for the input folks, I had suspected that the swap/page file was included in the heap, but I wanted to check to make sure of this due to the fact that I am wanting to make a program that is able to open a file that has the possibility of taking up more space than the user may have ram, and I did not want to write that may run on my computer but not on a user's computer that has less ram than mine. Thanks for the input.

    -JLBShecky
    Your not taking in what the others are saying....if you open a file bigger than the amount of physical memory, the OS's (I'm assuming windows as you mention MSDN) memory management would try to page anything that it cant squeeze into ram and them swap back and forth what it needs.....of course how successful this can be is dependant on the size of the file and the size of ram...and also what other resources are using memory that cannot be paged such as locked regions and non pagable memory. If this really concerns you, set a limit on the amount of available memory you require for your program and issue a warning if that amount is not available - the GlobalMemoryStatus api will give you an idea

    As to the paging file itself, you have no control or access to it. It is mapped to an area of memory that it out of bounds to usermode programs and will crash any app that tried to dereference a pointer to that reqion.
    Heaps used for malloc or operator new on the other hand are mapped to user mode areas of virtual memory

  9. #9
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    So virtual memory uses your hard drive? If that's correct, then (assuming you need more RAM) a faster hard drive could make programs run faster? I have two hard drives, one which came with the computer (about 2 years ago) and one that's about 2-3 months old, and much better. My computer only has 256 RAM, so could changing virtual memory to the second drive make my games run faster? Would there be any drawbacks?
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  10. #10
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Unlikely that it'll really help. Reading/writing from disk is much slower than accessing the RAM directly. If your programs are already eating up enough memory that they need to swap to disk, it may help marginally, but not noticeably. My recommendation: when running memory intensive programs, leave as much physical memory available as possible (i.e. shut down lots of extraneous programs), and upgrade your memory if its still a problem.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  11. #11
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Originally posted by JaWiB
    So virtual memory uses your hard drive? If that's correct, then (assuming you need more RAM) a faster hard drive could make programs run faster? I have two hard drives, one which came with the computer (about 2 years ago) and one that's about 2-3 months old, and much better. My computer only has 256 RAM, so could changing virtual memory to the second drive make my games run faster? Would there be any drawbacks?
    One note on terminology (even MS has had this incorrect):

    Virtual memory doesn't mean that it uses your hard drive. Virtual memory means that each process "lives" in its own address space, and the operating system maps these virtual addresses to physical ones.

    A consequence of this is that these virtual addresses don't need to map only into RAM; they can map to the hard disk, to other memory, even to files on another computer, and it's transparent to your program.

    So, *swap files* can be made; they are files on your hard drive which are mapped, as needed, into the virtual memory spaces of processes. So, you could change the *swap file* to your second drive, and that might improve performance, depending on many factors. But you can't talk about moving "virtual memory"; that's just the style of memory management that all modern operating systems use.

  12. #12
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    Originally posted by JaWiB
    ... so could changing virtual memory to the second drive make my games run faster?
    it would be a good idea to put the swap space on the faster drive.

    i would also put the system/os/programs files on the faster drive and use the slower drive for storage.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie "new" nuisance
    By Sharke in forum C++ Programming
    Replies: 3
    Last Post: 05-01-2009, 11:57 PM
  2. make "new" object
    By Hunter_wow in forum C++ Programming
    Replies: 9
    Last Post: 10-05-2007, 09:48 AM
  3. "new" is strange
    By The Wazaa in forum C++ Programming
    Replies: 26
    Last Post: 03-06-2006, 12:32 PM
  4. Replies: 2
    Last Post: 05-10-2005, 08:15 AM
  5. question about "new" with pointers
    By Boomba in forum C++ Programming
    Replies: 2
    Last Post: 08-06-2004, 05:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21