posix_memalign() and memalign() weirdness

Hi all,

I am trying to allocate an aligned buffer on a 4 byte boundary. I’m calling
posix_memalign() and it isn’t allocating the memory I’ve requested.
Instead, it doesn’t fill in the buffer I passed (leaves it NULL) and has a
return value of 16. The docs indicate it is supposed to return a -1 or a 0,
so I’m not sure what this means.

So I tried to use memalign(). I pass it a 4 and a 512 (for alignment and
size). It returns 0 and errno is set to 22 (EINVAL) The docs say memalign
returns this when alignment isn’t a multiple of sizeof( void *). Well, I’m
confused 'cause I thought 4 was a mulitple of 4? Am I missing something
obvious here?

Could I be linking against incorrect libraries or something?

Thanks
Kevin

Ok, so I figured out what the 16 meant that was being returned by
posix_memalgin() I was using %x in the printf statement so that 16 is
really 0x16 or 22, thus it is probably EINVAL.

The docs should be changed to indicate posix_memalign() will return the
actual error code, errno is not set.

However, it has the same problems as the ANSI memalign() in that I can’t use
sizeof( void *) as the alignment. Thus if the compiler is defaulting to 8
byte alignment I can’t specify 4.

Is this expected behavior?

Kevin

“Kevin Stallard” <kevin@ffflyingrobots.com> wrote in message
news:af8ljf$72p$1@inn.qnx.com

Hi all,

I am trying to allocate an aligned buffer on a 4 byte boundary. I’m
calling
posix_memalign() and it isn’t allocating the memory I’ve requested.
Instead, it doesn’t fill in the buffer I passed (leaves it NULL) and has a
return value of 16. The docs indicate it is supposed to return a -1 or a
0,
so I’m not sure what this means.

So I tried to use memalign(). I pass it a 4 and a 512 (for alignment and
size). It returns 0 and errno is set to 22 (EINVAL) The docs say
memalign
returns this when alignment isn’t a multiple of sizeof( void *). Well,
I’m
confused 'cause I thought 4 was a mulitple of 4? Am I missing something
obvious here?

Could I be linking against incorrect libraries or something?

Thanks
Kevin