c++syntaxsegmentation-fault

Why does g++ compile this?


Recently, after being very tired, I wrote the following code:

GLfloat* array = new GLfloat(x * y * z);

Which, of course should have been:

GLfloat* array = new GLfloat[x * y * z];

(Note the square brackets as opposed to the parenthesis.)

As far as I know, the first form is not valid, but g++ compiled it. Sure, it spat out a completely incomprehensible segfault, but it compiled.

Why?


Solution

  • GLfloat* array = new GLfloat(x * y * z);
    

    Creates a pointer called array to an object of type GLfloat with a value of x * y * z.