# Thread: Find Size Of Dynamic Memory Allocation?

1. ## Find Size Of Dynamic Memory Allocation?

Hi,

How do i find at run-time the size of a dynamic memory allocation, which is an array:

Code:
```double **imageBuffer = NULL;
imageBuffer = new double* [30]```
I need a way to find the size of this array as I wont know the value in the [].

Many Thanks
Alex

2. You can't.

Just store the size in a variable the same way that you're storing the pointer in a variable.

If you want an easy way of keeping them together, put them both in a struct.

Or better yet, since this is C++, make a small class which hides some of the gory detail.

3. Ok,

Thank you

Also quick question:

In some C code ive seen, when storing an array of values that make up an image (such as a tga or jpeg) they use a char type, why do they do that if the values for R G B are going to be numerical?

Many Thanks
-Alex

4. chars are just small integers as far as C is concerned.

5. so why would you not use the integer type?

Many Thanks
-Alex

6. Space perhaps?
Your 1280*1024 screen has well over 1M pixels.
Using chars to store RGB gets you to nearly 4M bytes.
It would be 16MB if you stored RGB as integers.

7. Ok,

Cheers
-Alex

8. Originally Posted by appleGuy
Ok,

Thank you

Also quick question:

In some C code ive seen, when storing an array of values that make up an image (such as a tga or jpeg) they use a char type, why do they do that if the values for R G B are going to be numerical?

Many Thanks
-Alex
I'm not sure here but it could be possible that the chars repesent the RGB components of the colour. You would then to use bitshifting to put them all into a 32bit colour value.

Heres an example (sorry its in C):
Code:
```int ToRGB(int r, int g, int b) { r=r<<16; g=g<<8; return r+g+b; }
int ToARGB(int a, int r, int g, int b) { a=a<<24; r=r<<16; g=g<<8; return a+r+g+b; }
int AlphaEl(int col) { return col >> 24; }
int RedEl(int col)   { return (col >> 16) & 255; }
int GreenEl(int col) { return (col >> 8) & 255;}
int BlueEl(int col)  { return col & 255; }

int main()
{
int r, g, b, col;
printf("Enter red element: "); scanf("%i", &r);
printf("Enter green element: "); scanf("%i", &g);
printf("Enter blue element: "); scanf("%i", &b);
col=ToRGB(r, g, b);
printf("Colour value %i\n", col);
printf("Convert to red: %i\n", RedEl(col));
printf("Convert to green: %i\n", GreenEl(col));
printf("Convert to blue: %i\n", BlueEl(col));
getchar();
getchar();
}```