Does malloc give you memory aligned on a 32 bit boundary?

Fairly easy to test with a loop and if(memptr % 4 == 0) still I'm lazy, and I'll bet most of the experts here already know the answer to that.

And if malloc doesn't do that, wouldn't it make sense to use a memory allocator that does?

Who is Participating?
chensuConnect With a Mentor Commented:
It depends on the underlying OS heap implementation. In Windows NT version 4.0 and later, it is aligned on a 8-byte boundary. For more information, check out

Heap: Pleasures and Pains
Sys_ProgConnect With a Mentor Commented:

As checnsu said, It depends on the underlying OS and the machine

Have a look at these links as well

AlexFMConnect With a Mentor Commented:
Visual Studio 7.1 has new allocation functions which allows to set desired alignment. See _aligned_malloc, _aligned_free. Variables allocated on stack may be aligned using __declspec(align(...)) statement. These features are used when we need to get alignment more than default.
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

stefan73Connect With a Mentor Commented:
Hi Sandra-24,
malloc() is not required to align, but as many OSes return aligned memory anyway, it's usually aligned.

If you want to be absolutely sure that you have a particular alignment, use the memalign() function:

#include <stdlib.h>
void *memalign(size_t alignment, size_t size);

malloc() normally returns memory aligned in a way that the largest atomic data type won't be misaligned. Intel machines just have a timing penalty for misaligned data, whereas M68K and many RISCs (such as SPARC) will trap, causing a core dump.

dimitryConnect With a Mentor Commented:
As all experts told, it depends on implementation.
If you want to write portable code where alignment is important, then the simplest way is to use
your own 'malloc' that is doing something like this:
void *mymalloc( size_t size )
  void *myPtr = (void *)malloc( size + 4 );

  if( (myPtr != NULL) && (((unsigned)myPtr & 0x3) != 0) )
    return( (void *)((char *)myPtr + (4 - (unsigned)myPtr & 0x3)) );
  return( myPtr );
dimitry, you need to add also custom free function.
I am sorry. You are right, thank you.
The simplest solution will be to align the pointer that is received from usual normal with bigger size.
Then there is no need of custom free().
> The simplest solution will be to align the pointer that is received
> from usual normal with bigger size.
> Then there is no need of custom free().

You'll end up carrying around two pointers, one aligned and one the original needed for free().

memalign doesn't work?

Stefan, I don't understand your frustration I feel from your comment...
We are trying to help and trying to exchange our experience.
memalign() will work, and if it is portable function that can be used not only under Unix (and maybe Linux)
and it is good for Sandra-24, let her choose it.
Sandra-24Author Commented:
Stephan is correct, you would need to carry around two pointers that way, you'd need the original so you could free the memory later.

You could provide your own new and delete operators to hide the fact that you have two pointers.

I use a fixed size memory pool in front of malloc in order to speed up some routines.
It might be worth it to use memalign() on those blocks, since it only needs to be done once.

malloc uses HeapAlloc and VirtualAlloc though so I think, atleast on later windows versions, that it will return aligned memory.

Thanks for your input,

Sandra-24Author Commented:
Also, I've heard the doug lea allocator returns aligned memory.
Yes - you  can define arbitrary (well, power-of-two-arbitrary) alignments when you redefine MALLOC_ALIGNMENT in dlmalloc. By default, alignment is 8.

But dlmalloc is pretty good, it has very little overhead.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.