Should I use HeapAlloc or malloc/new?

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
Rapid Cube
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 22
Joined: Mon Mar 14, 2011 11:43 pm
Programming Language of Choice: C++

Should I use HeapAlloc or malloc/new?

Post by Rapid Cube »

As I understand it, malloc and new both call HeapAlloc internally. The program I'm working on at the moment is a win32 application, so portability isn't really an issue. I'm not sure what the overhead in malloc and new does and if its really necessary or not. So if anyone knows please tell me.
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: Should I use HeapAlloc or malloc/new?

Post by short »

Depends. If your using C++ libraries use new. If your using C libraries use malloc.

I would go into more detail but google a mouse click away.
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Should I use HeapAlloc or malloc/new?

Post by Falco Girgis »

I have never even heard of anybody bypassing malloc() or new on a win32 platform and just directly calling heapAlloc(). Either the overhead is absolutely tiny, or (more than likely) the overhead is inlined and optimized away by the compiler, so it's free. I wouldn't bother.
Rapid Cube
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 22
Joined: Mon Mar 14, 2011 11:43 pm
Programming Language of Choice: C++

Re: Should I use HeapAlloc or malloc/new?

Post by Rapid Cube »

I looked through malloc.c and the overhead is really tiny. First it checks to see if you're trying to allocate more memory than the system could possibly allow. Then it checks to see if _crtheap (which I imagine is a handle to the default process heap) is null. After that it calls HeapAlloc and if that succeeds the function returns. You can supply your own memory handling function that's called in the event that HeapAlloc fails. This function is called until HeapAlloc can succeed or your function returns 0.
If anyone wants to try to do this then first call _set_new_mode(1), then call _set_new_handler passing a function like this:
int NewHandler(size_t size). To stop malloc from calling your new handler function call _set_new_mode(0). To clear the new handler call _set_new_handler(0).
Last edited by Rapid Cube on Wed May 04, 2011 1:41 pm, edited 1 time in total.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Should I use HeapAlloc or malloc/new?

Post by Falco Girgis »

Damn, that's good to know. Thanks for reporting the find. :)
Rapid Cube
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 22
Joined: Mon Mar 14, 2011 11:43 pm
Programming Language of Choice: C++

Re: Should I use HeapAlloc or malloc/new?

Post by Rapid Cube »

It seems you would mainly use win32's native heap functions for dealing with user created heaps.
Post Reply