How to get the size of a dynamically allocated array?

Discussion in 'C Programming' started by linq936@hotmail.com, Jun 8, 2007.

  1. Guest

    Hi,
    If the function signature is like this:

    int func( int arr[] );

    Or this:

    int func ( int* arr );

    Then inside the function I have no way to know the size of the
    array, is that right?

    I tried sizeof(arr)/sizeof(int), I get 1.

    So this means that I must change the signature so that the caller of
    this function must provide the size.

    Thanks.
    , Jun 8, 2007
    #1
    1. Advertising

  2. wrote:
    > Hi,
    > If the function signature is like this:
    >
    > int func( int arr[] );
    >
    > Or this:
    >
    > int func ( int* arr );
    >
    > Then inside the function I have no way to know the size of the
    > array, is that right?


    That is correct.

    > I tried sizeof(arr)/sizeof(int), I get 1.


    sizeof(int *) / sizeof(int) == 1 on your system. It's not possible to have
    an array function parameter. int func(int arr[]) declares func as a
    function accepting a single argument of type pointer-to-int, exactly as
    does int func(int *arr). When you then evaluate sizeof(arr), you get the
    size of the pointer.

    > So this means that I must change the signature so that the caller of
    > this function must provide the size.


    Either that, or you need a special value to behave as an "end of array"
    marker, similar to the '\0' that terminates strings.
    Harald van =?UTF-8?B?RMSzaw==?=, Jun 8, 2007
    #2
    1. Advertising

  3. In article <>,
    <> wrote:

    > If the function signature is like this:


    > int func( int arr[] );


    > Or this:


    > int func ( int* arr );


    > Then inside the function I have no way to know the size of the
    >array, is that right?


    Quite correct. If you must know the size inside the routine,
    you must pass the size in, or there must be some way from the data
    itself to identify the size.
    --
    There are some ideas so wrong that only a very intelligent person
    could believe in them. -- George Orwell
    Walter Roberson, Jun 8, 2007
    #3
  4. writes:

    > Hi,
    > If the function signature is like this:
    >
    > int func( int arr[] );
    >
    > Or this:
    >
    > int func ( int* arr );
    >
    > Then inside the function I have no way to know the size of the
    > array, is that right?


    Yes.

    > So this means that I must change the signature so that the caller of
    > this function must provide the size.


    That is the most flexible solution. You *might* be able to get away with:

    (a) A globally known size -- this might just work if, say, you program
    is modeling n-dimensional vectors and matrices. You might be able to
    make "n" a compile-time constant (#define is the way to do this).

    (b) Some kind of sentinel value you always place at the end of the
    array so that functions know when to avert their eyes. Personally I
    _hate_ such things, but they have their place (no pun intended).

    --
    Ben.
    Ben Bacarisse, Jun 8, 2007
    #4
  5. <> wrote in message
    news:...
    > Hi,
    > If the function signature is like this:
    >
    > int func( int arr[] );
    >
    > Or this:
    >
    > int func ( int* arr );
    >
    > Then inside the function I have no way to know the size of the
    > array, is that right?
    >
    > I tried sizeof(arr)/sizeof(int), I get 1.
    >
    > So this means that I must change the signature so that the caller of
    > this function must provide the size.
    >

    Yes.

    Some versions of C had a function called msize() which gave the size of a
    block allocated with malloc(). Whilst not hard to implement it never made it
    into the standard. The problem is that every funtion that uses it to
    evaluate the size of its argument must be passed a block allocated with
    malloc(), so msize() is a actually a nuisance.


    --
    Free games and programming goodies.
    http://www.personal.leeds.ac.uk/~bgy1mm
    Malcolm McLean, Jun 9, 2007
    #5
  6. BiGYaN Guest

    On Jun 8, 9:30 pm, wrote:
    > Hi,
    > If the function signature is like this:
    >
    > int func( int arr[] );
    >
    > Or this:
    >
    > int func ( int* arr );
    >
    > Then inside the function I have no way to know the size of the
    > array, is that right?
    >
    > I tried sizeof(arr)/sizeof(int), I get 1.
    >
    > So this means that I must change the signature so that the caller of
    > this function must provide the size.
    >
    > Thanks.


    Yup, you are quite right. Except for maybe using a global variable to
    store the size.
    BiGYaN, Jun 9, 2007
    #6
  7. BiGYaN <> writes:
    > On Jun 8, 9:30 pm, wrote:
    >> If the function signature is like this:
    >>
    >> int func( int arr[] );
    >>
    >> Or this:
    >>
    >> int func ( int* arr );
    >>
    >> Then inside the function I have no way to know the size of the
    >> array, is that right?
    >>
    >> I tried sizeof(arr)/sizeof(int), I get 1.
    >>
    >> So this means that I must change the signature so that the caller of
    >> this function must provide the size.
    >>
    >> Thanks.

    >
    > Yup, you are quite right. Except for maybe using a global variable to
    > store the size.


    Yes, I suppose you could use a global variable, but why on Earth would
    you want to? Providing information to functions is exactly what
    parameters are for.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jun 9, 2007
    #7
  8. BiGYaN Guest

    On Jun 10, 1:39 am, Keith Thompson <> wrote:
    > BiGYaN <> writes:
    > > On Jun 8, 9:30 pm, wrote:
    > >> If the function signature is like this:

    >
    > >> int func( int arr[] );

    >
    > >> Or this:

    >
    > >> int func ( int* arr );

    >
    > >> Then inside the function I have no way to know the size of the
    > >> array, is that right?

    >
    > >> I tried sizeof(arr)/sizeof(int), I get 1.

    >
    > >> So this means that I must change the signature so that the caller of
    > >> this function must provide the size.

    >
    > >> Thanks.

    >
    > > Yup, you are quite right. Except for maybe using a global variable to
    > > store the size.

    >
    > Yes, I suppose you could use a global variable, but why on Earth would
    > you want to? Providing information to functions is exactly what
    > parameters are for.
    >
    > --
    > Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    > San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    > "We must do something. This is something. Therefore, we must do this."
    > -- Antony Jay and Jonathan Lynn, "Yes Minister"



    @ Keith
    I was just mentioning an alternative way. Of course it doesn't make
    much sense and is quite silly. But one can never be sure about all the
    cases. So I just mentioned it.
    BiGYaN, Jun 10, 2007
    #8

  9. > > > Yup, you are quite right. Except for maybe using a global variable to
    > > > store the size.

    > >

    >
    > @ Keith
    > I was just mentioning an alternative way. Of course it doesn't make
    > much sense and is quite silly. But one can never be sure about all the
    > cases. So I just mentioned it.


    or one can use implementation dependent ways
    eg if one knows the array is malloced, then some malloc
    implementations store the allocated size somwhere before the pointed
    object

    okok i know this is the worst hack possible
    Szabolcs Nagy, Jun 10, 2007
    #9
  10. On Jun 10, 6:24 am, Szabolcs Nagy <> wrote:

    > or one can use implementation dependent ways
    > eg if one knows the array is malloced, then some malloc
    > implementations store the allocated size somwhere before the pointed
    > object
    >
    > okok i know this is the worst hack possible


    I had tried this out once and it didn't work. Yes, the size is stored
    before in the malloc'ed block header. That's why free() doesn't
    require a size argument. The problem is that this info cannot be
    accessed from user-space. Probably msize() uses this.

    Regards,
    Frodo B
    Frodo Baggins, Jun 10, 2007
    #10
  11. Frodo Baggins <> writes:
    > On Jun 10, 6:24 am, Szabolcs Nagy <> wrote:
    >> or one can use implementation dependent ways
    >> eg if one knows the array is malloced, then some malloc
    >> implementations store the allocated size somwhere before the pointed
    >> object
    >>
    >> okok i know this is the worst hack possible

    >
    > I had tried this out once and it didn't work. Yes, the size is stored
    > before in the malloc'ed block header. That's why free() doesn't
    > require a size argument. The problem is that this info cannot be
    > accessed from user-space. Probably msize() uses this.


    The problem is that there is no standard for storing this information.
    One implementation might store the size (which could be either the
    requested size or the allocated size) just before the malloc'ed block;
    another might store it in a centralized table. Code that make
    assumptions about this is *extremely* non-portable.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jun 10, 2007
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff Williams

    Dynamically allocated array question

    Jeff Williams, Aug 18, 2003, in forum: C++
    Replies:
    6
    Views:
    495
    Alf P. Steinbach
    Aug 19, 2003
  2. Replies:
    5
    Views:
    617
    Matt Wharton
    Dec 9, 2004
  3. kapilk
    Replies:
    1
    Views:
    274
    -berlin.de
    Aug 16, 2004
  4. Replies:
    5
    Views:
    400
    Keith Thompson
    Oct 21, 2005
  5. Replies:
    2
    Views:
    304
    Default User
    May 8, 2007
Loading...

Share This Page