Whats the point of GlobalAlloc()?

closed account (10oTURfi)
Today I found out about a function called GlobalAlloc. I was thinking about it and I couldnt find any way to apply it in a program (probably beacuse I'm still new in programing)
Moments later I was like: RAM EATER!
1
2
3
4
5
6
7
#include <Windows.h>

int main()
{
	while(true)
		GlobalAlloc(0, 5000);
}


Now seriously.

Whats the point?
That code you've just posted is about the most destructive piece of code I've ever seen posted. Do you have any idea at all what it is doing? For your information, GlobalAlloc is a tremendously useful function, and I use it all the time. If it weren't for GlobalAlloc(), malloc(), HeapAlloc(), etc., there wouldn't be any computers.

Your code above gobbles up every byte of ram your computer has in 5000 byte chunks. Now seriously, What's the point?

And you never use GlobalAlloc without storing the return value, because the return value must be returned back to the operating system with GlobalFree(). You obviously don't know what you are doing.
Last edited on
MSDN is your friend: http://msdn.microsoft.com/en-us/library/windows/desktop/aa366574(v=vs.85).aspx .

Read the note, then read the remarks. Basically, you'll find a bunch of memory functions in the Windows API. The differences were important in 16-bit Windows, but not anymore. Now many of these functions behave the same.

But again, the best you can do is check MSDN.
And please forgive me for being somewhat affronted when I see code like that. I know you are just looking for help, but it doesn't help to condem a function you are clueless as to its use.

If you know C++ then you've likely heard of the 'new' operator. new allocates memory for objects, be they simple things like ints, or complex ones like classes. GlobalAlloc does about the same thing - except at a lower level.

In order to have the very first inklings of what these functions do, you must learn to use pointers to memory. This is what GlobalAlloc returns, as well as the C++ new operator. When you are done using memory with new you call delete on it so the memory can be returned to the operating system. With GlobalAlloc you must call GlobalFree on the pointer.
closed account (10oTURfi)
Yes I know what that code is doing, its most evil piece of code I could ever think of :)

Anyway... how can you tell how much bytes of memory you will need? What do you do with that chunk of memory you allocated? Isnt it easier to either use new or put the objects into STL container? I am also not quite sure how to use the pointer returned by that function.
MSDN isnt answering my question: "Why?"
Last edited on
Well, MSDN can unveil the why only if you know a certain amount of C++. You mention the new operator, so you need to know that eventually, the CRT calls an OS function to allocate the memory in response to a call to the new operator. In modern Windows, new is most likely mapped to one of the Heap*() functions, but just so you know, GlobalAlloc() does pretty much the same thing. If you read MSDN, you must have seen that it has a peculiarity: The memory address returned is 8-byte aligned. If you ever need an 8-byte aligned pointer, this is the function to use.
Krofna wrote:
its most evil piece of code I could ever think of :)


More evil than a fork bomb?

1
2
while(1)
  fork();
closed account (10oTURfi)
More evil than a fork bomb?

Ok now I wanna know what does fork() do.
Tell pls :)
As I understand it, fork() duplicates the running process, but it is a concept not found under Windows. There is no such thing as forking in Windows.
GlobalAlloc is how you ask for memory in WIN16. GlobalAlloc is still in used in WIN32, but only in support of WIN16 functions that require it that were migrated to WIN32 (like clipboard functions).

In WIN32, it's replaced by VirtualAlloc, sort of.
I was told by the MicroSoft boys that the new operator actually uses malloc() and so on. That's why I rarely use the new operator. I just go directly to the memory functions.

To the OP, if you have Petzold's book, he demonstrates how to use GlobalAlloc() and GlobalFree() in his section on copying and pasting to the clipboard.

If you don't have Petzold's book and yet you want to become a good Windows programmer, they buying his book is a must. Even though it was written over ten years ago, it is still immensely useful. You can find it here --

http://www.amazon.com/Programming-Windows%C2%AE-Fifth-Microsoft/dp/157231995X/ref=sr_1_2?ie=UTF8&qid=1317436855&sr=8-2
new operator actually uses malloc() and so on


It might. It depends on the implementation.

That's why I rarely use the new operator. I just go directly to the memory functions.


That's a mistake. new calls ctors. malloc doesn't. trying to malloc any complex type will fail catastrophically. Likewise, delete calls dtors, but free doesn't.

Also, malloc isn't typesafe

Also, it's easier to botch the job with malloc because you have to do the whole sizeof(type) thing.



Really, malloc has no place in C++. You should pretty much always use new.
And even if new uses malloc(), it may very well be possible that malloc() uses the OS' heap functions in the Windows CRT.
new isn't an alternative to system allocators like malloc.

new is a C++ operator that obeys a number of rules that make them very useful.

You can overload new, allowing for easy pool specialisation in dfferent parts of the same program.

new initialises the object under construction, solving the single biggest problem with C.

malloc is the Unix system allocator. It only made it into C because it could not sensibly be decoupled. But each OS has its own system allocator which malloc in the C runtime library maps on to.

Okay, I stand corrected... and informed.
Topic archived. No new replies allowed.