Solved

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

Posted on 2004-04-06
12
940 Views
Last Modified: 2007-12-19
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
Comment
Question by:Sandra-24
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +3
12 Comments
 
LVL 23

Accepted Solution

by:
chensu earned 250 total points
ID: 10770486
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
 
LVL 10

Assisted Solution

by:Sys_Prog
Sys_Prog earned 100 total points
ID: 10771766

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
 
LVL 48

Assisted Solution

by:AlexFM
AlexFM earned 50 total points
ID: 10771960
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
Technology Partners: 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!

 
LVL 12

Assisted Solution

by:stefan73
stefan73 earned 50 total points
ID: 10773121
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
 
LVL 11

Assisted Solution

by:dimitry
dimitry earned 50 total points
ID: 10781268
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
 
LVL 48

Expert Comment

by:AlexFM
ID: 10781369
dimitry, you need to add also custom free function.
0
 
LVL 11

Expert Comment

by:dimitry
ID: 10781423
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
 
LVL 12

Expert Comment

by:stefan73
ID: 10782118
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
 
LVL 11

Expert Comment

by:dimitry
ID: 10784581
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
 
LVL 3

Author Comment

by:Sandra-24
ID: 10811322
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
 
LVL 3

Author Comment

by:Sandra-24
ID: 10811337
Also, I've heard the doug lea allocator returns aligned memory.
0
 
LVL 12

Expert Comment

by:stefan73
ID: 10811902
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

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question