So it seems I don't know how to use delete properly in C++. I have a "Matrix" class that dynamically allocates memory for a 2D array of floats, and I'm creating and destroying tonnes of these as the application runs. So I put this in the destructor.
for(GLuint i = 0 ; i < height ; i++)
delete matrix[i];
It crashes instantly.
Here is the matrix variable:
GLfloat **matrix;
And here is the code setting up the matrix:
matrix = new GLfloat*[height];
for(GLuint i = 0 ; i < height ; i++){
matrix[i] = new GLfloat[width];
for(GLuint j = 0 ; j < width ; j++){
matrix[i][j] = (i == j && isIdentity ? 1 : 0 );
}
}
Wtf is my stupid ass doing wrong?
Name:
Anonymous2008-10-22 15:50
OK, first things first. Don't use OpenGL. It is shite.
Name:
Anonymous2008-10-22 15:52
Also you might as well keep your matrix as an array of 16 floats because that is how OpenGL does it.
Name:
Anonymous2008-10-22 15:53
It's shite NOW. ._.
Name:
Anonymous2008-10-22 15:54
I actually don't need more than 4x4 for this app, I just wanted to see if I could make a nice Matrix class and use it. So if it comes to that, yeah okay I'll make it 16x16 solid.
Name:
Anonymous2008-10-22 16:08
What?
I mean OpenGL represents its 4x4 matrices at a GLfloat array of 16 values.
Name:
Anonymous2008-10-22 16:11
Oh.. Heh. That did seem odd. :P
Well, regardless, this shit still crashes. So unless there's somethin immediately wrong with that code up there, I'm just gonna retreat back to no-dynamic town.
>>1
To answer your question, when you allocate with operator new[] you have to free with delete[]. It's one of the many esoteric pieces of shit that make Sepples a terrible language.
for(GLuint i = 0 ; i < height ; i++)
delete[] matrix[i];
delete[] matrix;
Also, writing your own matrix operation is silly, especially if you're not SSE- optimizing them.
Name:
Anonymous2008-10-22 16:22
ONLY PUSSIES FREE MEMORY
THE OS FREES IT FASTER THAN YOU EVER CAN WHEN THE PROGRAM IS TERMINATED
JUST EXECVE A NEW INSTANCE IF YOU HAVE TO START FROM THE BEGINNING
ENJOY YOUR WASTED CPU CYCLES, I KNOW I WILL ENJOY THE EXTRA HEAT YOU GENERATE
>>10
Attach a debugger and find out why it crashes then.
Name:
Anonymous2008-10-22 16:32
>>10
1) Don't use quotes when you say Sepples
2) Yes, the farther you go the more you hate Sepples.
Name:
Anonymous2008-10-22 18:37
>>10
1) Programs always act the way the programmer told them to do, they don't have any ``mistake'' in THEM.
2) Don't learn OpenGL before knowing the basis
3) Doing something like new GLFloat[width][height]; is allowed in Sepples
4) I hope you realize you're assigning a boolean to a GLFloat without performing any typecast
5) See >>8,9
AND FUQIN LASTLY isIdentity ? 1 : 0
THIS IS THE MOST USELESS PIECE OF CODE EVER WRITTEN
IHBT
Name:
Anonymous2008-10-22 19:10
>>13 here
I forgot to tell you I didn't mention anything about segfaults because if you've done what >>8 told you, there shouldn't be any, learn to debug the rest of your code by yourself now(start by finding out where it crashes)