K. Jennings said:
I think I have the info that I wanted. If I understand it
correctly, it makes no difference whether you do
u64 *p = malloc(N) ;
or
char *p = malloc(N) ;
the address returned, when non-NULL, will be aligned on a boundary equal
to the largest basic data size supported. Thus, for 32-bit architectures
it might be either 4 or 8 bytes, whereas for 64-bit ones it would be 8.
Or am I missing something?
All that's guaranteed is that it's _properly_aligned_ for any type.
This does not necessarily mean that it is, using your example,
aligned to sizeof(longest_type). You may have a 64-bit "long long"
type, yet still get 4-byte (ie: 32-bit) alignment. The alignment
may also be larger than the minimum needed by the hardware. For
example, my system returns 8-byte alignment, even though the
hardware itself would be happy with 2-byte alignment (and happier
with 4-byte alignment). This is simply because the implementation
decided to do so for its own convenience, as it allows it to use
the low-order 3 bits of pointers within the heap for its own use.
Also, way back in MS-DOS days, I used an implementation which
returned 16-byte alignment, even though 1-byte alignment would
have sufficed. This allowed malloc's "far pointers" to always
have a zero offset, as I recall. (Another great reason for
making sure you include the correct headers, as without the
proper return type declared, the assumption of "int" would have
meant you would always see a zero return.)
As others have stated, there is no portable way to know what the
alignment is. Nor is there likely any need to know, as all you
really should care about is that the alignment is valid. (Unless
you are writing your own malloc, and you need to know that you
are implementing it correctly.)
Why do you think you need to determine malloc's alignment?
--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody |
www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net |
www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:
[email protected]>