niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » C
sizeof()
Post new topic   Reply to topic Page 1 of 1 [13 Posts] View previous topic :: View next topic
Author Message
dcorbit@connx.com
*nix forums addict


Joined: 13 Jun 2006
Posts: 92

PostPosted: Fri Jul 21, 2006 12:21 am    Post subject: Re: sizeof() Reply with quote

"Ronald Bruck" <bruck@math.usc.edu> wrote in message
news:200720061659215633%bruck@math.usc.edu...
[snip]
Quote:
This brings up a problem I've wondered about: what support is there in
C for two-byte characters? I can foresee, in not too many years, the
ordinary char going the way of the "short"--provided for efficiency,
but not often used. That way, I'll be able to get even MORE junk
e-mail in Kanji :-(

Googling and Wikipedia-ing, I find info on XDR (eXternal Data
Representation), but nothing specifically about C, except a library
provided by IBM. Is this done externally to C? Is there some standard
library for it?

You are referring to ICU, I imagine.
http://www-306.ibm.com/software/globalization/icu/index.jsp

That is the only tool set that I know of that is full featured enough to
solve the typical problem set associated with internationalization.

If you need to do internationalization of your software projects, then I can
say that I highly recommend it. We use it with C++ but I gather that it can
also be used from C.
Back to top
Ronald Bruck
*nix forums beginner


Joined: 13 Mar 2006
Posts: 11

PostPosted: Thu Jul 20, 2006 11:59 pm    Post subject: Re: sizeof() Reply with quote

In article <uh40c255petibtrmh91023l3ksq8ghpm29@4ax.com>, Barry Schwarz
<schwarzb@doezl.net> wrote:

Quote:
On Thu, 20 Jul 2006 03:33:18 GMT, dbtid <dbtid0@nospam.gmail.com
wrote:

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?

Because sizeof(int) could be 7, sizeof(double) could be 8, and
sizeof(char) must be 1. No padding at all.

This brings up a problem I've wondered about: what support is there in
C for two-byte characters? I can foresee, in not too many years, the
ordinary char going the way of the "short"--provided for efficiency,
but not often used. That way, I'll be able to get even MORE junk
e-mail in Kanji :-(

Googling and Wikipedia-ing, I find info on XDR (eXternal Data
Representation), but nothing specifically about C, except a library
provided by IBM. Is this done externally to C? Is there some standard
library for it?

--
Ron Bruck

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Back to top
Barry Schwarz
*nix forums Guru


Joined: 09 Apr 2005
Posts: 390

PostPosted: Thu Jul 20, 2006 11:33 pm    Post subject: Re: sizeof() Reply with quote

On Thu, 20 Jul 2006 03:33:18 GMT, dbtid <dbtid0@nospam.gmail.com>
wrote:

Quote:
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?

Because sizeof(int) could be 7, sizeof(double) could be 8, and
sizeof(char) must be 1. No padding at all.


Remove del for email
Back to top
Giorgio Silvestri
*nix forums beginner


Joined: 08 Jul 2006
Posts: 4

PostPosted: Thu Jul 20, 2006 5:10 pm    Post subject: Re: sizeof() Reply with quote

"Richard Tobin" <richard@cogsci.ed.ac.uk> ha scritto nel messaggio
news:e9o2q3$18ar$1@pc-news.cogsci.ed.ac.uk...
Quote:
In article <1153403331.267938.304730@s13g2000cwa.googlegroups.com>,
sarathy <sps.sarathy@gmail.com> wrote:

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 Smile */
printf ("size = %d\n",(int)sizeof(b));

return EXIT_SUCCESS;
}

In C99 you can use also %zu.



Giorgio Silvestri
Back to top
Richard Tobin
*nix forums Guru


Joined: 22 Mar 2005
Posts: 372

PostPosted: Thu Jul 20, 2006 2:09 pm    Post subject: Re: sizeof() Reply with quote

In article <1153403331.267938.304730@s13g2000cwa.googlegroups.com>,
sarathy <sps.sarathy@gmail.com> wrote:

Quote:
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.

Quote:
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
Back to top
sarathy
*nix forums beginner


Joined: 17 Jul 2006
Posts: 12

PostPosted: Thu Jul 20, 2006 1:48 pm    Post subject: Re: sizeof() Reply with quote

Walter Roberson wrote:
Quote:
In article <1153324264.506809.63940@i3g2000cwc.googlegroups.com>,
Jack <junw2000@gmail.com> 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?

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
Back to top
jaysome
*nix forums addict


Joined: 01 Mar 2006
Posts: 69

PostPosted: Thu Jul 20, 2006 8:40 am    Post subject: Re: sizeof() Reply with quote

On 19 Jul 2006 08:58:01 -0700, "Robert Gamble" <rgamble99@gmail.com>
wrote:

Quote:
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/
Back to top
dbtid
*nix forums beginner


Joined: 03 Jul 2006
Posts: 3

PostPosted: Thu Jul 20, 2006 3:33 am    Post subject: Re: sizeof() Reply with quote

Tom St Denis wrote:
Quote:
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?
Back to top
Roberto Waltman
*nix forums Guru Wannabe


Joined: 21 Feb 2005
Posts: 180

PostPosted: Wed Jul 19, 2006 4:06 pm    Post subject: Re: sizeof() Reply with quote

"Jack" <junw2000@gmail.com> wrote:
Quote:
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 ]
Back to top
Walter Roberson
*nix forums Guru


Joined: 19 Feb 2005
Posts: 1300

PostPosted: Wed Jul 19, 2006 4:01 pm    Post subject: Re: sizeof() Reply with quote

In article <1153324264.506809.63940@i3g2000cwc.googlegroups.com>,
Jack <junw2000@gmail.com> wrote:

Quote:
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)
Back to top
Robert Gamble
*nix forums Guru


Joined: 15 Apr 2005
Posts: 447

PostPosted: Wed Jul 19, 2006 3:58 pm    Post subject: Re: sizeof() Reply with quote

Jack wrote:
Quote:
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
Back to top
Tom St Denis
*nix forums Guru Wannabe


Joined: 22 Feb 2005
Posts: 161

PostPosted: Wed Jul 19, 2006 3:54 pm    Post subject: Re: sizeof() Reply with quote

Jack wrote:
Quote:
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
Back to top
junw2000@gmail.com
*nix forums addict


Joined: 24 Feb 2006
Posts: 92

PostPosted: Wed Jul 19, 2006 3:51 pm    Post subject: sizeof() Reply with quote

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
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [13 Posts] View previous topic :: View next topic
The time now is Fri Nov 21, 2008 8:45 pm | All times are GMT
navigation Forum index » Programming » C
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts sizeof() junw2000@gmail.com C++ 5 Wed Jul 19, 2006 3:56 pm
No new posts Implementing the sizeof operator abhishek.srivastava78@gma C++ 32 Wed Jul 05, 2006 11:43 am
No new posts MIPSPro compiler sizeof classes philparsonage@hotmail.com apps 2 Wed Jun 28, 2006 2:00 pm
No new posts sizeof pnreddy1976@gmail.com C 90 Sun Jun 18, 2006 11:42 am
No new posts sizeof 'a' returns 4 Laurentiu C 29 Sun Jun 18, 2006 5:58 am

Samsung | Free Ringtones | Buy Anything On eBay | Credit Card Consolidation | Hypotonic Cerebral Palsy
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.5504s ][ Queries: 20 (0.4300s) ][ GZIP on - Debug on ]