This site is no longer active and is available for archival purposes only. Registration and login is disabled.

New Vs Malloc()


New Vs Malloc()

Postby DillRye » Feb 5, 2004 @ 12:40am

User avatar
DillRye
pm Insider
 
Posts: 477
Joined: Apr 25, 2002 @ 7:28am
Location: Iowa State University of Eng


Postby warmi » Feb 5, 2004 @ 12:52am

warmi
pm Insider
 
Posts: 518
Joined: Aug 24, 2002 @ 8:07am
Location: Chicago USA


Postby Digby » Feb 5, 2004 @ 1:50am

<yoda voice>If you're app is allocating memory with enough frequency that you'd notice any difference between the two methods, you've got design problems that you should address first. </yoda voice>

Both operator new and malloc call HeapAlloc underneath and that is where the real work is. From a design standpoint if you're writing a C++ app where you're already using operator new to allocate memory for classes, you're better off using it to allocate buffers as well. This reduces the chance that you'll get confused in calling operator delete[] vs. free() when you go to destroy the buffer. It also makes it easier to override the memory allocator and track memory leaks and other sort of diagnostics if your app allocates/frees memory through a single point of code.
Digby
pm Insider
 
Posts: 1011
Joined: Apr 29, 2001 @ 1:53pm


Postby DillRye » Feb 5, 2004 @ 4:06am

ok so what you guys said is basically(except I didnt talk or know about the heap alloc stuff digby explained) what I was thinking, but heres a question I got in response:

"And tell me how you deal with a malloc failure within a new call?"

I dont really know since I dont ever use malloc. I assume new handels this by itself and is one of the advantages. Anyone have any ideas?
User avatar
DillRye
pm Insider
 
Posts: 477
Joined: Apr 25, 2002 @ 7:28am
Location: Iowa State University of Eng


Postby mlepage » Feb 5, 2004 @ 4:22am

There are a few different functions new calls when it fails. You can set these to your custom handlers if you wish to handle those types of errors. Otherwise, an exception is thrown.
www.scalenesoftware.com
Great games for your Palm and Pocket PC!
User avatar
mlepage
pm Insider
 
Posts: 1050
Joined: Aug 3, 2003 @ 4:47am
Location: Canada


Postby StephC » Feb 5, 2004 @ 12:12pm

Stephane Cocquereaumont / Game Developer at <a href=http://int13.net>int13 production</a> (code monkey)
User avatar
StephC
pm Insider
 
Posts: 442
Joined: Jun 12, 2003 @ 10:41am
Location: Bordeaux - France


Postby Dan East » Feb 5, 2004 @ 2:47pm

User avatar
Dan East
Site Admin
 
Posts: 5264
Joined: Jan 25, 2001 @ 5:19pm
Location: Virginia, USA


Postby StephC » Feb 5, 2004 @ 2:59pm

Stephane Cocquereaumont / Game Developer at <a href=http://int13.net>int13 production</a> (code monkey)
User avatar
StephC
pm Insider
 
Posts: 442
Joined: Jun 12, 2003 @ 10:41am
Location: Bordeaux - France


Postby warmi » Feb 5, 2004 @ 4:18pm

Well everything has its place but gotos and globals are definitely not a way to proceeed when dealing with reasonable system.
Personally, I think C++ is the way to go when dealing with a typical user space application but the reality is that a lot of people are used to the "old" ways of doing things and are highly resistant to any change :-)

I still remember the days when everything was written in ASM not because performance reasons but rather due to memory limitations.
StephenC is right when it comes to use of new and malloc - as soon as the game loop is rolling, it is unwise to use these functions on a continuous basis on a PocketPC device.

On the other hand, while I can understand lack of threading , replacing loops with state driven code seems insane ...
warmi
pm Insider
 
Posts: 518
Joined: Aug 24, 2002 @ 8:07am
Location: Chicago USA


Postby StephC » Feb 5, 2004 @ 4:59pm

Stephane Cocquereaumont / Game Developer at <a href=http://int13.net>int13 production</a> (code monkey)
User avatar
StephC
pm Insider
 
Posts: 442
Joined: Jun 12, 2003 @ 10:41am
Location: Bordeaux - France


Postby warmi » Feb 5, 2004 @ 5:16pm

warmi
pm Insider
 
Posts: 518
Joined: Aug 24, 2002 @ 8:07am
Location: Chicago USA


Return to Windows Mobile


Sort


Forum Description

A discussion forum for mobile device developers on the Windows Mobile platform. Any platform specific topics are welcome.

Moderators:

Dan East, sponge, Digby, David Horn, Kevin Gelso, RICoder

Forum permissions

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

cron