简体   繁体   中英

What is the byte alignment of an allocated block of memory on 64bit platforms?

I have always assumed that malloc returns an allocated block of memory with an address that is 16byte aligned for 64bit systems using both Visual Studio and gcc. However, I've been reading some contradictory articles on the issue. The MSDN site is very clear on this but the GNU documentation is a lot more "nuanced".

Any clarification would be most helpful.

Great answers here:

Aligned memory management?

malloc returns a pointer with fundamental alignment (C11 §7.22.3) which is the alignment of max_align_t (C11 6.2.8). This varies among operating systems, and on Windows it's not good enough for SSE.

There are various options , but if it's for SSE, it looks to me like a good approach would be to call _mm_malloc and _mm_free . If desired, use macros to map those names to appropriate platform-specific facilities such as _aligned_malloc .

The alignment is guaranteed to be suitable for any purpose. Which usually mean 8 byte alignment. However it's something that is subject to change as new compilers come out. Large allocations might also align to page boundaries, because of the way memory allocation algorithms work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM