Find Size Of Dynamic Memory Allocation?

This is a discussion on Find Size Of Dynamic Memory Allocation? within the C++ Programming forums, part of the General Programming Boards category; Hi, How do i find at run-time the size of a dynamic memory allocation, which is an array: Code: double ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    45

    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. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,553
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    45
    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. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,553
    chars are just small integers as far as C is concerned.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    45
    so why would you not use the integer type?

    Many Thanks
    -Alex

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,553
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Aug 2006
    Posts
    45
    Ok,

    Duh...i really should had thought about that before asking..


    Thanks For Your Help Anyway!

    Cheers
    -Alex

  8. #8
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Quote Originally Posted by appleGuy View Post
    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();
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adventures in labyrinth generation.
    By guesst in forum Game Programming
    Replies: 8
    Last Post: 10-12-2008, 01:30 PM
  2. Dynamic memory allocation
    By Luciferek in forum C++ Programming
    Replies: 118
    Last Post: 10-02-2008, 11:34 AM
  3. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  4. Dynamic Memory Allocation (Seg. Fault)
    By dr_jaymahdi in forum C++ Programming
    Replies: 10
    Last Post: 09-30-2007, 02:02 PM
  5. Dynamic Memory Allocation Question
    By Piknosh in forum C++ Programming
    Replies: 1
    Last Post: 04-14-2004, 01:55 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21