473,698 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sizeof()

For the structure below:

struct A{
int a;
double b;
char c;
};

struct A sa;
printf("sizeof( A): %d\n", sizeof(sa));

The output is
sizeof(A): 16

Why it is 16?

Thanks.

Jack

Jul 19 '06 #1
17 2179

Jack wrote:
For the structure below:

struct A{
int a;
double b;
char c;
};

struct A sa;
printf("sizeof( A): %d\n", sizeof(sa));

The output is
sizeof(A): 16

Why it is 16?
why not?

Tom

Jul 19 '06 #2
Jack wrote:
For the structure below:

struct A{
int a;
double b;
char c;
};

struct A sa;
printf("sizeof( A): %d\n", sizeof(sa));

The output is
sizeof(A): 16

Why it is 16?
Read the FAQ, <http://c-faq.com/>, question 2.13.

Robert Gamble

Jul 19 '06 #3
In article <11************ *********@i3g20 00cwc.googlegro ups.com>,
Jack <ju******@gmail .comwrote:
>For the structure below:
>struct A{
int a;
double b;
char c;
};
struct A sa;
printf("sizeof( A): %d\n", sizeof(sa));
>The output is
sizeof(A): 16
>Why it is 16?
That implementation probably uses 4 bytes for int, 8 bytes for double,
1 byte for char. But implementations are required to pad structures so
that the size of the structure is a multiple of the architectural
alignment restrictions (often 4 bytes), so the structure likely gets
padded by adding 3 unused bytes.

If the structure were not padded, then if you had

struct A sa2[2];

then the address of sa2[1] would be 13 bytes after the start of sa2[0]
and you would be asking the system to read an int from an odd byte
address. A lot of systems cannot do that, or can only do that by using
special trap handlers, or can only do it by using special
"load unaligned" instructions that are often very slow.
--
"law -- it's a commodity"
-- Andrew Ryan (The Globe and Mail, 2005/11/26)
Jul 19 '06 #4
"Jack" <ju******@gmail .comwrote:
>For the structure below:

struct A{
int a;
double b;
char c;
};

struct A sa;
printf("sizeof( A): %d\n", sizeof(sa));

The output is
sizeof(A): 16

Why it is 16?
Probably because sizeof(int) is 4, sizeof(double) is 8, sizeof(char)
is 1 (of course,) and 3 chars of padding are added to guarantee proper
alignment.

But it could also be that in your machine sizeof(int) is 1,
sizeof(double) is 14 and no padding is necessary.

Or maybe sizeof(int) is 7, followed by one char padding,
sizeof(double) is 5, followed by one char padding, plus the size of c
and one last char of padding for alignment.

Or any other valid combination adding up to 16.

Check your compiler's documentation for the sizes of the primitive
data types and alignment requirements.

Roberto Waltman

[ Please reply to the group,
return address is invalid ]
Jul 19 '06 #5
Tom St Denis wrote:
Jack wrote:
>For the structure below:

struct A{
int a;
double b;
char c;
};

struct A sa;
printf("sizeof( A): %d\n", sizeof(sa));

The output is
sizeof(A): 16

Why it is 16?

why not?

Tom
What a useless answer.

Why not just tell him there is padding?
Jul 20 '06 #6
On 19 Jul 2006 08:58:01 -0700, "Robert Gamble" <rg*******@gmai l.com>
wrote:
>Jack wrote:
>For the structure below:

struct A{
int a;
double b;
char c;
};

struct A sa;
printf("sizeof( A): %d\n", sizeof(sa));

The output is
sizeof(A): 16

Why it is 16?

Read the FAQ, <http://c-faq.com/>, question 2.13.
which says:

Q. Why does sizeof report a larger size than I expect for a structure
type, as if there were padding at the end?

A. Padding at the end of a structure may be necessary to preserve
alignment when an array of contiguous structures is allocated. Even
when the structure is not part of an array, the padding remains, so
that sizeof can always return a consistent size. See also question
2.12.

References: H&S Sec. 5.6.7 pp. 139-40

Best regards
--
jay

NOTE: The C FAQ book has the same contents as the online C FAQ and
more. The online version is great and the book is even better. Find
out more here:

http://c-faq.com/book/
Jul 20 '06 #7

Walter Roberson wrote:
In article <11************ *********@i3g20 00cwc.googlegro ups.com>,
Jack <ju******@gmail .comwrote:
For the structure below:
struct A{
int a;
double b;
char c;
};
struct A sa;
printf("sizeof( A): %d\n", sizeof(sa));
The output is
sizeof(A): 16
Why it is 16?

That implementation probably uses 4 bytes for int, 8 bytes for double,
1 byte for char. But implementations are required to pad structures so
that the size of the structure is a multiple of the architectural
alignment restrictions (often 4 bytes), so the structure likely gets
padded by adding 3 unused bytes.

If the structure were not padded, then if you had

struct A sa2[2];

then the address of sa2[1] would be 13 bytes after the start of sa2[0]
and you would be asking the system to read an int from an odd byte
address. A lot of systems cannot do that, or can only do that by using
special trap handlers, or can only do it by using special
"load unaligned" instructions that are often very slow.
--
"law -- it's a commodity"
-- Andrew Ryan (The Globe and Mail, 2005/11/26)
What does it mean that system cannot read odd byte address ?
Is this due to the concept of word length, which is the basic length of
a memory address.
If such is the case, then how is memory allocated for following case.

