
<snip on alignment> Jonathan Wakely wrote:
memory is alignment with the type being allocated. Malloc and New are guaranteed to return memory that is aligned for an object of that size
Guaranteed to be aligned for _any_ type, isn't it ?
Any type that is not bigger than the memory size allocated. I'm pretty sure that the ANSI requirement for malloc and new does not require alignment to 8/16 byte blocks for memory allocations of a single byte (for example). This means that, to be pedantic, a 1 byte allocation is not guaranteed to be aligned for a double (for example). However, since this is a silly example, for all practical purposes you are correct. Also, some implementation may well guarantee alignment for small allocations. Nevertheless, my rather terse comment about being guaranteed to return memory aligned for an object of that size was probably a bit too terse, so thanks for the quotes and clarification. Dave

At 12:38 PM 1/20/2005, Dave Handley wrote:
<snip on alignment>
Jonathan Wakely wrote:
memory is alignment with the type being allocated. Malloc and New are guaranteed to return memory that is aligned for an object of that size
Guaranteed to be aligned for _any_ type, isn't it ?
Any type that is not bigger than the memory size allocated. I'm pretty sure that the ANSI requirement for malloc and new does not require alignment to 8/16 byte blocks for memory allocations of a single byte (for example). This means that, to be pedantic, a 1 byte allocation is not guaranteed to be aligned for a double (for example).
For for malloc, the C++ standard just refers to the C standard, so the C standard is what applies. Here is what it says: "The pointer returned if the allocation succeeds is suitably aligned so that it may be assigned to a pointer to any type of object and then used to access such an object or an array of such objects in the space allocated (until the space is explicitly deallocated)." There is no discussion of the size of the allocation having any impact on the alignment of the returned pointer. --Beman
participants (2)
-
Beman Dawes
-
Dave Handley