_aligned_malloc is always better than malloc?

I heard _aligned_malloc allocate in memory without memory fragmentation.
but malloc can make a memory fragmentation.

because actually malloc does not always allocate memory in physical memory in order.

if I allocate in p like this,

int* p = (int*)malloc(sizeof(int)*5);

Maybe p is 0x0004, but p+1 is not always 0x0008 in physical memory.

but _aligned_malloc do allocate always like that.

then.. _aligned_malloc is always better than malloc?
Last edited on
I would expect aligned_malloc to cause more memory fragmentation.

I would also expect that this is very much not something you should have to think about or worry about 99% of the time.

As an aside, this is C++; can I interest you in not using C code such as malloc, and also not using new?
How about using safer, modern C++ to handle your memory?

Based on this post and others, it looks like you're learning low-level C coding for hardware device driver creation and similar. Is that what you meant to learn?
int* p = (int*)malloc(sizeof(int)*5);

Maybe p is 0x0004, but p+1 is not always 0x0008 in physical memory.


If the size of an int is four bytes, and p is 0x0004, p+1 will be 0x0008. Do you have an example when this isn't true? I'd be really surprised. It would make no sense. It would break everything. Maybe I misunderstand you.

malloc gives you a single block of memory. It has to; pointer arithmetic wouldn't work otherwise. It would break everything.
Last edited on
// Repeater
Thank you for your comment.
actually I am implementing doubly-linked-list like STL list class.
and most container use allocator class. so I was finding how to use allocator and custom allocator.

so again I was finding how to implement allocate function.
and someone uses new.. someone uses malloc, someone uses _aligned_malloc.. and so on..
so I just needed to know about memory allocation functions and which is more effective.

and maybe I think I saw the wrong information.
I saw that info this blog.. but written by Korean.
https://chfhrqnfrhc.tistory.com/entry/alignedmalloc
Last edited on
I heard _aligned_malloc allocate in memory without memory fragmentation.
but malloc can make a memory fragmentation.

because actually malloc does not always allocate memory in physical memory in order.
Not true. Both allocate a single block of memory.

According to the microsoft docs "_aligned_malloc is based on malloc."
https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/aligned-malloc?view=vs-2019

Aligned malloc simply returns a block that is aligned on a particular power-of-two address boundary.

I am implementing doubly-linked-list like STL list class.
and most container use allocator class. so I was finding how to use allocator and custom allocator.
It's a good idea to understand allocators, but usually you don't need them. Just let the compiler use the default allocator.

someone uses new.. someone uses malloc, someone uses _aligned_malloc.. and so on..
so I just needed to know about memory allocation functions and which is more effective.
Before you decide which is more "effective", you should be crystal clear on how different allocators behave differently:
new (and new[]) allocate memory and call the constructor. The memory must be deallocated with delete or delete[].
malloc() just returns a block of memory. It is uninitialized and must be dellocated with free() The documentation usually doesn't state this, but the memory is suitably aligned to store any data type.
calloc() is like malloc(), but the parameters are different and the returned memory is zero-initialized.
_aligned_malloc() returns a block allocated on the requested boundary. It must be freed with _aligned_free()

I haven't even mentioned what happens if the allocation fails, but there are differences there too.
I strongly advise you not to try to second guess alignment / padding anywhere in code without very good reasons.

the default alignment for your platform is usually fine, and messing with it to try to force something will lead to performance problems if you migrate the code to a different platform even if you got it right for the original target.

you can get a performance boost on a target platform on occasion, and sometimes this is a big deal. But its not a good strategy for everything, use this only when you need to do so. The last time I can remember doing this in code, the program ran on a system with low double digit MHZ (talking below 50 MHZ) and every little thing mattered a lot.
Last edited on
// dhayden
// jonnin
I appreciate your feedback and comments! It helped me a lot
As an aside, this is C++; can I interest you in not using C code such as malloc, and also not using new?
How about using safer, modern C++ to handle your memory?


We do C here too.
Gosh really, kbw? I never noticed. Even in this very post in which we all discussed C code, I never noticed we were doing C. Even when I suggested C++ as an alternative to C, I did it somehow without noticing that we were doing C. Amazing.
Last edited on
Yeah, really.
Topic archived. No new replies allowed.