PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

New Vs Malloc()

Ok, so I got in a little argument with someone on another message board about how I believe the performance of new vs malloc is neglegable.

I was wondering what people here thought, hopefully some of the hard core optimization guys will answer :).

Thanks, Dylan.
New will most likely call malloc anyway :-)

You can consider new() to be a wrapper around malloc - as with all wrappers it won't be as fast as the original call but the difference won't be great.
<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.
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?
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.
Before writting games for mobiles devices I was working on embedded "industrial" real time software, and both new() and malloc() were prohibited.

I think that a game could be designed with the same rule : NEVER use dynamic allocation during the run time.

Write your own memory allocator.

(for some systems (automotive engine controllers) loops were also prohibited and all the code was written as state machines.)
StephC wrote:(for some systems (automotive engine controllers) loops were also prohibited and all the code was written as state machines.)


That probably is a result of how simplistic and single-purpose the hardware is. Their goal isn't to create a robust programming environment for developers, but to be able to cheaply mass produce reliable chips.

Dan East
Dan East":3fhpb28s]

The processor used was the C167 (Siemens) it's a 68000 core with some added hardware to control peripherals.

A lot of things were prohibited (threads, malloc, loops) but I don't think it was a "simplistic" system, in fact I've almost never seen a more complex and robust piece of code.

I was responsible of one of the most math intensive module : EGR (Exhaust Gaz Recirculation), written entirely with integer math, LUT and state machines.

We were using globals and "goto" massively and that was not considered "evil".
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 wrote:Well everything has its place but gotos and globals are definitely not a way to proceeed when dealing with reasonable system.


gotos are not very practical in C, so there is not very often a reason to use them, but globals, if used carefully, can greatly simplify the API of any system.

For example, in uEngine, the adress of the backbuffer is always contained in a global, same for the state of the input (mouse, keys), and I don't see any reason to not store those data as globals.


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 think C++ is the way to go for people who know *exactly* what they are doing, my main fear with those OO language is that people often write very inefficient code with them (I've seen numerous example).


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.


The use of complex data structure is eased by the use of higher level language, linked list or tree are often used when there is absolutely no necessity..


On the other hand, while I can understand lack of threading , replacing loops with state driven code seems insane ...


For this system (automotive embedded calculator), this was absolutely needed.

Think of a game that have to go at 6000 Frame per Second and always handle all the critical operation at this frame rate...
"the adress of the backbuffer is always contained in a global, same for the state of the input (mouse, keys), and I don't see any reason to not store those data as globals."

Well, I do the same thing except I tend to use singletons with this sort of information attached.
For example retrieving current input state would be a static method for a WR_Application class.
As far as other globals, I prefer to create a static class and wrap all system-wide variables inside that class.
Underneath it really works similar to your typical C global but it results in much better organized and readable code.

"I think C++ is the way to go for people who know *exactly* what they are doing, my main fear with those OO language is that people often write very inefficient code with them (I've seen numerous example)."

Of course, one has to understand how C/C++ works to be able to use them efficiently.
C++ is simply a codified way of what C programmers have been doing for ages - creating structs with function pointers etc ..
Most of C++ programmers I know, started out with C and thus have no problem using C++ efficiently - my experience has been that the problem you are describing happens mostly in Java community...

"Think of a game that have to go at 6000 Frame per Second and always handle all the critical operation at this frame rate...
"

Well, yeah - I have done things like that back in 80s with compiled sprites etc but we didn?t have a choice back then , these days one could simply use a slightly faster CPU and create more maintainable code .