-1

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?

Community
  • 1
  • 1
cdcdcd
  • 547
  • 1
  • 5
  • 15
  • 1
    Does it help? https://www.gnu.org/software/libc/manual/html_mono/libc.html#Aligned-Memory-Blocks – Milack27 Feb 10 '17 at 11:24
  • 1
    gcc does not provide the standard library. You should read the documentation of `glibc`. But the actual question is: why do you care at all? `malloc` guarantees the block returns is properly aligned for any datatype of your platform. If that is not sufficient, either `malloc` alone is not suitable, or you should reconsider your design. And: Which documentation did you read? What **specifically** is not clear? What do you mean with "a lot more nuanced"? – too honest for this site Feb 10 '17 at 11:33
  • @Olaf We have 64-byte (512-bit) vector types now, so "aligned for any datatype of your platform" is wildly optimistic. Microsoft is unique in keeping `malloc` unaligned. – Potatoswatter Feb 10 '17 at 11:39
  • @Potatoswatter: 1) That does not mean they have to be aligned at 512 bit boundaries to be usable. 2) That's what "`malloc` alone is not suitable" implies. `malloc` is for the standard types, it does not cover implementation-specific type or blocks optimised for cache-utilisation. 3) You can over-allocate with `malloc` and manually align (yes, that would be implementation-specific), or use the standard `aligned_alloc` function which is exactly for such scenarios! – too honest for this site Feb 10 '17 at 11:42
  • Alignment to 8 is pretty universal afaik, including Potato's favorite target. About any CRT has a non-standard helper function to get a better one, look for something resembling aligned_alloc() or _aligned_malloc() or posix_memalign(). – Hans Passant Feb 10 '17 at 11:44
  • `malloc`will align with whatever its developer thought (and will think, in future library versions) to be a "reasonable" alignment. The *minimum* alignment is enforced by the C standard to be "proper alignment for all data types" (regardless of performance), but the *optimum* might be determined by the compiler, OS, CPU and other criteria. So knowing this alignment is actually useless knowledge. – tofro Feb 10 '17 at 11:45
  • 2
    @HansPassant: `aligned_alloc` is standard C; no need to use non-standard or restrict to POSIX. – too honest for this site Feb 10 '17 at 11:46
  • @Olaf what on earth are you talking about? No one said that gcc provides the ANSI standard. How each compiler deals with the parameters before allocation is compiler specific - the implementation of malloc in terms of each platform is not specified by the C standard either. For example one implementation may force one alignment for one platform while one for another. I've implemented my own alignment function using malloc. This is standard fare I just wanted to know if this was necessary. You seem confused I hope that clears things up for you. – cdcdcd Feb 10 '17 at 20:02
  • @cdcdcd: Did you even read what `aligned_alloc` is, what gcc provides and what part glibc contributes? I did not even talk about parameters, how are those related here at all? FYI: gcc is actually a freestanding implementation of the C standard. It becomes a hosted one with a correcponding library, e.g. glibc. Re. alignment: read the standard! It is very clear about `malloc`; it very well specifies the behaviour! And that's all I will talk to you. Getting ad hominem is not a way to start an argument. – too honest for this site Feb 10 '17 at 20:08

2 Answers2

1

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.

Community
  • 1
  • 1
Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
  • Thanks for the downvote. What part of this is false? – Potatoswatter Feb 10 '17 at 11:50
  • Why not use the standard function `aligned_alloc` instead of relying on platform details? And fundamental alignment does not mean guarantee `max_align_t`. This is just the minimum guaranteed alignment. – too honest for this site Feb 10 '17 at 11:50
  • @Olaf Because it's C11 and not all platform vendors are on-board with that. – Potatoswatter Feb 10 '17 at 11:51
  • Ehm, you cite C11 and this is the only standard. Not to forget that after 6 years (plus some years of development) modern platforms should support it now. Anyway, it would be the first approach, use the others as fallback on broken platforms, e.g. using a macro-wrapper with `#ifdef`s. – too honest for this site Feb 10 '17 at 11:54
  • 1
    @Olaf I happen to be more reasonable than Microsoft. – Potatoswatter Feb 10 '17 at 11:54
  • Potato I agree things just get down voted here because of stupid pedantic reasons. You qualified your point. In the end I just implemented a method similar to the top answer here: http://stackoverflow.com/questions/5061392/aligned-memory-management – cdcdcd Feb 10 '17 at 14:06
0

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.

Malcolm McLean
  • 6,258
  • 1
  • 17
  • 18
  • This is what I mean by nuanced. Every experience I have had with 64bit machines using gcc and VS has been 16byte aligned for double word or qaud word types. – cdcdcd Feb 10 '17 at 14:29