RFC: PR c++/36159: C++ compiler should issue a warning with missing new operator

Richard Guenther richard.guenther@gmail.com
Wed Jan 21 20:07:00 GMT 2009


On Wed, Jan 21, 2009 at 8:27 PM, H.J. Lu <hjl.tools@gmail.com> wrote:
> Hi,
>
> Alignment returned default new operator for vector files aren't
> sufficient. This patch issues an error when it happens:
>
>
> [hjl@gnu-6 pr36159]$ cat pr36159-2.cc
> #include <stdio.h>
> #include <emmintrin.h>
>
> struct A {
> public:
>       __m128i m;
>       void init() { m = _mm_setzero_si128(); }
> };
>
> int main()
> {
>       A * a = new A;
>       printf("Address of A: %p\n", a);
>       a->init();
>       delete a;
>       return 0;
> }
> [hjl@gnu-6 pr36159]$ make pr36159-2.s
> /export/build/gnu/gcc-avx/build-x86_64-linux/gcc/xgcc
> -B/export/build/gnu/gcc-avx/build-x86_64-linux/gcc/ -O2 -msse3 -m32
> pr36159-2.cc -S -o pr36159-2.s
> pr36159-2.cc: In function 'int main()':
> pr36159-2.cc:12: error: global 'operator new' can't be used on type
> 'A' due to insufficient alignment
> make: *** [pr36159-2.s] Error 1
> [hjl@gnu-6 pr36159]$ cat pr36159-1.cc
> #include <stdio.h>
> #include <stdlib.h>
> #include <emmintrin.h>
>
> struct A
> {
> public:
>    __m128i m;
>    void init() { m = _mm_setzero_si128(); }
>    void *operator new (size_t s) throw()
>      {
>        void *p;
>        int ret = posix_memalign (&p, __alignof__ (__m128i), s);
>
>        if (ret)
>          return NULL;
>        return p;
>      }
> };
>
> int main()
> {
>       A * a = new A;
>       printf("Address of A: %p\n", a);
>       a->init();
>       delete a;
>       return 0;
> }
> [hjl@gnu-6 pr36159]$ make pr36159-1
> /export/build/gnu/gcc-avx/build-x86_64-linux/gcc/xgcc
> -B/export/build/gnu/gcc-avx/build-x86_64-linux/gcc/ -O2 -msse3 -m32
> pr36159-1.cc -c -o pr36159-1.o
> g++ -m32 -o pr36159-1 pr36159-1.o
> [hjl@gnu-6 pr36159]$ ./pr36159-1
> Address of A: 0x8620020
> [hjl@gnu-6 pr36159]$
>
> Any comments?

As this is a perfectly valid program you cannot issue an error here.  If you are
clever enough to detect the problem, why not emit a call to posix_memalign
and do the construction in-place instead?

Richard.

> Thanks.
>
>
> --
> H.J.
> ---
> gcc/
>
> 2009-01-21  H.J. Lu  <hongjiu.lu@intel.com>
>
>        PR c++/36159
>        * config/i386/linux.h (MALLOC_ABI_ALIGNMENT): New.
>        * config/i386/linux64.h (MALLOC_ABI_ALIGNMENT): Likewise.
>
> gcc/cp/
>
> 2009-01-21  H.J. Lu  <hongjiu.lu@intel.com>
>
>        PR c++/36159
>        * init.c (build_new_1): Don't allow global operator new on
>        types whose alignments are > MALLOC_ABI_ALIGNMENT.
>



More information about the Gcc-patches mailing list