Pointers in a function, sizeof doesn't get array length

This is a discussion on Pointers in a function, sizeof doesn't get array length within the C Programming forums, part of the General Programming Boards category; When a function takes a pointer to an array as a parameter, how do I determine the size of the ...

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

    Pointers in a function, sizeof doesn't get array length

    When a function takes a pointer to an array as a parameter, how do I determine the size of the array and not the pointer? This is what I mean, for a brief example:

    Code:
    unsigned char RandomArray[16384];
    
    void TestFunction(unsigned char *ArrayPointer)
    {
       const int ArrayLength = sizeof(ArrayPointer); // * or & in front doesn't work
       const int ObviousRoute = sizeof(RandomArray); // this works as expected
    }
    I need the top case to work, not the bottom case. The function processes multiple arrays and I need to make sure I don't exceed the array's limit. Whether I put * or & in the front, I don't get the 16,384 I'm expecting, just 1 in the case of the * and 4 in the case of the & (the latter I'd expect since it's getting the address instead, a 32-bit value).
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,338
    You're just going to have to pass the size as an additional parameter.

    There is NO WAY to discover how much memory a pointer points to from just looking at the pointer.
    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
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Question 6.21
    Read all c-faqs.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ulillillia View Post
    When a function takes a pointer to an array as a parameter, how do I determine the size of the array and not the pointer? This is what I mean, for a brief example:

    Code:
    unsigned char RandomArray[16384];
    
    void TestFunction(unsigned char *ArrayPointer)
    {
       const int ArrayLength = sizeof(ArrayPointer); // * or & in front doesn't work
       const int ObviousRoute = sizeof(RandomArray); // this works as expected
    }
    I need the top case to work, not the bottom case. The function processes multiple arrays and I need to make sure I don't exceed the array's limit. Whether I put * or & in the front, I don't get the 16,384 I'm expecting, just 1 in the case of the * and 4 in the case of the & (the latter I'd expect since it's getting the address instead, a 32-bit value).
    Actually sizeof() is behaving correctly... in the first line you asked it for the size of a pointer which it obediently returned.

    If your function is working on arrays by pointer you will need to add a second parameter to your function and pass in the size of the array as a value.

    If, by chance, those arrays are holding strings which may be of different lengths you can use strlen() to discover the size of the string... but not of the buffer holding it.

    Your other choice is to add an extra element to each array and use a guard value in that last element that can't possibly occur in normal use... something like -127... you can then scan forward from the array pointer until you find your guard value... and derive your array size from that.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    Quote Originally Posted by ulillillia View Post
    When a function takes a pointer to an array as a parameter, how do I determine the size of the array and not the pointer? This is what I mean, for a brief example:

    Code:
    unsigned char RandomArray[16384];
    
    void TestFunction(unsigned char *ArrayPointer)
    {
       const int ArrayLength = sizeof(ArrayPointer); // * or & in front doesn't work
       const int ObviousRoute = sizeof(RandomArray); // this works as expected
    }
    I need the top case to work, not the bottom case. The function processes multiple arrays and I need to make sure I don't exceed the array's limit. Whether I put * or & in the front, I don't get the 16,384 I'm expecting, just 1 in the case of the * and 4 in the case of the & (the latter I'd expect since it's getting the address instead, a 32-bit value).
    You could use a structure, rather than passing the array itself to functions:

    Code:
    #define spanof( data )\
    ( sizeof( data ) / sizeof( data[ 0 ] ) )
    
    #define define_array( type )\
    struct array_##type  {\
    type* data;\
    size_t size;\
    };\
    
    // Example
    
    #include "stdio.h"
    
    define_array( int );
    
    void display( array_int values ) {
        for( size_t idx = 0; idx < values.size; ++idx ) {
            printf( "%d ", values.data[ idx ] );
        }
    }
    
    int main( ) {
        int loc[ 7 ] = { 8, 6, 7, 5, 3, 0, 9 };
        array_int vls = { loc, spanof( loc ) };    
        display( vls );
    }
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. array of function pointers
    By kiros88 in forum C Programming
    Replies: 30
    Last Post: 09-15-2009, 10:38 PM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 05:58 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

Tags for this Thread


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