Can't seem to see the forest for the trees

Started by
44 comments, last by Indogo 2000 15 years, 8 months ago
SEEMS to be allocating just fine. Filling rbuf with values kills it. also crashes on DeleteRenderBuffers() I'm not a newb but this FAIRLY standard buffer implementation is driving me buggy. what very simple thing have missed? #include <stdlib.h> #include <stdio.h> #include <windows.h> unsigned char ***rbuf; // to become rbuf[scrX][scrY][3] bool RBufsActive=FALSE; // to make sure we don't call one twice int scrX, scrY; // for the dynamic array int AllocRenderBuffers( void ) { int x, y; // we've been here without calling DeleteRenderBuffers() first if(RBufsActive==TRUE) return 0; // Allocate rbuf[x][y][3], the main render buffer if((rbuf=(unsigned char***)malloc(scrX*sizeof(unsigned char)))==NULL) return 0; for( x=0; x<scrX; x++) if((rbuf[x]=(unsigned char**)malloc(scrY*sizeof(unsigned char)))==NULL) return 0; for( y=0; y<scrY; y++) for( x=0; x<scrX; x++ ) if((rbuf[x][y] = (unsigned char*)malloc(3*sizeof(unsigned char)))==NULL) return 0; RBufsActive=TRUE; return 1; } int DeleteRenderBuffers( void ) { int x, y; if(RBufsActive==FALSE) return 0; // deallocate from the outside in: for( y=0; y<scrY; y++) for( x=0; x<scrX; x++ ) free( rbuf[x][y] ); for( x=0; x<scrX; x++) free( rbuf[x] ); free( rbuf ); RBufsActive=FALSE; return 1; } int main(void) { int x, y, c, ch; ch=0; scrX = 640; scrY = 480; if(!AllocRenderBuffers()) return(0); for(x=0; x<scrX; x++) for(y=0; y<scrY; y++) for(c=0; c<3; c++) { if( ch>255 ) ch=0; // keeping ch in the 0-255 (unsigned char) range rbuf[x][y][c] = (unsigned char)ch; // fill it with something small } // we all know the "array value printout loop" lol for(x=0; x<scrX; x++) for(y=0; y<scrY; y++) for(c=0; c<3; c++) printf("%d ", rbuf[x][y][c] ); if(!DeleteRenderBuffers()) return(0); return(1); } getting the access violations at the line: rbuf[x][y][c] = (unsigned char)ch; ok am using: VS2005 4 gigs of ram and yes i should be spanked for my commenting. thanks guys in advance!
Advertisement
Hello,

I think you're making it too complicated.
Why don't you use a simple array with size scrX*scrY*3 ?

unsigned char * rbuf = (unsigned char *)malloc(scrX*scrY*3*sizeof(unsigned char));

or use C++

std::vector<unsigned char> rbuf;


Uncle
Quote:Original post by UncleRemus
I think you're making it too complicated.
Why don't you use a simple array with size scrX*scrY*3 ?

Seconded!
Quote:
or use C++

Thirded!

Also, the debugger is your friend, here. Step through, and see what fails. The values of everything leading up to this point will be invaluable.
stepped through with the debugger quite slowly. yep it's been a good friend lol
just haven't cought anything through it so far with this one (sigh).
uncle remus this vector thing is new to me. is it part of the standard c++ ?
i'll wander around this sight toof ore more on it. thx guys.

ps: i've gone three and for dimensional arrays like this before.
i'm pretty sure it was on the same compiler. must be getting old heh

Doing it that way is making things difficult. What if you want to access the table as a whole to say write to a file or copy it or something.

Much easier to do as above. scrX*SrcY*3
Quote:Original post by Indogo 2000
this vector thing is new to me. is it part of the standard c++ ?


#include <vector>std::vector<unsigned char> rbuf;rbuf.push_back('a');rbuf.push_back('x');cout << "First value: " << rbuf[0] << endl;rbuf.clear();


Most useful tool I've ever encountered. Google for some more usage on it, like Iterating and such.

"The right, man, in the wrong, place, can make all the dif-fer-rence in the world..." - GMan, Half-Life 2

A blog of my SEGA Megadrive development adventures: http://www.bigevilcorporation.co.uk

Umm... what happens if you do a multidimensional array access on a non-multidimensional-array variable?

If you wish to manually handle multidimensional arrays, do it like this:

char *a;

a[num_columns*row+col]=whatever.




or for a 3 dimensional array:

a[max_x*max_y*z+ max_x*y+x]=whatever.
ok guys. i suppose you're right about the one dimensional array method.
and you guys really have me intrigued about this std::vector deal.
was trying to resurrect some ancient (1995) voxel-terrain code I found from
one of my backup discs. very nastalgic. thanks guys for you insights!!!
Besides the tips above, in the OP's snippet there are at least 2 errors (I haven't looked furthur):

Instead of
if((rbuf=(unsigned char***)malloc(scrX*sizeof(unsigned char)))==NULL)
you have to allocate _pointers_ like here
if((rbuf=(unsigned char***)malloc(scrX*sizeof(unsigned char**)))==NULL)

And later on, instead of
if((rbuf[x]=(unsigned char**)malloc(scrY*sizeof(unsigned char)))==NULL)
you have to allocate _pointers_ like here
if((rbuf[x]=(unsigned char**)malloc(scrY*sizeof(unsigned char*)))==NULL)

The 3rd allocation is correct so far.
ah HAH!!
that was EXACTLY the embarrasingly simple oversight i was hoping it would be
yay i'm not cray lol
works like a charm


thx haegar.
you guys all rock

This topic is closed to new replies.

Advertisement