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

Recommended Answers

All 6 Replies

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.

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.

commented: convincing +0

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...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.