Sign in to follow this  
Indogo 2000

Can't seem to see the forest for the trees

Recommended Posts

Indogo 2000    122
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!

Share this post


Link to post
Share on other sites
UncleRemus    313
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

Share this post


Link to post
Share on other sites
jouley    805
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.

Share this post


Link to post
Share on other sites
Indogo 2000    122
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

Share this post


Link to post
Share on other sites
empirical2    567
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

Share this post


Link to post
Share on other sites
deadstar    536
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.

Share this post


Link to post
Share on other sites
losethos    100
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.

Share this post


Link to post
Share on other sites
Indogo 2000    122
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!!!

Share this post


Link to post
Share on other sites
haegarr    7372
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.

Share this post


Link to post
Share on other sites
jouley    805
Quote:
Original post by Indogo 2000
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

So now you've learned a lesson and will use std::vectors, right? [smile]

Share this post


Link to post
Share on other sites
Spoonbender    1258
Quote:
Original post by Indogo 2000
you definitely got me curious enough to check out
the std::vector mechanism Jouley! i've started poking at it.
what about performance though?

In principle, identical to raw arrays.
Indexing into the array typically gets inlined by the compiler so it is as efficient as if it'd been a regular array.

If you're using MSVC 2k5 or later though, Microsoft has added a bit of overhead to it, by doing bounds-checking by default. That can be disabled by defining the right macro however, and usually the performance hit from this bounds-checking is negligible in any case.

Share this post


Link to post
Share on other sites
Indogo 2000    122

It may sound wierd but i'm working to DEoptimize
the ray casting mechanism to just regular vector math.
Yep it'll be painfully slow. I will then be porting the
routines to run in parallel on the GPU via CUDA. Was thinking
of a way to implement the paralelism.
Ok a few ways come to mind but trying to send even just
a 512x512 heightmap to each gpu would be way too much.

So I was thinking of like a supervoxel. say 8x8x8 segments.
Using a 3d no-hit-test bresenhams-line-algorithm to make a
slice of data to pass to the gpus. they would each hit test
their own low-res supervoxel, flagging it as having data or not.
The supervoxels actually HAVING data would be sent BACK to
the gpus for voxel level hit-testing. I know it seems like a lot
of extra work but when the voxel data starts getting even bigger
it seems like it would pay off even more.

Share this post


Link to post
Share on other sites
Indogo 2000    122
eh? about what? i was the one intoduced to the std::vector stuff. i really know
nothing about it. if you mean about the voxel thing...just nerding out. i even have the quintisential<sp?> boney nerd chest so i can't do too much thumping.
how do smileys work?

Share this post


Link to post
Share on other sites
MaulingMonkey    1728
Quote:
Original post by losethos
Who pays you to push the stl library vector stuff? I find a lot of people who teach it as some kind of gospel. Who here agrees with me?


There really is no need to need to pay anyone to encourage the use of your C++ Standard Library, of which vector is a part of. C++'s "unfortunate" C heritage, and the persisting misinformation surrounding (or plain ignorance of) that standard means proponents of using C++'s standard library tend to be understandably louder than for other languages, where using your standard library is already a given.

Does this encouragement to learn more about your tools disagree with you somehow? Can you elaborate how what you're seeing here is somehow a bad thing? It certainly seems to be implied from the whole (hilarious) shilling accusation that you have some bone to pick with it.

Share this post


Link to post
Share on other sites
fpsgamer    856
Quote:
Original post by losethos
Who pays you to push the stl library vector stuff? I find a lot of people who teach it as some kind of gospel. Who here agrees with me?


You're being an idiot.

Share this post


Link to post
Share on other sites
losethos    100
On a commodore 64, every programmer knew how to do array indexing and understood how two dimensional arrays were stored because you needed it to put a letter on the screen in a certain x,y spot. Kids these days! The screen was 40 columns wide and started at memory location 1024.

poke 1024+40*y+x,42

(42 is ASCII for '*')

Let's say you have a 4x8 array. It is stored

0 1 2 3 4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23
24 25 26 27 28 29 30 31

You homework assignment is to write the formula for index this 4x8 array given a x and y variables.

It's not so hard, right? This standard library is threatening to leave programmer ignorant of the basics of computer science. Sometimes, making things "simpler" leaves people ignorant of the basics and you can't ever hope to be a skilled programmer if you haven't a clue about the implimentation.

I don't have the patience to bother learning the standard library. It doesn't seem like it improves anything that I do regularly. I guess if it were a case where I wanted a top-of-the-line algorithm and didn't want reimplement and debug it, it'd be good.

Most programming is pretty straightforward -- dialog boxes, menus, crap like that... rarely do you do tricky cool stuff. What I hate is, since you use it so rarely, you never can remember it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this