Velocity Reviews > sizeof(struct something)??

# sizeof(struct something)??

Zero
Guest
Posts: n/a

 04-27-2004
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?

Richard Bos
Guest
Posts: n/a

 04-27-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (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

Thomas Stegen
Guest
Posts: n/a

 04-27-2004
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.

Joona I Palaste
Guest
Posts: n/a

 04-27-2004
Zero <(E-Mail Removed)> 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 ((E-Mail Removed)) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"I am looking for myself. Have you seen me somewhere?"
- Anon

Al Bowers
Guest
Posts: n/a

 04-27-2004

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;

for(i = 0;i < president.num_names;i++)
printf("president.name[%u] = %s\n",i,president.name[i]);
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: (E-Mail Removed) (remove the x to send email)
http://www.geocities.com/abowers822/

macluvitch
Guest
Posts: n/a

 04-30-2004
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
Guest
Posts: n/a

 04-30-2004
change "bits" to "bytes". change "alignement" to "alignment".

"macluvitch" <(E-Mail Removed)> wrote in message
> 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
Guest
Posts: n/a

 05-02-2004
Tanc u fulk