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

Dani AI

Generated

A short, practical summary that ties together the existing replies from , and for :

For ordinary C++ code the new operator (and RAII) is the right choice: it allocates memory the C++ runtime expects and runs constructors/destructors. GlobalAlloc is a Win32 API that returns an HGLOBAL handle and exists mainly for compatibility with old Windows APIs; it should be used only when an API explicitly requires a global memory handle. (learn.microsoft.com)

The clipboard is one of those APIs that does require a global memory handle. The required pattern is: open the clipboard, allocate a moveable global block (GlobalAlloc(GMEM_MOVEABLE, size)), lock it (GlobalLock) to get a pointer and copy the bytes, unlock it, then call SetClipboardData(format, hMem) and close the clipboard. If SetClipboardData succeeds the system takes ownership of the handle (so the app must not free it); if it fails the application must free the handle. Also avoid leaving the memory locked when closing the clipboard and check return values (and retry OpenClipboard briefly if it is busy). (learn.microsoft.com)

A common misconception (noted by ) is that GlobalAlloc creates memory that is visible to all processes. That is historical: on modern Windows the global/local functions are wrappers around the process heap and do not provide cross-process shared memory. For true shared memory, use file-mapping objects (CreateFileMapping / MapViewOfFile) or other IPC. (learn.microsoft.com)

Practical rule: prefer new (and smart pointers) for C++ objects; use GlobalAlloc only when the API requires a global handle (clipboard, some legacy APIs). Check GetLastError on failures and follow the lock/ownership rules above. Minimal C++ call order shown below (error checks elided for brevity). (learn.microsoft.com)

if (OpenClipboard(hwnd)) {
    EmptyClipboard();
    HGLOBAL h = GlobalAlloc(GMEM_MOVEABLE, size);
    if (h) {
        void* p = GlobalLock(h);
        if (p) { memcpy(p, data, size); GlobalUnlock(h); }
        if (!SetClipboardData(CF_UNICODETEXT, h)) { GlobalFree(h); }
    }
    CloseClipboard();
}

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.