Subject: [boost] Bug with gcc and array
From: Andreas Klein (klein_at_[hidden])
Date: 2008-11-24 08:01:21


Dear developers.

I send this both to the boost and the gcc developers.

I have found a strange limitation for the size of an array. The error
is the same both for boost 1.36 and for the array in gcc 4.3.2 (and in
the gcc 4.4 snapshot I use). I am almost sure that it is a bug of gcc
and not of the array implementation.

Here is the code.

#include<array>
using namespace std;

int main () {
   array<unsigned int, (1<<22)> a; // Create a big array

   a[(1<<22)-13]=0; // This give a segmantation fault
}

In contrast a C-array works fine.

int main () {
   unsigned int a[1<<22];

   a[(1<<22)-13]=0;
}

Since the naturale implementation of the array class is just a wrapper
around the C-style array this is very strange.

I tryed to track down the bug. You use the following implementation:

   template<typename T, unsigned int n> class array {
     T C[n];

   public:
     typedef unsigned int size_type;
     T& operator[](size_type p) {
       return C[p];
     }
   };

If we change the C[n] to an explicite new it works fine.

   template<typename T, unsigned int n> class array {
     T* C;

   public:
     typedef unsigned int size_type;
     array() {
       C = new T[n];
     }
     T& operator[](size_type p) {
       return C[p];
     }
   };

It seams that this resolves the bug. But I can not explain why.
I tested the code with the intel compiler and it works fine for both
implementations. So it seams that it is a gcc bug, that the first
solution do not work. What happens here?

Kind regards
      Andreas Klein