简体   繁体   中英

bad_alloc when allocating array

I have this code on the constructor of a class:

verts=new GLfloat[nVerts];

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

I'd check 3 things:

  1. Are you 100% sure that nVerts is 4?
  2. Isn't memory really exhausted?
  3. Isn't your heap corrupted? valgrind can help you with that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM