obtaining the size of a structure

P

pete

kris said:
Hi I am writing a small program where I need to obtain the actual size
of a structure.
The programm is as follows

struct abc
{
int j;
char k;
int i;
}*a;

main()
{
a->j=10;

printf("size of structure is %d",sizeof(a));
}

In this program when I print the size of structure I get the whole
size (i.e size of 2 int variables+size of char variable).

But I need to get the size of structure such that it resembles the
chunk of memory that is actually being used.


/* BEGIN new.c */

#include <stdio.h>

struct abc {
int j;
char k;
int i;
} *a;

int main(void)
{
printf("size of structure is %u.\n",
(unsigned)sizeof (struct abc));
printf("size of structure is %d.\n",
(int)((char *)(a + 1) - (char *)a));
return 0;
}

/* END new.c */
 

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

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top