Bad_alloc on array allocation

I have this code in the class constructor:

verts=new GLfloat[nVerts];

verts is a member variable of the class (GLfloat *), and nVerts is 4. Is there something wrong with this code? I get the same error. If I try to do malloc (sizeof (GLfloat) * nVerts), I think I do not have enough memory, because if I change this line of code to another part of the code (for example, to a method that calls the constructor just before it is called, here like this: GLfloat *test=new GLfloat[4]it works)

+3
source share
1 answer

I would check 3 things:

  • Are you 100% sure what nVertsis 4?
  • Isn't memory exhausted?
  • Isn't your heap damaged? valgrind can help you with this.
+4
source

All Articles