sizeof() function on a pointer to an array of a struct

This is a discussion on sizeof() function on a pointer to an array of a struct within the C++ Programming forums, part of the General Programming Boards category; Whew now that that confusing Thread Title is done and over with.. I think I explained it correct... Well... here ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    93

    Question sizeof() function on a pointer to an array of a struct

    Whew now that that confusing Thread Title is done and over with.. I think I explained it correct...

    Well... here is my problem..

    Code:
    RGBQUAD *mColorTable;
    
    mColorTable = new RGBQUAD[256];
    sizeof(mColorTable);
    thats obviously not my exact code, but you get the general idea of what I'm trying to do..

    Additional info: RGBQUAD struct contains 4 member variables.

    Therefore the size of mColorTable should be 4 * 256 = 1024.
    However, since mColorTable is pointing to the first index in the array the code is returning 4 * 1 = 4.

    Obviously i can rid of pointers all together and just use

    Code:
    RGBQUAD mColorTable[256];
    sizeof(mColorTable);
    but I do not know the size of the array until runtime, which is why I am using pointers.

    thnx for any help...

    P.S. for those wondering I am trying to create a class for loading BITMAPS of various sizes and color depths... this portion is for storing the color table of my BITMAP.. currently I am only able to store the first entry of the color table, which is black meaning all I get when I try and draw my BITMAP is a black screen
    Last edited by tegwin; 05-28-2004 at 02:40 PM.

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    what's happening here is you're trying to find the size of the pointer itself. Most pointers tend to be 4 bytes big.

    What you need to do is something like so:

    sizeof(RGBQUAD)*256

    so you take the size of a RGBQUAD structure times the number of items in the array. This is the best way to calculate it (as far as i know, please correct me if i'm wrong)

    But keep that in mind in the future, when doing sizeof of a pointer, it will actually tell you the size OF the pointer, because the compiler doesn't know how big the array is going to be either

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    A pointer is a reference to the object it is assigned. It holds the address of the object it is "pointing" to. A pointer will always be 4 bytes in size (whether this a pointer to a char, int, float or an object of any size in bytes) and therefore you will not be able to get the size of an object directly through a pointer.
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,630
    >> A pointer will always be 4 bytes in size
    On a 32-bit machine, they're 8 bytes on a 64-bit machine

    gg

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    >> they're 8 bytes on a 64-bit machine


    That may depend on your compiler. Pointers are the same size as integers.
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,630
    >> Pointers are the same size as integers.
    Wrong. There is no coorelation between sizeof(void*) and sizeof(int).
    [edit]
    or any other integer. Just because sizeof() returns the same size for a pointer as it does for an integer, doesn't mean one can make that statement.
    Yes, pointers are the same size as int's and long's on most all 32-bit compilers.
    [/edit]

    However, it is compiler dependent. You can read about MS's 64-bit compilation model (LLP64) here.
    All of the *nix 64-bit compiler implementations use the LP64 model, where the "long" data type becomes 8 bytes.

    To get your learn-on even further, start here.

    gg
    Last edited by Codeplug; 05-28-2004 at 04:08 PM.

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    Okay, the size of a pointer comment may confuse newbies. I was only using int as a comparison. Not to cause any confusion.
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  8. #8
    Registered User
    Join Date
    Feb 2002
    Posts
    93
    ah I thought the sizeof function was getting the size of the first index of the array i did not know that all pointers are 4 bytes.. just so happened the struct was 4 bytes and the pointer was the same which confused me..

    thnx for the info.. i guess i'll just determine the size of the array at runtime and then multiply that variable by the size of the structure as mentioned above...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 02:55 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

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