Consider that word length is 4. For the struct,

struct a
{
char c;
};
int main()
{
struct a b[10];
printf ("size = %d\n",sizeof(b) );
}

As per the discussion, should the answer be 40 (3 bytes padded for
evrey 1 char)????
so that there is not need for reading odd byte address?
Correct me if this not true.

Sarathy

Jul 20 '06 #8
In article <11************ **********@s13g 2000cwa.googleg roups.com>,
sarathy <sp*********@gm ail.comwrote:
>and you would be asking the system to read an int from an odd byte
address. A lot of systems cannot do that
>What does it mean that system cannot read odd byte address ?
He said read *an int* from an odd byte address. Many modern machines
can read a single byte from any location, but a 2-byte word only from
even addresses, a 4-byte word only from multiple-of-4 addresses, and
so on.
>Consider that word length is 4. For the struct,

struct a
{
char c;
};
int main()
{
struct a b[10];
printf ("size = %d\n",sizeof(b) );
}

As per the discussion, should the answer be 40 (3 bytes padded for
evrey 1 char)????
In this case, you never need to read anything bigger than char, so alignment
isn't likely to be a problem. sizeof(b) will probably be 10.

-- Richard
Jul 20 '06 #9

"Richard Tobin" <ri*****@cogsci .ed.ac.ukha scritto nel messaggio
news:e9******** ***@pc-news.cogsci.ed. ac.uk...
In article <11************ **********@s13g 2000cwa.googleg roups.com>,
sarathy <sp*********@gm ail.comwrote:
and you would be asking the system to read an int from an odd byte
address. A lot of systems cannot do that
What does it mean that system cannot read odd byte address ?

He said read *an int* from an odd byte address. Many modern machines
can read a single byte from any location, but a 2-byte word only from
even addresses, a 4-byte word only from multiple-of-4 addresses, and
so on.
Consider that word length is 4. For the struct,

struct a
{
char c;
};
int main()
{
struct a b[10];
printf ("size = %d\n",sizeof(b) );
}
No.

sizeof(b) is of type 'size_t'.
It can be bigger than 'int'.
#include <stdio.h>
#include <stdlib.h>

struct a
{
char c;
};

int main(void)
{
struct a b[10];

/* I suppose sizeof(b) <= INT_MAX :-) */
printf ("size = %d\n",(int)size of(b));

return EXIT_SUCCESS;
}

In C99 you can use also %zu.

Giorgio Silvestri


Jul 20 '06 #10

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

Similar topics

3
3549
by: Sunil Menon | last post by:
Dear All, A class having no member variables and only a method sizeof(object) will return 1byte in ANSI and two bytes in Unicode. I have the answer for this of how in works in ANSI. But I don't know it returns two bytes in UniCode. Please help... For ANSI: In ISO/ANSI C++ Standard, 5.3.3 § 1, it stays: "The sizeof operator yields the number of bytes in the object representation of its
2
2459
by: Xiangliang Meng | last post by:
Hi, all. What will we get from sizeof(a class without data members and virtual functions)? For example: class abnormity { public: string name() { return "abnormity"; }
19
9227
by: Martin Pohlack | last post by:
Hi, I have a funtion which shall compute the amount for a later malloc. In this function I need the sizes of some struct members without having an instance or pointer of the struct. As "sizeof(int)" is legal I assumed "sizeof(struct x.y)" to be legal too. But is is not: #include <dirent.h>
9
3013
by: M Welinder | last post by:
This doesn't work with any C compiler that I can find. They all report a syntax error: printf ("%d\n", (int)sizeof (char)(char)2); Now the question is "why?" "sizeof" and "(char)" have identical precedence and right-to-left parsing, so why isn't the above equivalent to printf ("%d\n", (int)sizeof ((char)(char)2));
7
1929
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
42
2395
by: Christopher C. Stacy | last post by:
Some people say sizeof(type) and other say sizeof(variable). Why?
8
2532
by: junky_fellow | last post by:
Consider the following piece of code: #include <stddef.h> int main (void) { int i, j=1; char c; printf("\nsize =%lu\n", sizeof(i+j));
90
8414
by: pnreddy1976 | last post by:
Hi, How can we write a function, which functionality is similar to sizeof function any one send me source code Reddy
32
2577
by: Abhishek Srivastava | last post by:
Hi, Somebody recently asked me to implement the sizeof operator, i.e. to write a function that accepts a parameter of any type, and without using the sizeof operator, should be able to return the size occupied by that datatype in memory in bytes. Thanks :) Abhishek Srivastava
5
2893
by: Francois Grieu | last post by:
Does this reliably cause a compile-time error when int is not 4 bytes ? enum { int_size_checked = 1/(sizeof(int)==4) }; Any better way to check the value of an expression involving sizeof before runtime ? I also have: { void check_foo_size(void);
0
8603
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
9026
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
8893
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
8861
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
7723
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...
1
6518
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3045
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2328
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.