Solved

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

Posted on 2004-04-06
12
892 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
  • 3
  • 3
  • 2
  • +3
12 Comments
 
LVL 23

Accepted Solution

by:
chensu earned 250 total points
Comment Utility
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
Comment Utility

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
Comment Utility
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
 
LVL 12

Assisted Solution

by:stefan73
stefan73 earned 50 total points
Comment Utility
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
Comment Utility
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
Comment Utility
dimitry, you need to add also custom free function.
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 11

Expert Comment

by:dimitry
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Also, I've heard the doug lea allocator returns aligned memory.
0
 
LVL 12

Expert Comment

by:stefan73
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now