obtaining the size of a structure

K

kris

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.

Thanks
Krish
 
F

Fred Kleinschmidt

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));

This is the size of a pointer to the structure.
'a' is of type 'struct abc *'
}

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.

try sizeof(*a)

Why do you need this?

Note that the actual size of this
structure is implementation dependent; the compiler may
add padding between j and k, and between k and i, and
even after i. Different compilers may add different
amounts of padding.
 
J

jacob navia

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.

Thanks
Krish

You have to tell your compiler to not align anything. Different
compilers use different alignment directives.

Just read your compiler documentation.
 
M

Mark Bluemel

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

Standard headers such as said:
struct abc
{
int j;
char k;
int i;
}*a;

main()

I'd prefer "int main(void)" and I think it may be required in C99
{
a->j=10;

using the value of a uninitialized pointer. Bad dog! No biscuit!
printf("size of structure is %d",sizeof(a));

That tells you the size of the pointer. "sizeof(*a)" would tell you the
size of the structure.

No "return" statement is poor style.
 
N

Nikos Chantziaras

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.

(That program won't work, but I assume it's just a copy&paste error?)

First, sizeof(a) will always return the number of bytes occupied by
pointers (since "a" is a pointer), which should be 4 on most 32-bit systems.

Second, the size of the struct is "sizeof(struct abc)". This is the
same as "sizeof(*a)", assuming you allocated the space for "a" with:

a = malloc(sizeof(struct abc));

The size of the struct is always the same. I don't understand what you
mean with "I need to get the size of structure such that it resembles
the chuck of memory that is actually being used".
 
M

Mark Bluemel

Second, the size of the struct is "sizeof(struct abc)". This is the
same as "sizeof(*a)", assuming you allocated the space for "a" with:

a = malloc(sizeof(struct abc));

Nope - sizeof(*a) will return the sizeof the structure, even for an
uninitialized or NULL pointer...
 
S

santosh

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;

a is a pointer of type struct abc*.

As per the C Standard main must return an int. So declare it to do so.

int main(void)
{
a->j=10;

a is a pointer. You need to initialise it to point to a valid structure abc
object. Do;

struct abc foo;
a = &foo;
a->j = 10;
printf("size of structure is %d",sizeof(a));

The d format specifier is for an int argument. Use either lu or zu for a
size-t argument, which is the return type of the sizeof operator.

To calculate the size of the structure do:
sizeof *a;

Note the lack of parenthesis.
}

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

No you don't. You get the size of the pointer a.
But I need to get the size of structure such that it resembles the
chunk of memory that is actually being used.

Do as above.
 
A

Army1987

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()
In C99, you need to specify the return type (int).
{
a->j=10;

printf("size of structure is %d",sizeof(a));
sizeof returns a size_t, which cannot be an int (as expected by
%d). See www.c-faq.com, question 12.9b.

In C89, you need to explicitly return a value (0 to indicate
success).
}

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).
No, you get the size of a pointer to it.
But I need to get the size of structure such that it resembles the
chunk of memory that is actually being used.
sizeof *a, i.e. sizeof(struct abc) is required to include the
padding.
(a is a pointer to struct abc, so *a is a struct abc.)
 
A

Army1987

(That program won't work, but I assume it's just a copy&paste error?)

First, sizeof(a) will always return the number of bytes occupied by
pointers (since "a" is a pointer), which should be 4 on most 32-bit systems.

Second, the size of the struct is "sizeof(struct abc)". This is the
same as "sizeof(*a)", assuming you allocated the space for "a" with:

a = malloc(sizeof(struct abc));

The size of the struct is always the same. I don't understand what you
mean with "I need to get the size of structure such that it resembles
the chuck of memory that is actually being used".
I think he means the size of the struct including the padding,
rather than just the sum of the sizes of its members.
 
S

santosh

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.

Don't post multiple times. Usenet, to which Google Groups interfaces, can
sometimes be slow. Wait for a few hours before reposting. See you earlier
post for replies.
 
A

Army1987

kris wrote: [snip]
But I need to get the size of structure such that it resembles the
chunk of memory that is actually being used.

You have to tell your compiler to not align anything. Different
compilers use different alignment directives.

Just read your compiler documentation.
He just asked how to know the size of the struct including the
padding, not how to force the padding to be 0. Also, a compiler
isn't required to let you specify the alignment.
 
J

jacob navia

Army1987 said:
kris wrote: [snip]
But I need to get the size of structure such that it resembles the
chunk of memory that is actually being used.
You have to tell your compiler to not align anything. Different
compilers use different alignment directives.

Just read your compiler documentation.
He just asked how to know the size of the struct including the
padding, not how to force the padding to be 0.

Well I think he asked for no padding. The OP only can answer this

Also, a compiler
isn't required to let you specify the alignment.


Ahh that is news to me. How would you do it then?

(just curious how you do it without a compiler)
 
S

santosh

jacob said:
Army1987 said:
kris wrote: [snip]
But I need to get the size of structure such that it resembles the
chunk of memory that is actually being used.
You have to tell your compiler to not align anything. Different
compilers use different alignment directives.

Just read your compiler documentation.
He just asked how to know the size of the struct including the
padding, not how to force the padding to be 0.

Well I think he asked for no padding. The OP only can answer this

Also, a compiler
isn't required to let you specify the alignment.


Ahh that is news to me. How would you do it then?

(just curious how you do it without a compiler)

He probably means that the C Standard does not require implementations to
allow the user to specify alignment for structure members.
 
J

JT

Army1987 said:
Also, a compiler
isn't required to let you specify the alignment.

Ahh that is news to me. How would you do it then?
(just curious how you do it without a compiler)

I'm sure he meant
"a compiler doesn't have to let you specify the alignment", i.e.
"a compiler isn't required to let you specify the alignment".

And he would be right.
 
J

jacob navia

JT said:
I'm sure he meant
"a compiler doesn't have to let you specify the alignment", i.e.
"a compiler isn't required to let you specify the alignment".

And he would be right.

Ahh

OK, true.

I just did not understand the sentence
 
M

Martin Ambuhl

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;

No space has been allocated for a to point to. Dereferencing a pointer
is meaningless, and a nice implementation would generate a runtime error.
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).

You should never get to the printf(), the program already having blown up.
But I need to get the size of structure such that it resembles the
chunk of memory that is actually being used.

You allocated no memory to actually be used. There is no "chunk of
memory that is actually being used." Assuming you had allocated such
memory, your question would be meaningless. The sizeof an object is
(not merely "resembles") the size of the chunk of memory used for it.
 
A

Army1987

I am ashamed to confess that I understood that no compiler
is required !!!

PARSE ERROR!

:)
Now I get it. "You don't need a compiler in order to specify the
alignment." I would have never realized that my sentence could
mean *that* by myself...
 
J

Jack Klein

I'd prefer "int main(void)" and I think it may be required in C99

The void in the parentheses is not, if you don't need the definition
to also serve as a prototype. Implicit int is gone, so either:

int main()

....or:

int main(void)

....are acceptable.
using the value of a uninitialized pointer. Bad dog! No biscuit!

No, a is most certainly initialized. It's a null pointer.
That tells you the size of the pointer. "sizeof(*a)" would tell you the
size of the structure.


No "return" statement is poor style.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 

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,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top