PDA

View Full Version : What about the free store? :)



BlueJ
05-08-2001, 01:43 PM
I hope this isn't too off topic...

void main()
{
float bitmapA[200][200][32/8];
float bitmapB[200][200][32/8];
}

This program crashes unless the second line is commented out. So I guess I need to allocate the memory somehow before I use it, but how?

Boresight
05-08-2001, 01:53 PM
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).

BlueJ
05-08-2001, 02:08 PM
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?

Deiussum
05-08-2001, 02:11 PM
Are your books really C books or could they be C++ books? malloc/free is basically the C equivalent to the C++ new/delete operators.

05-08-2001, 03:38 PM
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:
[CODE]
// 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 );

BlueJ
05-08-2001, 05:18 PM
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.

john
05-08-2001, 05:30 PM
float* bmpA = new float [200][200];

[][] is really double in-direction, ie. a pointer to a pointer (to a value), so at the very least you'd need float **bmpA. But that won't compile, either.

What you'll need to do is allocate the array of pointers, and THEN the data:

float **bmp;

bmp=new float*[200];
for(int t=0; t<200; t++)
bmp[t]=new float[200];

ie. you're allocating one reference at a time.

hope this helps,

cheers,
John

john
05-08-2001, 05:36 PM
Another thing:

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) */

cheers,
John

05-08-2001, 05:46 PM
that is correct. In face bmp[0]-bmp[1] will be sizeof(float *)

05-08-2001, 05:47 PM
er bmp[1]-bmp[0] will be sizeof(float *)

05-08-2001, 05:55 PM
er again

rather the difference between the
adress of bmp[1] (a float pointer)
and the address of bmp[0] (also a float pointer) will be sizeof(float *)

john
05-08-2001, 06:42 PM
well, yes. that IS true for C, but my point was that some systems do magiclaly pack double dimension arrays so into row or column major order, so

m[5][5]=
ABCDEabcdeABCDEabcdeABCDE

so in THISE case you can compute m[1]-m[0]==5*sizeof(whatever) (for column major storage). that was my point: all the data is in a **contigious** block.

er, what i meant before was *bmp[1]-*bmp[0] not being 200*sizeof. oops =)

cheers,
John


[This message has been edited by john (edited 05-08-2001).]