what is the difference between memory allocated by GlobalAlloc & memory allocated by new .
is GlobalAlloc used in C++ or is it for C?
:)

No, GlobalAlloc is something unique to various windows platforms (search MSDN for it).

new is the standard C++ way to allocate memory.
Further, new also ensures that constructors get called to initialise the objects correctly.

How any commercial compiler manufacturer implements the C++ keyword 'new' is likely proprietary information, I expect. GlobalAlloc is of course unique to Win32. Its a function callable from any language capable of calling external functions. I use it a lot in PowerBASIC. I also use it in C and C++. So I wouldn't say the issue is unique to any language wheter C family or not. Of course you realize GlobalAlloc() isn't portable whereas new is - at least in the C++ world.

Yes, like Salem said, the differences between basic memory allocation functions (there are a lot of them in Win32) and the 'new' C++ operator is that 'new' does things very specific to the inner workings and logic of the C++ language relating to the operations of classes in terms of constructors and destructors, whereas basic memory allocation functions just give you a consequitive array of bytes.

Edited 6 Years Ago by Frederick2: n/a

when I deal with clipboard, I have to use GlobalAlloc.
is it recommended to use GobalAlloc where new can be used without problem (in Windows programming)?

> when I deal with clipboard, I have to use GlobalAlloc.
Because it tells you to in the manual pages perhaps?

If the API's you're using have special requirements which mean you MUST use GlobalAlloc's special properties, then that is what you must use.

If you're just writing standard C++, then use new.

when I deal with clipboard, I have to use GlobalAlloc.

That makes sense. new allocates within the realm of the program. Once the program exits, the memory is released. The clipboard then cleared (or broken). GlobalAlloc , I assume, leaves the allocated memory until the system clears it.

is it recommended to use GobalAlloc where new can be used without problem (in Windows programming)?

No.

Comments
convincing

new allocates within the realm of the program

so if I use GlobalAlloc to allocate memory, all programs will have it in common. programs can cooperate then...

This article has been dead for over six months. Start a new discussion instead.