Community
cancel
Showing results for 
Search instead for 
Did you mean: 
296 Views

Did "malloc" allocated noaligned memory?

Jump to solution

Hello,

I have a question about alignment data. I tested "malloc" and saw that memory aligned on 16-byte. I retried this experiment, but result is same. 

Did "malloc" allocated noaligned memory? Or "malloc" allocated aligned memory on 16 byte? Options of compiler is standard.

Thanks for a answer.

0 Kudos
1 Solution
jimdempseyatthecove
Black Belt
296 Views

malloc has no requirement for alignment. However, most C runtime systems introduce an alignment that is an artifact of its heap managers node management. Most heap managers have at least a {Node* Next, intptr_t lenOfNode} header for nodes. This would place a minimum node size of 2*sizeof(void*). This often is 8 on 32-bit systems, and 16 on 64-bit systems. Heap managers, additionaly may introduce additional information either before or after the returned buffer. An example is when a C++ new[] allocates an array of objects. If your program requires alignment (e.g. for SSE/AVX) then use one of the aligned allocation routines available to your compiler such as _aligned_malloc, and you can also overload new, and new[] to provide alignment as well.

Jim Dempsey

View solution in original post

8 Replies
jimdempseyatthecove
Black Belt
297 Views

malloc has no requirement for alignment. However, most C runtime systems introduce an alignment that is an artifact of its heap managers node management. Most heap managers have at least a {Node* Next, intptr_t lenOfNode} header for nodes. This would place a minimum node size of 2*sizeof(void*). This often is 8 on 32-bit systems, and 16 on 64-bit systems. Heap managers, additionaly may introduce additional information either before or after the returned buffer. An example is when a C++ new[] allocates an array of objects. If your program requires alignment (e.g. for SSE/AVX) then use one of the aligned allocation routines available to your compiler such as _aligned_malloc, and you can also overload new, and new[] to provide alignment as well.

Jim Dempsey

View solution in original post

296 Views

Thanks Jim for the detailed answer!

Marián__VooDooMan__M
New Contributor II
296 Views

On Linux, AFAIK malloc() returns pointer aligned to 16 bytes-boundary. On Windows not. On Windows you need to call _mm_malloc and _mm_free counterpart. see http://software.intel.com/sites/products/documentation/studio/composer/en-us/2011Update/compiler_c/i...

SergeyKostrov
Valued Contributor II
296 Views
>>...Or "malloc" allocated aligned memory on 16 byte?... You need to verify it with a C++ compiler and I see that you've done it. Intel, or Microsoft, or MinGW C++ compilers could have some differences. Note: A couple of months ago I "switched" almost all memory allocations to _mm_malloc and _mm_free intrinsic functions because they are more flexible ( in my cases ). However, there are some issues with C++ operators new and delete since they could use malloc and free CRT functions.
jimdempseyatthecove
Black Belt
296 Views

I should add something to my prior comment. When using new[], my prior observaton was the heap manager managed nodes in multiple of 2x sizeof(void*) as described before. Now then although the raw node aligned on 8 (32-bit) or 16 (64-bit), the entry for the number of objects went into the first element (of size_t) in the node, then the returned memory pointer was at node+sizeof(size_t). Which almost always led to unaligned allocations for use with SSE. Most C memory managers have this fixed now, but you cannot rely on the alignment. Use _mm_malloc/_mm_free or alternate methods to enforce alignment for allocated objects (if this is a requirement).

Jim Dempsey

QIAOMIN_Q_
New Contributor I
296 Views

The_mm_malloc routine takes an extra parameter, which is the alignment constraint. This constraint must be a power of two. The pointer that is returned from_mm_malloc is guaranteed to be aligned on the specified boundary.

Note :Memory that is allocated using_mm_malloc must be freed using_mm_free. Calling freeon memory allocated with_mm_mallocor calling_mm_freeon memory allocated with malloc will cause unpredictable behavior.

 

Thank you. -- QIAOMIN.Q

Intel Developer Support

SergeyKostrov
Valued Contributor II
296 Views
>>...Calling freeon memory allocated with_mm_mallocor calling_mm_freeon memory allocated with malloc will cause >>unpredictable behavior... In practice, it is very predictable and in that case an application always crashes. If the application is compiled with Microsoft or Intel C++ compilers in Debug configuration an Assert message informs a developer that inconsistency with calls detected. I had some number of crashes during an initial phase of changing all calls to always alligned memory allocations using _mm_malloc and _mm_free intrinsic functions.
Kumar__Siva
Beginner
296 Views

I realize that this is an old thread, however, . . .

My understanding of malloc is that it must always return an address which meets the strictest alignment requirement for the architecture on which it's running.
 
For example, if a double (8 bytes) was the basic data type that has an alignment requirement of 8 bytes, then all malloced addresses would meet this criteria (addr % 8) == 0
 
I believe that this is to satisfy the aligment requirement of malloced data which is then cast to another data type.
For example:
double *dptr = malloc(sizeof(double));
 
From this:
<SNIP>
 
The order and contiguity of storage allocated by successive calls to malloc() is unspecified. The pointer returned if the allocation succeeds shall be suitably aligned so that it may be assigned to a pointer to any type of object and then used to access such an object in the space allocated (until the space is explicitly freed or reallocated)
<SNIP>
Reply