So going over the new DirectXMath and loving how it has better documentation then the old versions, but anyways.

It makes a note on many of its targets need to have aligned memory blocks, but that throws me for a loop because I don't know of a way to declare an aligned memory block other than _aligned_malloc(...).

Also I never really thought about it, but in the documentation it goes over the difference between malloc and new as new calls a constructor so not to embed them in classes without overriding the new function to properly allocate these members with _aligned_malloc(...)

So this raises a couple questions for me about new. Firstly, is their an actual constructor call for value type members? Such that malloc() is better to call large fields of data for rather than new char[..]?

When you override new how do you do this for a class if you want alignment? If it's a 20 byte class do you simply ask for 20 bytes from malloc and return a casted pointer like

class Foo
   static Foo* operator new(size_t size)
      void* p = _aligned_malloc(size,16);
      return (Foo*)p;

This would create a set of bytes 32 byte memory correct?

malloc() is designed to be used with C new is used with C++

Therefore, if you are writing C++, use new

new is designed for general purpose, it is good for big object(like 1024 bytes or more)
the benefits of new over malloc are type safe and could be overwrite
but new may squander a lot of spaces when you deal with small objects

if you need to allocate small objects frequently and it will be the bottleneck of
your program, you could find some allocators from 3rd parties
or write one for yourself(then you may need to overwrite operator new)

there are no silver bullet to solve all of the problems related to memory allocation
but in most of the cases, we don't need to overwrite operator new or design allocators
by ourselves

Edited 5 Years Ago by stereomatching: n/a

This article has been dead for over six months. Start a new discussion instead.