These variables are allocated in the stack, long variables is better allocated in the heap (whith malloc function) or like global variables (out a function).
Thanks, globalizing those arrays fixes that crash and my real program is running fine now.
None of my C books seem to have explanations of malloc, what kind of book would go into that? Or maybe it would be in the index under something other than allocating or memory?
Simply ‘malloc’ takes one parameter that specifies the ammount of memory to allocate and returns a pointer to the memory (you need to type-cast this). ‘free’ takes a pointer to some malloc’d memory.
eg to allocate and free an array of 100 ints:
// pointer for the memory
int *pIntArray;
// allocate the memory
pIntArray = ( int* ) malloc( sizeof(int) * 100 );
// some code that uses the memory
// Now free the memory
free( pIntArray );
Well Deiussum you’re right, I’m all setup for C++ here. Thanks Anon. I’ve even used the new/delete operators before but I don’t think I’ve ever created multi-dimensional arrays. I’d expect it to work like this:
float* bmpA = new float [200][200];
But that line won’t compile with that second dimension on there.
you MIGHT be better off to “manually” compress your double indirection into a single large vector. the store for bmp described above is NOT contiguous, and it can’t easily be cast to a float* that opengl wants when you’re binding texture.
explaining this further:
float **bmp is a pointer to a pointer to a float. so, bmp points to an ARRAY of pointers. this array is contiguous, but each element in that array points to ANOTHER array, this time of floats. These additional arrays are not necessarily in a single block. ie. in the above example, bmp[1]-bmp[0] is not necessarily 200*sizeof(float).
you might find it more useful to “pack” the image into a vector:
float *bmp=new float[200*200];
bmp[x+y*200]=blah; /* set pixel (x,y) */