Topic: alignment of new []
Author: rado42@my-dejanews.com
Date: 1998/11/05 Raw View
In article <71cvom$hfv$1@engnews1.eng.sun.com>,
clamage@Eng.Sun.COM (Steve Clamage) wrote:
....stuff.......
> Recall that a new-expression results in a call on an allocation
> function, followed by construction of objects. The compiler might
> generate other bookkeeping code as well. For example, if an
> array-delete expression does not have compile-time access to the
> number of elements allocated, that information must be stored
> someplace at run time by the array-new expression.
>
> The quoted paragraph recognizes that the compiler might want to
> request some extra space beyond what is needed for the array
> being allocated. If so, the compiler-generated code must preserve
> alignment.
>
> Allocation functions (such as operator new or operator new[]) are
> unconditionally constrained to return storage properly aligned for
> any object that could be stored in the requested memory. Your
> operator new[] above does not meet this requirement.
>
.....stuff.....
Hi,all
Does all that mean that in the following code (assuming 8 byte allignment,
which also happens to be the sizeof double)
double *x = new double[1];
int ix = (int)x;
assert (x % 8 == 0);
'assert()' should not fail? It *does* under MSVC5.0. Is this a library bug?
Rado
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: fjh@cs.mu.OZ.AU (Fergus Henderson)
Date: 1998/11/05 Raw View
rado42@my-dejanews.com writes:
>Does all that mean that in the following code (assuming 8 byte allignment,
>which also happens to be the sizeof double)
>
> double *x = new double[1];
> int ix = (int)x;
> assert (x % 8 == 0);
>
>'assert()' should not fail? It *does* under MSVC5.0. Is this a library bug?
That code is ill-formed; a diagnostic is required.
I presume you meant `assert (ix % 8 == 0)'.
^^
But even then, there is no guarantee that the assertion will succeed.
Firstly the semantics of `(int)x' are implementation-defined.
That's probably not the reason why it fails, since most implementations,
doubtless including MSVC5.0, define it in the obvious way.
But in addition, the alignment required for `double' is
implementation-defined. On the x86 architecture, misaligned doubles
just result on slower execution, not a trap, so an implementation is
perfectly free to say that 8-byte doubles need only be aligned
on 4-byte boundaries.
--
Fergus Henderson <fjh@cs.mu.oz.au> | "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh> | of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3 | -- the last words of T. S. Garp.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1998/11/05 Raw View
rado42@my-dejanews.com writes:
>Does all that mean that in the following code (assuming 8 byte allignment,
>which also happens to be the sizeof double)
> double *x = new double[1];
> int ix = (int)x;
> assert (x % 8 == 0);
>'assert()' should not fail? It *does* under MSVC5.0. Is this a library bug?
Why do you assume 8-byte alignment? The storage you get must be
suitably aligned, but "suitably" is defined by the implementation.
Some platforms require 8-byte alignment for doubles but many do not.
If the implemenation documents that 8-byte alignment is required
for type double, then the assert should pass. Otherwise it need not.
--
Steve Clamage, stephen.clamage@sun.com
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: lisa_lippincott@advisories.com (Lisa Lippincott)
Date: 1998/11/05 Raw View
In article <71rm5b$c05$1@nnrp1.dejanews.com>, rado42@my-dejanews.com wrote:
> Does all that mean that in the following code (assuming 8 byte allignment,
> which also happens to be the sizeof double)
>
> double *x = new double[1];
> int ix = (int)x;
> assert (x % 8 == 0);
>
> 'assert()' should not fail? It *does* under MSVC5.0. Is this a library bug?
I've run into this one. While the MSVC compiler, by default, aligns
doubles to 8-byte boundaries, the allocation routines in the library
only provide 4-byte alignment. Since everything works on 4-byte
boundaries, I believe this ridiculous behavior conforms to the standard.
On the other hand, it does confound the usual alignment tricks one
needs when writing operators new, and provides an excellent argument
for adding language features which deal with alignment and uninitialized
memory.
--Lisa Lippincott
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]