Any ideas why this is giving a segmentation fault?:

#include <iostream>
#include <new>

using namespace std;

struct List {

int A;
int B;
List *next;


int main(void) {

int llength;


List *L;

L= new List[llength];


delete [] L;

return 0;




More seriously, if you're running this program on a 32-bit machine, you're probably asking for much more memory than the system is capable of providing; and it would not be surprising to learn that an overflow is taking place as part of computing how much memory you need.

I thought it should crash out with a bad_alloc exception though?

In a perfect world, it would. In practice, it is not uncommon for implementations to fail to catch integer overflow.

So, am I right in saying that if the g++ compiler had the correct implementation, then the above would through a bad_alloc exception instead of a segmentation fault? (and if so, is this something which is likely to be fixed on future releases of the g++ compiler?)