sizeof(struct something)??

Discussion in 'C Programming' started by Zero, Apr 27, 2004.

  1. Zero

    Zero Guest

    If we have a structure like:

    struct something{
    int *a;
    int b;
    };

    We allocate mempry for a using malloc or calloc. The question is when
    we want to know the size of the structure, sizeof(struct something),
    it will not give us the correct number (i.e. the size of the structure
    + the size of the meory allacated). How do we get the "correct" size
    of the structure?
    Zero, Apr 27, 2004
    #1
    1. Advertising

  2. Zero

    Richard Bos Guest

    (Zero) wrote:

    > If we have a structure like:
    >
    > struct something{
    > int *a;
    > int b;
    > };
    >
    > We allocate mempry for a using malloc or calloc. The question is when
    > we want to know the size of the structure, sizeof(struct something),
    > it will not give us the correct number (i.e. the size of the structure
    > + the size of the meory allacated).


    Yes, it will. The size of the struct is sizeof(struct something). The
    size of the memory somethingstruct.a will be pointing at is a different
    matter entirely, and cannot be determined by the compiler for the
    obvious reason that _you_ are the one who decides how large that memory
    should be.

    > How do we get the "correct" size of the structure?


    I don't know how to get the "correct" size, but to get the actually
    correct size, use sizeof(struct something). Then decide how large the
    block of memory for this particular somethingstruct.a should be, and do
    a second malloc().
    Do not try to do this with only one malloc() call for both struct and
    pointed-at memory; that would be begging for alignment problems.

    Richard
    Richard Bos, Apr 27, 2004
    #2
    1. Advertising

  3. Zero wrote:
    > If we have a structure like:
    >
    > struct something{
    > int *a;
    > int b;
    > };
    >
    > We allocate mempry for a using malloc or calloc. The question is when
    > we want to know the size of the structure, sizeof(struct something),
    > it will not give us the correct number (i.e. the size of the structure
    > + the size of the meory allacated). How do we get the "correct" size
    > of the structure?


    By remembering the size of the memory a is pointing to.

    struct something{
    int *a;
    unsigned num_ints;
    /*or
    size_t num_bytes;
    */
    int b;
    };

    --
    Thomas.
    Thomas Stegen, Apr 27, 2004
    #3
  4. Zero <> scribbled the following:
    > If we have a structure like:


    > struct something{
    > int *a;
    > int b;
    > };


    > We allocate mempry for a using malloc or calloc. The question is when
    > we want to know the size of the structure, sizeof(struct something),
    > it will not give us the correct number (i.e. the size of the structure
    > + the size of the meory allacated). How do we get the "correct" size
    > of the structure?


    Do you want to know how much allocated memory the member "a" points to?
    There is no standard way to know, other than keeping track of it
    yourself. Either keep track of all malloc() and calloc() calls, try to
    find a system-specific way, or rethink your design.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "I am looking for myself. Have you seen me somewhere?"
    - Anon
    Joona I Palaste, Apr 27, 2004
    #4
  5. Zero

    Al Bowers Guest

    Zero wrote:
    > If we have a structure like:
    >
    > struct something{
    > int *a;
    > int b;
    > };
    >
    > We allocate mempry for a using malloc or calloc. The question is when
    > we want to know the size of the structure, sizeof(struct something),
    > it will not give us the correct number (i.e. the size of the structure
    > + the size of the meory allacated). How do we get the "correct" size
    > of the structure?


    I think you really want the correct size or count of the allocations,
    not the total structure size.

    You will need to to keep track of allocations you make for the pointer
    a. I find it best to put a counter along with the pointer to the
    array in a datatype. To access the data members you would use functions
    that will increment the counter on allocations and decrement the counter
    on deallocations. Using only the functions to modify the datatype will
    help insure that the count will not get out of sync with the actual
    allocations.

    Example:

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

    #define MAXNAME 30

    typedef struct NAMES
    {
    char (*name)[MAXNAME+1];
    size_t num_names;
    }NAMES;

    char *addNAME(NAMES *p, const char *name);
    void freeNAMES(NAMES *p);

    int main(void)
    {
    NAMES president = {NULL};
    size_t i;

    addNAME(&president,"George Washington");
    addNAME(&president,"Bill Clinton");
    addNAME(&president,"George Bush");
    for(i = 0;i < president.num_names;i++)
    printf("president.name[%u] = %s\n",i,president.name);
    printf("The size of the array is: %u\n",
    (sizeof *president.name)*president.num_names);
    freeNAMES(&president);
    printf("After deallocation, the array size is %u\n",
    (sizeof *president.name)*president.num_names);
    return 0;
    }

    char *addNAME(NAMES *p, const char *name)
    {
    char (*tmp)[MAXNAME+1];

    tmp = realloc(p->name,(p->num_names+1)*(sizeof *tmp));
    if(tmp == NULL) return NULL;
    p->name = tmp;
    strncpy(p->name[p->num_names],name,MAXNAME);
    p->name[p->num_names][MAXNAME] = '\0';
    return tmp[p->num_names++];
    }

    void freeNAMES(NAMES *p)
    {
    free(p->name);
    p->name = NULL;
    p->num_names = 0;
    }





    --
    Al Bowers
    Tampa, Fl USA
    mailto: (remove the x to send email)
    http://www.geocities.com/abowers822/
    Al Bowers, Apr 27, 2004
    #5
  6. Zero

    macluvitch Guest

    To get the correct size of a given structure beware with word alignement

    ie:

    struct foo{
    int a;
    char c;
    int b;
    }oof;

    suppose the integers are 4 bits and char 1 bit;
    a->4 bits
    c->1 bit
    word qlignement -> 3 bits
    b -> 4 bits

    so the total size is 1+4+3+4 = 12
    macluvitch, Apr 30, 2004
    #6
  7. Zero

    xarax Guest

    change "bits" to "bytes". change "alignement" to "alignment".

    "macluvitch" <> wrote in message
    news:...
    > To get the correct size of a given structure beware with word alignement
    >
    > ie:
    >
    > struct foo{
    > int a;
    > char c;
    > int b;
    > }oof;
    >
    > suppose the integers are 4 bits and char 1 bit;
    > a->4 bits
    > c->1 bit
    > word qlignement -> 3 bits
    > b -> 4 bits
    >
    > so the total size is 1+4+3+4 = 12
    >
    >
    >
    >
    >
    >
    xarax, Apr 30, 2004
    #7
  8. Zero

    macluvitch Guest

    Tanc u fulk
    macluvitch, May 2, 2004
    #8
    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,307
    Ron Natalie
    Oct 14, 2004
  2. Trevor

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

    Trevor, Apr 3, 2004, in forum: C Programming
    Replies:
    9
    Views:
    617
    CBFalconer
    Apr 10, 2004
  3. Chris Fogelklou
    Replies:
    36
    Views:
    1,358
    Chris Fogelklou
    Apr 20, 2004
  4. Vinu
    Replies:
    13
    Views:
    1,393
    Lawrence Kirby
    May 12, 2005
  5. blufox

    sizeof( int ) != sizeof( void * )

    blufox, May 22, 2006, in forum: C Programming
    Replies:
    2
    Views:
    545
    Joe Smith
    May 22, 2006
Loading...

Share This Page