Thread: Determine length of an array

  1. #1
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582

    Determine length of an array

    How do you determine the length of an array? For example:

    unsigned char HillsData[163840];

    When I use:

    DebugTest[3] = sizeof(HillsData);

    I get 4 rather than 163840. I'd like to know how you determine the size of an array, as to prevent overflow. The image, for example, is 1024x40 pixels at 32-bit color. If one bothers to modify it to 1024x48 at the same color depth, array overflow would occur because it's reading more data than the array has available. If it was reduced to 1024x32 instead, then the image would not have array overflow when loaded.

    In short, how do you determine the length of an array (how many elements it has)?

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    sizeof() should return the size of a static array. For dynamically allocated memory, you need to keep track of it yourself or use a non-portable method of obtaining the information.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    My guess is you're passing it to a function and trying to get its size there. You can't do that.
    Code:
    void foo( unsigned char array[] )
    {
        ... sizeof( array ) gives you the size of a pointer to unsigned char ...
    }
    
    int main( void )
    {
        unsigned char array[200];
    
        ... sizeof( array ) gives you the size of the array ...
    
        foo( array );
    
        return 0;
    }
    Arrays when passed to functions, arrive as a pointer to the type of its first element.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    The array is static. I don't know how to make dynamic-sized arrays. If the static array has 163840 elements. The array is pointed to within the function though.

    Code:
    char LoadFile(const char FileName[64], char FileType, BITMAPINFOHEADER *BMPInfo, unsigned char *BMPData, char FogUsed, double ObjectScaling, char LoadType)
    {
    	...
    	if (DebugTest[0] == 2) // check to see if the method works
    	{
    		DebugTest[0] = 3;
    		DebugTest[1] = sizeof(BMPData); // a test
    		sprintf(DebugDetails, "First array length is %d", DebugTest[1]); // to display debug details
    		MessageBox(hwnd, DebugDetails, "DebugResults", MB_OK | MB_ICONEXCLAMATION); // shows 4
    	}
    	
    	if (DebugTest[0] == 1)
    	{
    		DebugTest[0] = 2;
    		DebugTest[2] = sizeof(BMPData); // a test
    		sprintf(DebugDetails, "Second array length is %d", DebugTest[2]); // to display debug details
    		MessageBox(hwnd, DebugDetails, "DebugResults", MB_OK | MB_ICONEXCLAMATION); // shows 4
    	}
    	
    	if (DebugTest[0] == 0)
    	{
    		DebugTest[0] = 1;
    		DebugTest[3] = sizeof(BMPData); // a test
    		sprintf(DebugDetails, "Third array length is %d", DebugTest[3]); // to display debug details
    		MessageBox(hwnd, DebugDetails, "DebugResults", MB_OK | MB_ICONEXCLAMATION); // shows 4
    	}
    What I'm doing is trying to see if the image is too big for the array.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    > The array is pointed to within the function though.
    Yeah well, the size of a pointer is the size of a pointer.

    Pass the number of elements to a function
    Store the number of elements as the last item in the array
    Iterate to a special value

    You'll think of something.

  6. #6
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Change your declaration in the function head to match the one you used to declare your array.
    unsigned char *BMPData
    into
    unsigned char BMPData[163840]

  7. #7
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    Including that 163840 will definitely not work. The array size varies from 70,400 on the low end to as much as even 1.44 million. I'm testing another method - putting the sizeof thing in the call's parameters and using that....

  8. #8
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    Yeah, adding another element to the function's parameters worked and gives exactly what I'm expecting to see. Thanks for the assistance though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. length of char array
    By taurus in forum C Programming
    Replies: 41
    Last Post: 09-22-2007, 05:33 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Making a script with arbitrary array length
    By Bri Rock in forum Linux Programming
    Replies: 3
    Last Post: 07-15-2004, 08:59 AM
  5. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM