sizeof dynamic array

Discussion in 'C Programming' started by D@nny, Nov 7, 2004.

  1. D@nny

    D@nny Guest

    hi,

    i would like to know how to calculate the size of a dynamic array
    created using a dereference declaration like int *numbers and
    allocating via malloc or calloc: numbers=(int
    *)malloc(sizeof(int)*10);

    using sizeof(numbers) will return the pointers size...

    is there a possibility?

    D@nny
    --
    life already is expensive, so why waste money on expensive
    software.
    D@nny, Nov 7, 2004
    #1
    1. Advertising

  2. D@nny

    Mike Wahler Guest

    "D@nny" <> wrote in message
    news:fZljd.1744$...
    > hi,
    >
    > i would like to know how to calculate the size of a dynamic array


    Determine how big you need it to be, and allocate that much.

    > created using a dereference declaration like int *numbers


    That's not a 'dereference declaration'. Its a declaration
    of a pointer object.

    >and
    > allocating via malloc or calloc: numbers=(int
    > *)malloc(sizeof(int)*10);


    Never cast the return value from 'malloc()'. See the FAQ
    for details.

    >
    > using sizeof(numbers) will return the pointers size...


    Yes. Because 'numbers' is a pointer.

    >
    > is there a possibility?


    Yes. You know the size when you allocate it. Just retain
    that value (i.e. store it in a 'size_t' object).

    int *numbers = 0;
    size_t sz = 10 * sizeof *numbers;

    if(numbers = malloc(sz))
    {
    printf("%lu bytes allocated.\n", (unsigned long)sz);
    /* do stuff */
    free(numbers);
    }

    -Mike
    Mike Wahler, Nov 7, 2004
    #2
    1. Advertising

  3. D@nny wrote:
    > hi,
    >
    > i would like to know how to calculate the size of a dynamic array
    > created using a dereference declaration like int *numbers and
    > allocating via malloc or calloc:


    >numbers=(int *)malloc(sizeof(int)*10);


    Guess what: the size of the array is sizeof(int)*10. What did you think
    it was.

    BTW, your code line above is not, at least here, idiomatic. Had you
    lurked before posting (expected usenet behavior) or checked the FAQ
    before posting (expected usenet behavior), you would have no this.

    You have no idea how many people everyday violate those two standard
    rules every day just to post code that casts the return value from
    malloc (corrected several times a day here), uses hard-coded magic
    numbers, uses hard-coded data types in allocation statements, asks
    questions answered multiple times a week (as is yours), and fails to
    include a compilable example.

    #include <stdlib.h>
    int main(void)
    {
    const number_elements = 10;
    int *numbers;
    numbers = malloc(number_elements * sizeof *numbers);
    if (!numbers) { /* handle error */ }
    else free(numbers);
    return 0;
    }

    > using sizeof(numbers) will return the pointers size...
    >
    > is there a possibility?


    Obviously, there is. If you can figure out how much space to ask malloc
    for, you already know the answer.
    Martin Ambuhl, Nov 7, 2004
    #3
  4. D@nny <> scribbled the following:
    > hi,


    > i would like to know how to calculate the size of a dynamic array
    > created using a dereference declaration like int *numbers and
    > allocating via malloc or calloc: numbers=(int
    > *)malloc(sizeof(int)*10);


    > using sizeof(numbers) will return the pointers size...


    > is there a possibility?


    Not in standard C. You'll have to use implementation-specific tricks.
    But since you know the size when you allocate the memory, why not simply
    keep track of it?

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-------------------------------------------------------- rules! --------/
    "To know me IS to love me."
    - JIPsoft
    Joona I Palaste, Nov 7, 2004
    #4
  5. D@nny

    Al Bowers Guest

    D@nny wrote:
    > hi,
    >
    > i would like to know how to calculate the size of a dynamic array
    > created using a dereference declaration like int *numbers and
    > allocating via malloc or calloc: numbers=(int
    > *)malloc(sizeof(int)*10);
    >
    > using sizeof(numbers) will return the pointers size...
    >
    > is there a possibility?


    What you should do is declare a variable, ex. size_t nelements,
    that you can use to store the number of elements allocated for
    the int array. If you dynamically allocated the array for
    10 elements, then store in nelements the value 10. If you
    modify the number of array elements, then modify nelements
    appropriately. In the occasion where you might need the
    total size of the allocation, just multiply the sizeof(int)
    with nelements.

    #include <stdio.h>
    #include <stdlib.h>

    int main(void)
    {
    int *iarr, *tmp;
    size_t nelements = 0;

    iarr = malloc((sizeof *iarr)*10);
    if(iarr) nelements = 10;
    printf("Number of elements allocated is %u\n"
    "Size of the allocation is %u\n\n",
    nelements, nelements*sizeof(int));
    if(nelements)
    {
    puts("An attempt to increase the allocation one element\n");
    tmp = realloc(iarr,(sizeof *iarr)*(nelements+1));
    if(tmp)
    {
    iarr = tmp;
    nelements++;
    }
    printf("Number of elements allocated is %u\n"
    "Size of the allocation is %u\n\n",
    nelements, nelements*sizeof(int));
    }
    free(iarr);
    return 0;
    }


    --
    Al Bowers
    Tampa, Fl USA
    mailto: (remove the x to send email)
    http://www.geocities.com/abowers822/
    Al Bowers, Nov 7, 2004
    #5
  6. "Mike Wahler" <> wrote in message
    news:cfmjd.7863$...
    > "D@nny" <> wrote in message
    > news:fZljd.1744$...
    > > hi,
    > >
    > > i would like to know how to calculate the size of a dynamic array

    >
    > Determine how big you need it to be, and allocate that much.
    >
    > > created using a dereference declaration like int *numbers

    >
    > That's not a 'dereference declaration'. Its a declaration
    > of a pointer object.
    >
    > >and
    > > allocating via malloc or calloc: numbers=(int
    > > *)malloc(sizeof(int)*10);

    >
    > Never cast the return value from 'malloc()'. See the FAQ
    > for details.


    Hello I am not the original poster... but this seems interesting.

    I can't the answer in this FAQ;

    http://www.eskimo.com/~scs/C-faq/s7.html

    Why shouldn't it be typecasted ?

    1. Maybe because it doesn't compile on true c compilers ?

    2. Or does it lead to bugs ?

    Bye,
    Skybuck.
    Skybuck Flying, Nov 7, 2004
    #6
  7. On Sun, 7 Nov 2004 15:47:26 +0100, "Skybuck Flying"
    <> wrote:

    >
    >"Mike Wahler" <> wrote in message
    >news:cfmjd.7863$...


    snip

    >> Never cast the return value from 'malloc()'. See the FAQ
    >> for details.

    >
    >Hello I am not the original poster... but this seems interesting.
    >
    >I can't the answer in this FAQ;
    >
    >http://www.eskimo.com/~scs/C-faq/s7.html
    >
    >Why shouldn't it be typecasted ?


    a. It is unnecessary in C. Superfluous casts should always be
    avoided.

    b. It leads to undefined behavior (under C89, currently the standard
    implemented by most popular compilers) if the prototype for malloc is
    omitted.


    <<Remove the del for email>>
    Barry Schwarz, Nov 7, 2004
    #7
  8. D@nny

    Method Man Guest

    "D@nny" <> wrote in message
    news:fZljd.1744$...
    > hi,
    >
    > i would like to know how to calculate the size of a dynamic array
    > created using a dereference declaration like int *numbers and
    > allocating via malloc or calloc: numbers=(int
    > *)malloc(sizeof(int)*10);
    >
    > using sizeof(numbers) will return the pointers size...
    >
    > is there a possibility?
    >


    As others have mentioned, keep track of the size when you allocate the
    array.

    I suppose one possible exception is a null-terminated char array where you
    can call 'strlen' to get the length of the array.
    Method Man, Nov 8, 2004
    #8
  9. D@nny

    Default User Guest

    Method Man wrote:
    > "D@nny" <> wrote in message
    > news:fZljd.1744$...
    > > hi,
    > >
    > > i would like to know how to calculate the size of a dynamic array


    > As others have mentioned, keep track of the size when you allocate

    the
    > array.
    >
    > I suppose one possible exception is a null-terminated char array

    where you
    > can call 'strlen' to get the length of the array.


    That gives you length of the string, not the size of the array. They
    are often different. So for instance, you have no way of knowing
    whether you can extend the string safely unless you know the size of
    the underlying array.



    Brian
    Default User, Nov 8, 2004
    #9
  10. D@nny

    Mike Wahler Guest

    "Method Man" <> wrote in message
    news:H1Ljd.5882$...
    >
    > "D@nny" <> wrote in message
    > news:fZljd.1744$...
    > > hi,
    > >
    > > i would like to know how to calculate the size of a dynamic array
    > > created using a dereference declaration like int *numbers and
    > > allocating via malloc or calloc: numbers=(int
    > > *)malloc(sizeof(int)*10);
    > >
    > > using sizeof(numbers) will return the pointers size...
    > >
    > > is there a possibility?
    > >

    >
    > As others have mentioned, keep track of the size when you allocate the
    > array.
    >
    > I suppose one possible exception is a null-terminated char array where you
    > can call 'strlen' to get the length of the array.


    char *array = malloc(100);
    size_t sz = 0;

    if(array)
    {
    strcpy(array, "Hello");
    sz = strlen(array); /* sz != 100 */
    }

    free(array);

    -Mike
    Mike Wahler, Nov 9, 2004
    #10
  11. D@nny

    Method Man Guest

    "Mike Wahler" <> wrote in message
    news:mkUjd.21470$...
    >
    > "Method Man" <> wrote in message
    > news:H1Ljd.5882$...
    > >
    > > "D@nny" <> wrote in message
    > > news:fZljd.1744$...
    > > > hi,
    > > >
    > > > i would like to know how to calculate the size of a dynamic array
    > > > created using a dereference declaration like int *numbers and
    > > > allocating via malloc or calloc: numbers=(int
    > > > *)malloc(sizeof(int)*10);
    > > >
    > > > using sizeof(numbers) will return the pointers size...
    > > >
    > > > is there a possibility?
    > > >

    > >
    > > As others have mentioned, keep track of the size when you allocate the
    > > array.
    > >
    > > I suppose one possible exception is a null-terminated char array where

    you
    > > can call 'strlen' to get the length of the array.

    >
    > char *array = malloc(100);
    > size_t sz = 0;
    >
    > if(array)
    > {
    > strcpy(array, "Hello");
    > sz = strlen(array); /* sz != 100 */
    > }
    >
    > free(array);
    >


    Yeah, I should have noted that 'strlen' gives the length of the string and
    not necessarily the allocated size of the array. That's why I said "possible
    exception".
    Method Man, Nov 9, 2004
    #11
  12. D@nny

    Mike Wahler Guest

    "Method Man" <> wrote in message
    news:cwVjd.5957$...
    >
    > "Mike Wahler" <> wrote in message
    > news:mkUjd.21470$...
    > >
    > > "Method Man" <> wrote in message
    > > news:H1Ljd.5882$...
    > > >
    > > > "D@nny" <> wrote in message
    > > > news:fZljd.1744$...
    > > > > hi,
    > > > >
    > > > > i would like to know how to calculate the size of a dynamic array
    > > > > created using a dereference declaration like int *numbers and
    > > > > allocating via malloc or calloc: numbers=(int
    > > > > *)malloc(sizeof(int)*10);
    > > > >
    > > > > using sizeof(numbers) will return the pointers size...
    > > > >
    > > > > is there a possibility?
    > > > >
    > > >
    > > > As others have mentioned, keep track of the size when you allocate the
    > > > array.
    > > >
    > > > I suppose one possible exception is a null-terminated char array where

    > you
    > > > can call 'strlen' to get the length of the array.

    > >
    > > char *array = malloc(100);
    > > size_t sz = 0;
    > >
    > > if(array)
    > > {
    > > strcpy(array, "Hello");
    > > sz = strlen(array); /* sz != 100 */
    > > }
    > >
    > > free(array);
    > >

    >
    > Yeah, I should have noted that 'strlen' gives the length of the string and
    > not necessarily the allocated size of the array.


    Right. The point is that there is no way to determine after the fact
    how much memory was allocated. Another 'subtle' point: 'malloc()'
    is required to attempt to allocate *at least* the requested size.
    It is allowed to (and often does) allocate more (but the program
    must only access within bounds of the requested size).

    > That's why I said "possible
    > exception".


    In standard C, no exceptions.

    -Mike
    Mike Wahler, Nov 9, 2004
    #12
    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. Derek
    Replies:
    7
    Views:
    24,324
    Ron Natalie
    Oct 14, 2004
  2. Trevor

    sizeof(str) or sizeof(str) - 1 ?

    Trevor, Apr 3, 2004, in forum: C Programming
    Replies:
    9
    Views:
    626
    CBFalconer
    Apr 10, 2004
  3. Vinu
    Replies:
    13
    Views:
    1,414
    Lawrence Kirby
    May 12, 2005
  4. blufox

    sizeof( int ) != sizeof( void * )

    blufox, May 22, 2006, in forum: C Programming
    Replies:
    2
    Views:
    554
    Joe Smith
    May 22, 2006
  5. Replies:
    46
    Views:
    644
    Shao Miller
    Jan 14, 2013
Loading...

Share This Page