Sizeof operator

  • Thread starter news.fe.internet.bosch.com
  • Start date
N

news.fe.internet.bosch.com

Hi All ,

I u find out size of struct , does it considers paddding chars into
consideration

struct A
{
char c;
int i;
};

struct B
{
int i;
char c;
}

we see that for alignment some bits are added in case of struct A, but not
in case of struct B
A a ;
sizeof(a);
B b;
sizeof(b);

Will this be same in both cases

TIA

Mohan
does it give
 
A

Alexei A. Frounze

news.fe.internet.bosch.com said:
Hi All ,

I u find out size of struct , does it considers paddding chars into
consideration

struct A
{
char c;
int i;
};

struct B
{
int i;
char c;
}

we see that for alignment some bits are added in case of struct A, but not
in case of struct B

I know a compiler that would pad both (although on that particular target
the types used in the above structs are char=shor=int and long). That's
because longs *must* be aligned on even address (sizeof(long)==2) in memory,
if they aren't you just don't get the right value at the right address. The
reason for aligning B is that you may make an array of struct B, where each
element must also be aligned, hence padding in both cases.

Alex
 
B

Barry Schwarz

Hi All ,

I u find out size of struct , does it considers paddding chars into
consideration

Yes, the sizeof operator will return the complete size of the struct,
including whatever padding the compiler chooses to include.
struct A
{
char c;
int i;
};

struct B
{
int i;
char c;
}

we see that for alignment some bits are added in case of struct A, but not
in case of struct B

How do you see this?

On a typical system with 1 byte char and 4 byte aligned int, the size
of each struct should be 8.

In struct A, three alignment (padding) bytes are added after c to
align the integer on a 4 byte boundary and the struct itself will also
be aligned on at least a 4 byte boundary.

In struct B, the three alignment bytes are added after c so that
should you have an array of such structures, the second structure
(which needs 4 byte alignment) will immediately follow the first
structure.
A a ;
sizeof(a);
B b;
sizeof(b);

Will this be same in both cases

While 8 would satisfy both, there is nothing prohibiting the compiler
from making one 12 and one 16.



<<Remove the del for email>>
 
M

Michael Mair

news.fe.internet.bosch.com said:
Hi All ,

I u find out size of struct , does it considers paddding chars into
consideration

struct A
{
char c;
int i;
};

struct B
{
int i;
char c;
}

we see that for alignment some bits are added in case of struct A, but not
in case of struct B
A a ;
sizeof(a);

We are in comp.lang.c. The above will not compile.
If you are using a C++ compiler to use C, then be aware that
C and C++ are different languages with different semantics.
As the semantics do not differ everywhere, you may mistakenly
assume this is not so.
In C, you have to write
struct A a;
to declare a of type struct A.

Note: You can write "sizeof a" and "sizeof (struct A)"; the
parens are not necessary for variables.
B b;
sizeof(b);

As soon as you want an array of struct B elements, you
need the "i"s properly aligned, so effectively you have
the same situation.

There are no guarantees, though; the compiler may pad as it
likes.

If you take more struct members into account, though,
the result may very well be different:
struct C {
char c;
int i;
char d;
int j;
short k;
char e;
int l;
unsigned char f;
unsigned short m;
} sc;
struct D {
int i;
int j;
int l;
unsigned short m;
short k;
char c;
char d;
char e;
unsigned char f;
} sd;

In sd, members of the same type are grouped together, and the different
types are ordered by size (large to small -- as far as we know it).
This gives a much better chance to avoid excess padding and may result
in
sizeof sd < sizeof sc

Cheers
Michael
 
S

santosh

My outlook is hving some problem. I am testing whether I am able to send
mail to news group.
 
S

santosh

Hello,
I have a fundamental doubt regarding alignment of data when it is stored in
memory.
I understand the data alignment happens because memory is word addressable.
That means when the system accesses any memory location the address should
be divisible by word size (typically 4bytes).
So in this case when suppose I want to see the memory location of the
variable sd.e ( &(sd.e)) , it is refereing to address location which is not
divisible by 4 in a SOLARISH machine(V 5.8).

Can anyone explain why this alignment happens what are benifits etc...?
 
F

Flash Gordon

santosh said:
My outlook is hving some problem. I am testing whether I am able to send
mail to news group.

Post below the text you are replying to, not above.

Don't post test messages to normal groups, look for groups with test in
the name such as alt.test

Don't repost within minutes because you post has not appeared. It can
take hours for a post to appear.

Don't post as a reply to an existing message when you are not replying
to that message.
 
F

Flash Gordon

santosh wrote:

This should not have been a reply to an existing message, you should
have created a new post. You message has nothing to do with the subject
line and is completely unrelated to the post you replied to. Even OE can
do this.
Hello,
I have a fundamental doubt regarding alignment of data when it is stored in
memory.

Alignment issues are entirely implementation specific. On some systems
there are NO alignment restrictions.
I understand the data alignment happens because memory is word addressable.

Or for any other reason. Such as a double only being able to be read by
a floating point processor if it is aligned on a double word boundary.
Or because the person who wrote the implementation had shares in a chip
manufacturer and wanted to increase sales of memory chips.
That means when the system accesses any memory location the address should
be divisible by word size (typically 4bytes).
So in this case when suppose I want to see the memory location of the
variable sd.e ( &(sd.e)) , it is refereing to address location which is not
divisible by 4 in a SOLARISH machine(V 5.8).

It may be, it may not.
Can anyone explain why this alignment happens what are benifits etc...?

Sometimes because the processor can only access a particular data type
with a specific alignment. Sometimes because it is inefficient to access
non-aligned data. If you want to know why a specific alignment is used
on a specific system ask on a group dedicated to that system.
 

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
474,169
Messages
2,570,915
Members
47,456
Latest member
JavierWalp

Latest Threads

Top