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. Advertisements

  2. Zero

    Richard Bos Guest

    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.
    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. Advertisements

  3. 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 Stegen, Apr 27, 2004
    #3
  4. 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 I Palaste, Apr 27, 2004
    #4
  5. Zero

    Al Bowers Guest

    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, 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".
     
    xarax, Apr 30, 2004
    #7
  8. Zero

    macluvitch Guest

    Tanc u fulk
     
    macluvitch, May 2, 2004
    #8
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.