- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks Jim for the detailed answer!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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/intref_cls/common/intref_allocate_free_aligned_mem.htm
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I realize that this is an old thread, however, . . .

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page