sizeof(struct something)??

Z

Zero

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?
 
R

Richard Bos

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
 
T

Thomas Stegen

Zero said:
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;
};
 
J

Joona I Palaste

Zero said:
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.
 
A

Al Bowers

Zero said:
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;
}
 
M

macluvitch

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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top