[patch]: new returns misaligned pointers

Jason Merrill jason@cygnus.com
Tue Nov 30 23:59:00 GMT 1999


>>>>> Oleg Zabluda <zabluda@mindspring.com> writes:

 > In article < 19991112180622Q.mitchell@codesourcery.com > you wrote:
 > :>>>>> "Diego" == Diego Novillo <dnovillo@cygnus.com> writes:

 > :     Diego> I agree, but in this case they were expecting new and
 > :     Diego> malloc to return the same alignment. That's the only reason
 > :     Diego> why we needed to change it.

 > : There's nothing in the standard that says that.  In fact, when the
 > : compiler sees:

 > :   struct S { }; /* Double sized thing full of shorts */
 > :   new S;

 > : it can give back short-aligned memory, even if `malloc' would normally
 > : give back memory with enough alignment for a double.

 > : Any program making the assumption you state is broken, I think.

 > I think you are incorrect. 5.3.4/12 says that 'new S' results in 
 > operator new(sizeof S), which must return pointer
 > properly alighed for any complete type (3.7.3.1/2).

Correct.  But array new is a different case; since array new does not
evaluate to the result of the call to operator now, it isn't automatically
subject to the same constraint; the standard only requires that for new
char[].

10- A new-expression passes the amount of space requested to the allocation
function as the first argument of type std::size_t. That argument shall be
no less than the size of the object being created; it may be greater than
the size of the object being created only if the object is an array. For
arrays of char and unsigned char, the difference between the result of the
new-expression and the address returned by the allocation function shall be
an integral multiple of the most stringent alignment requirement
(basic.types) of any object type whose size is no greater than the size of
the array being created. [Note: Because allocation functions are assumed to
return pointers to storage that is appropriately aligned for objects of any
type, this constraint on array allocation overhead permits the common idiom
of allocating character arrays into which objects of other types will later
be placed. ] 

Jason



More information about the Gcc-patches mailing list