Jump to content
  • Advertisement
Sign in to follow this  
heyeh

pointer to pointer?

This topic is 4848 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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:

// buffer
unsigned char** buf;

// determined at run time
int images = 5;

// Create an array of 5 uchar pointers
buf = new unsigned char*[5];

// Load the data
for(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

Share this post


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

Share this post


Link to post
Share on other sites
Simply replace
buf = new unsigned char *[images];
with
buf = (unsigned char **)malloc(images * sizeof(unsigned char *));
and
buf[x] = new unsigned char[size];
with
buf[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);

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!