Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 959
  • Last Modified:

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?

Thanks,
-Sandra
0
Sandra-24
Asked:
Sandra-24
  • 3
  • 3
  • 2
  • +3
5 Solutions
 
chensuCommented:
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
http://msdn.microsoft.com/library/en-us/dngenlib/html/heap3.asp?frame=true
0
 
Sys_ProgCommented:

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

Have a look at these links as well

http://www.fokus.gmd.de/gnu/docs/glibc/libc_28.html
http://www.unet.univie.ac.at/aix/libs/basetrf1/malloc.htm
http://www.delorie.com/gnu/docs/glibc/libc_31.html


Amit
0
 
AlexFMCommented:
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.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
stefan73Commented:
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.

Cheers,
Stefan
0
 
dimitryCommented:
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 );
}
0
 
AlexFMCommented:
dimitry, you need to add also custom free function.
0
 
dimitryCommented:
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().
0
 
stefan73Commented:
dimitry,
> 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
0
 
dimitryCommented:
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.
0
 
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

0
 
Sandra-24Author Commented:
Also, I've heard the doug lea allocator returns aligned memory.
0
 
stefan73Commented:
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.
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 3
  • 3
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now