473,396 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Sizeof operator

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
Nov 15 '05 #1
12 2090
"news.fe.internet.bosch.com" <mo**********@in.bosch.com> wrote in message
news:dc**********@ns2.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


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
Nov 15 '05 #2
On Thu, 4 Aug 2005 11:36:47 +0530, "news.fe.internet.bosch.com"
<mo**********@in.bosch.com> wrote:
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>>
Nov 15 '05 #3
news.fe.internet.bosch.com wrote:
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
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 15 '05 #4
test
Nov 15 '05 #5
test
Nov 15 '05 #6
test
Nov 15 '05 #7
test mail
Nov 15 '05 #8
My outlook is hving some problem. I am testing whether I am able to send
mail to news group.
"santosh" <sa*************@in.bosch.com> wrote in message
news:dc**********@ns2.fe.internet.bosch.com...
test mail

Nov 15 '05 #9
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...?
Nov 15 '05 #10
santosh wrote:
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.
"santosh" <sa*************@in.bosch.com> wrote in message
news:dc**********@ns2.fe.internet.bosch.com...
test mail


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.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 15 '05 #11
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.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 15 '05 #12
"santosh" <sa*************@in.bosch.com> writes:
My outlook is hving some problem. I am testing whether I am able to send
mail to news group.


There are test newsgroups for that.
--
Ben Pfaff
email: bl*@cs.stanford.edu
web: http://benpfaff.org
Nov 15 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

70
by: Roy Yao | last post by:
Does it mean "(sizeof(int))* (p)" or "sizeof( (int)(*p) )" ? According to my analysis, operator sizeof, (type) and * have the same precedence, and they combine from right to left. Then this...
31
by: Anjali M | last post by:
#include <stdio.h> int main() { int number = 10; printf("Number is: %d\n", number); printf("Sizeof of number is: %d\n", sizeof(number++)); printf("Number now is: %d\n", number);
7
by: dam_fool_2003 | last post by:
#include<stdio.h> int main(void) { unsigned int a=20,b=50, c = sizeof b+a; printf("%d\n",c); return 0; } out put: 24
12
by: ozbear | last post by:
If one were writing a C interpreter, is there anything in the standard standard that requires the sizeof operator to yield the same value for two different variables of the same type? Let's...
12
by: sonu | last post by:
#include<stdio.h> main() { int x=10,y; y=sizeof(++x); printf("x=%d\ny=%d\n",x,y); } Oput Put
15
by: Alex Vinokur | last post by:
Why does one need to use two kinds of sizeof operator: * sizeof unary-expression, * sizeof (type-name) ? Their behavior seem not to be different (see an example below). ------ C++ code...
43
by: Richard | last post by:
Could someone point me to why "sizeof x" is/isnt preferable to "sizeof(x)",
28
by: Howard Bryce | last post by:
I have come across code containing things like sizeof int How come that one can invoke sizeof without any parentheses surrounding its argument? Is this admissible within the standard? Can it...
36
by: Frederick Gotham | last post by:
sizeof has the same precedence as a cast, and they both bind from right to left. The following won't compile for me with gcc: int main(void) { sizeof(double)5; return 0; }
4
by: Divick | last post by:
Hi, does sizeof operator evaluate the size at compile time or run time ? In other words is sizeof(SomeType) evaluated at compile time or at runtime? Please provide the reasoning involved as well....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.