Thread: difference between new and HeapAlloc?

  1. #1
    julie lexx... btq's Avatar
    Join Date
    Jun 2002
    Posts
    161

    difference between new and HeapAlloc?

    so, can annyone tell me the difference and what to use?
    thanktankkk
    /btq

    (was this a dumb question?)

  2. #2
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    use new.

    new calls class constructors and delete calls destructors.
    Best Regards,

    Bonkey

  3. #3
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    he may not be coding C++.

    That having been said, I see no advantage to HeapAlloc over new. GlobalAlloc at one time had an advantage but may not anymore. However, GlobalAlloc is still necessary for certain operations such as Clipboard copying and pasting of odd types.

    These allocation routines have the feature of using handles, thus "movable" memory. However I think I remember hearing that all memory was "virtual pointer" anyway in 32 bit. This includes new. That last bit I'm not entirely sure of but if true completely negates the reasons for using GlobalAlloc and HeapAlloc
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    HeapAlloc is an API...its a function that's part of the system's documented interface

    new is an operator that's implemented in the C++ libraries..as said, it calls constructors and is more type safe.....on many implementations (including VC++) it calls malloc (its C counterpart) to do the dirty work (malloc could then call HeapAlloc to do the dirty work - not too sure on this, but I have noticed calls to something resembling HeapAlloc in the bowels of malloc )

    GlobalAlloc is remnant of 16bit windows... these days it probably calls HeapAlloc and is slower than needs be......you cant use it to transfer memory between processes anymore, so its largely just for legacy code...Also, the return is a handle but that is just a 32bit pointer anyway and all GlobalLock seems to do is cast to LPVOID....so IMO, forget GlobalAlloc....

    My preference - I try to use "new" as much as possible

  5. #5
    julie lexx... btq's Avatar
    Join Date
    Jun 2002
    Posts
    161
    thanks for yer replies...although I was more interested in how they relate when it comes to allocating memory on the heap in terms of heapfragmentation, speed issues etc...

    but thanks anyhow
    /btq

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Here's an essay (well more of a rambling) by someone who knows his stuff here

Popular pages Recent additions subscribe to a feed