sizeof dynamic structure array

Discussion in 'C Programming' started by cerr, Sep 23, 2009.

  1. cerr

    cerr Guest

    Hi There,

    Let's say I have a structure array that changes its size (elements in
    array) with realloc().
    Is there a possibility for me to read how much memory is currently
    allocated and how many elements are available or do i need to carry
    the "number of elements" in a global variable forward?

    Thank you,
    Ron
    cerr, Sep 23, 2009
    #1
    1. Advertising

  2. cerr <> wrote:
    > Let's say I have a structure array that changes its size
    > (elements in array) with realloc().
    > Is there a possibility for me to read how much memory is
    > currently allocated and how many elements are available


    http://c-faq.com/malloc/querysize.html

    > or do i need to carry the "number of elements" in a
    > global variable forward?


    Put it in the struct it pertains to.

    --
    Peter
    Peter Nilsson, Sep 23, 2009
    #2
    1. Advertising

  3. cerr

    Seebs Guest

    On 2009-09-23, cerr <> wrote:
    > Let's say I have a structure array that changes its size (elements in
    > array) with realloc().
    > Is there a possibility for me to read how much memory is currently
    > allocated and how many elements are available or do i need to carry
    > the "number of elements" in a global variable forward?


    You can't query how much is in use or how much is being used in general,
    but I would recommend strongly against a global variable. You might consider
    a struct:

    struct many_foo {
    int size;
    int count;
    foo *foos;
    };

    -s
    --
    Copyright 2009, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    Seebs, Sep 23, 2009
    #3
  4. cerr

    Scooser Guest

    Hi,

    if you don't like the layer of indirection you can also do it this way

    struct many_foo {
    size_t max;
    size_t count;
    foo foos[]
    };

    many_foo * ptr_to_foo = malloc ( sizeof (*ptr_to_foo ) + max *
    sizeof (foo) );
    if (ptr_to_foo)
    {
    ptr_to_foo->max = max;
    ptr_to_foo->count = 0;
    }

    If you have to use a C90 compiler you'll have to a hack - violating
    the standard - and add an array with one dummy object to the struct

    struct many_foo {
    size_t max;
    size_t count;
    foo foos[1]
    };

    many_foo * ptr_to_foo = malloc ( sizeof (*ptr_to_foo ) + (max - 1)
    * sizeof (foo) );
    if (ptr_to_foo)
    {
    ptr_to_foo->max = max;
    ptr_to_foo->count = 0;
    }


    Scooser
    Scooser, Sep 23, 2009
    #4
  5. Scooser <> writes:

    > if you don't like the layer of indirection you can also do it this way
    >
    > struct many_foo {
    > size_t max;
    > size_t count;
    > foo foos[]
    > };
    >
    > many_foo * ptr_to_foo = malloc ( sizeof (*ptr_to_foo ) + max *
    > sizeof (foo) );


    Small suggestion. You obviously like the

    T *tp = malloc(sizeof *tp);

    idiom so why not extend it to avoid accidental use of the wrong type
    for the foos array?

    many_foo *ptr_to_foo = malloc(sizeof *ptr_to_foo +
    max * sizeof ptr_to_foo->foos[0]);

    You don't get the same instant checkability (because the name of
    flexible member is not evident in the declaration) but I think you get
    a little more robustness to change this way.

    <snip>
    --
    Ben.
    Ben Bacarisse, Sep 23, 2009
    #5
    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,302
    Ron Natalie
    Oct 14, 2004
  2. Trevor

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

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

    sizeof( int ) != sizeof( void * )

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

Share This Page