Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

aligned_alloc function requirements

I'm looking at the explanation of aligned-alloc(): http://en.cppreference.com/w/c/memory/aligned_alloc

void *aligned_alloc( size_t alignment, size_t size );

"Allocate size bytes of uninitialized storage whose alignment is specified by alignment. The size parameter must be an integral multiple of alignment."

However, the example code uses it like this:

int *p2 = aligned_alloc(1024, 10*sizeof *p2);

10*sizeof *p equals 40, so it's not an integral multiple of 1024.

What do I misunderstand?

like image 980
MichaelSB Avatar asked Dec 08 '16 03:12

MichaelSB


1 Answers

Actually, it seems like C11 standard cannot make up its own mind, and keeps changing the requirements: initially it was an undefined behavior, then with DR 460 they changed it to fail and return null pointer, and now it seems like they want to change it to accept any values as arguments: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2072.htm

And compilers seem to not care about those restrictions.

like image 178
MichaelSB Avatar answered Sep 22 '22 20:09

MichaelSB