473,796 Members | 2,661 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2118
"news.fe.intern et.bosch.com" <mo**********@i n.bosch.com> wrote in message
news:dc******** **@ns2.fe.inter net.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.intern et.bosch.com"
<mo**********@i n.bosch.com> wrote:
Hi All ,

I u find out size of struct , does it considers paddding chars into
consideratio n
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.interne t.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.inter net.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

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

Similar topics

70
8916
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 expression should equal to "sizeof( (int)(*p) )", but the compiler does NOT think so. Why? Can anyone help me? Thanks. Best regards. Roy
31
2391
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
1939
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
1889
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 assume that the interpreter does conform to the range values for, say, type int, but allocates storage for the variables based on their value. So, for two variables foo and bar int foo = 0; /* interpreter allocates two bytes */ int bar =...
12
2422
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
2248
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 ------ #include <iostream> using namespace std;
43
781
by: Richard | last post by:
Could someone point me to why "sizeof x" is/isnt preferable to "sizeof(x)",
28
6759
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 in general be done with one argument functions? Why would one want to do it anyway, other than showing that one knows and to save two characters?
36
2816
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
6062
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. TIA, Divick
0
9684
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9530
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10236
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10182
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10017
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9055
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6793
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3734
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2928
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.