pointer to pointer?
Hi guys!
I need to create few buffers for bitmap data. The problem is that i dont know how many bitmaps there will be and how big they are. I thought about making pointer to pointer like that:
unsigned char** buf;
then grabbing some memory for each bitmap pointer by malloc(making for example 5 pointers for 5 bitmaps), and finnaly alloc memory for each pointer for bitmap data. I tested all combination but my program isnt working well. Anyone can explain me how to do that? I will apreciate any help
Quote:Original post by heyeh
I need to create few buffers for bitmap data. The problem is that i dont know how many bitmaps there will be and how big they are. I thought about making pointer to pointer like that.
Here's one way I know off the top of my head. It is in C++ and pseudo parts are marked:
// bufferunsigned char** buf;// determined at run timeint images = 5;// Create an array of 5 uchar pointersbuf = new unsigned char*[5];// Load the datafor(int x=0;x<images;x++){ // We need to know the size of the file (PSEUDO) int size = Image_x.bmp -> Size // Now we allocate the memory buf[x] = new unsigned char[size]; // Now read in all the data (PSEUDO) ReadBmp( &buf[x], ... )}//Finally after you are done with them:for(int x=0;x<images;x++){ delete [] buf[x]; buf[x] = 0;}delete [] buf;buf = 0;
Now I know someone is going to come along and correct something that I have done, but that's a-ok [smile]. I just showd the general concept of what you need to do. However, I used new/delete because, well I know how. I'm sure you could substitue in malloc and free and get the deisred effects, but you will need to check the usage of them. I do not have experience using those two.
- Drew
Hey,
I can think of 2 ways to fix this problem...
1. Allocate space for more pointers than you'll ever need. Unfortunately, this is a waste of memory and places a limitation on the number of images. It is easier to manage though.
2.
char*** buf=0;
buf=(char***)malloc(imagecount*sizeof(char*));
(*buf)[index];
whoa, not so sure if the *** is nessecary, but yeah, when you do
char** buf=0x0F000000;
buf[1]=0xDEADBEEF;
it gets broken into &buf + sizeof(char*)... I think... But it's waaay too early in the morning. I think that the following code will cause an error...
char** buf=malloc(sizeof(char*));
buf[0]=0; <-- I think buf is now =0
*buf=0;
I'm working from memory here, so there's a pretty good chance I'm wrong...
I can think of 2 ways to fix this problem...
1. Allocate space for more pointers than you'll ever need. Unfortunately, this is a waste of memory and places a limitation on the number of images. It is easier to manage though.
2.
char*** buf=0;
buf=(char***)malloc(imagecount*sizeof(char*));
(*buf)[index];
whoa, not so sure if the *** is nessecary, but yeah, when you do
char** buf=0x0F000000;
buf[1]=0xDEADBEEF;
it gets broken into &buf + sizeof(char*)... I think... But it's waaay too early in the morning. I think that the following code will cause an error...
char** buf=malloc(sizeof(char*));
buf[0]=0; <-- I think buf is now =0
*buf=0;
I'm working from memory here, so there's a pretty good chance I'm wrong...
Simply replace
buf = new unsigned char *[images];
withbuf = (unsigned char **)malloc(images * sizeof(unsigned char *));
andbuf[x] = new unsigned char[size];
withbuf[x] = (unsigned char *)malloc(size * sizeof(unsigned char));
for C allocation. To deallocate:for(int x = 0; x < images; ++x) free(buf[x]);free(buf);
Much thanks Oluseyi. I really had no idea how it's done in C and I didn't wanna just post something whack. [smile]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement