Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

problem with sizeof(struct sizelist)

10 views
Skip to first unread message

Xiaojian Chen

unread,
Jan 13, 1999, 3:00:00 AM1/13/99
to
Hi everyone,

I do not know why the following program prints "error!" but not
"right!", can anybody has patience to read this little program and
tell me the reason, much thanks!

#include <stdio.h>
struct sizelist {
int sl_size; /* # of blocks */
int sl_no; /* # of the list in this list */
int sl_leafno; /* B-tree leaf number in the volume */
};

void main(void)
{
int i;


i = -1;
if (i < sizeof(struct sizelist))
printf("right!\n");
else
printf("error!\n");
}


-Chen

Graham Macrae

unread,
Jan 13, 1999, 3:00:00 AM1/13/99
to

Xiaojian Chen wrote in message <19990113214...@suri.co.jp>...


I think that sizeof will return an *unsigned* int - you are compareing it to
a signed int that will be promoted to an unsigned int (with a very large
value) and so 'i' is *bigger* than sizeof().

Try:
printf("i=%u sizeof=%u", i, sizeof(struct sizelist));

to show what the compiler thinks.

--
Graham


Graham Macrae

unread,
Jan 13, 1999, 3:00:00 AM1/13/99
to

Ronald van Riet wrote in message <77ice3$jq6$1...@koza.nl.net>...

>int i;
>i = -1
>
>will result in i getting a positive value depending on the actual
>implementation, but most likely 1.
>
>int is always positive, try:
>
>short i
>i = -1
>
>this will result in i getting the desired negative value.


This is not true.

An int is signed, so is a short. A short is guarenteed to be 2 bytes on all
platforms (-32767 to +32768) an int is machine/platform dependant and
usually a word (4 bytes on most larger boxes).

An unsigned int is positive only.

--
Graham

Torbj|rn Andersson

unread,
Jan 13, 1999, 3:00:00 AM1/13/99
to
Xiaojian Chen <xjc...@suri.co.jp> wrote:

> I do not know why the following program prints "error!" but not
> "right!", can anybody has patience to read this little program and
> tell me the reason, much thanks!

[snip]

> int i;
>
>

> i = -1;
> if (i < sizeof(struct sizelist))
> printf("right!\n");
> else
> printf("error!\n");

I had something like that happen to me once long ago. If memory serves
me, sizeof( ... ) is unsigned, and so i is quietly turned unsigned as
well (and God help you if i is a negative number then).

Torbjörn Andersson


Larry Jones

unread,
Jan 13, 1999, 3:00:00 AM1/13/99
to
Graham Macrae wrote:
>
> An int is signed, so is a short. A short is guarenteed to be 2 bytes on all
> platforms (-32767 to +32768) an int is machine/platform dependant and
> usually a word (4 bytes on most larger boxes).

Guaranteed by whom? All ISO C guarantees is that short is *AT LEAST*
16 bits long (meaning a guaranteed range of -32767 to +32767), which
is exactly the same guarantee as it makes for int.

> An unsigned int is positive only.

Almost; it's non-negative. (Pedants insist that zero is neither
negative nor positive.)

-Larry Jones

Oh, now YOU'RE going to start in on me TOO, huh? -- Calvin

Roger

unread,
Jan 14, 1999, 3:00:00 AM1/14/99
to
On Wed, 13 Jan 1999 17:44:13 -0500, Larry Jones
<larry...@sdrc.com> wrote:

>Graham Macrae wrote:
>>
>> An int is signed, so is a short. A short is guarenteed to be 2 bytes on all
>> platforms (-32767 to +32768) an int is machine/platform dependant and
>> usually a word (4 bytes on most larger boxes).
>
>Guaranteed by whom? All ISO C guarantees is that short is *AT LEAST*
>16 bits long (meaning a guaranteed range of -32767 to +32767), which
>is exactly the same guarantee as it makes for int.

Strictly speaking you should had said only that the C standard
guarantees that shorts and ints have a range of at least -32767
to +32767. The number of bits is not specified; on twos
complement machines it can be done in 16 bits and would usually
(always?) include the value -32768.
--
Roger

Roger

unread,
Jan 14, 1999, 3:00:00 AM1/14/99
to
On Wed, 13 Jan 1999 13:45:40 -0000, "Graham Macrae"
<graham...@tcam.stratus.com> wrote:

>I think that sizeof will return an *unsigned* int

Not according to the C standard. sizeof returns an unsigned
integral type called size_t.

>Try:
> printf("i=%u sizeof=%u", i, sizeof(struct sizelist));

And what happens if sizeof returns something larger than an int?

printf("sizeof=%lu", (unsigned long) sizeof(struct
sizelist));

will worth for all values returned by sizeof.
--
Roger

Alex Vinokur

unread,
Jan 17, 1999, 3:00:00 AM1/17/99
to
Xiaojian Chen wrote:

> Hi everyone,


>
> I do not know why the following program prints "error!" but not
> "right!", can anybody has patience to read this little program and
> tell me the reason, much thanks!
>

> #include <stdio.h>
> struct sizelist {
> int sl_size; /* # of blocks */
> int sl_no; /* # of the list in this list */
> int sl_leafno; /* B-tree leaf number in the volume */
> };
>
> void main(void)
> {

> int i;
>
> i = -1;
> if (i < sizeof(struct sizelist))
> printf("right!\n");
> else
> printf("error!\n");
> }
>

> -Chen


Hi,

Please see below.
Alex


#################################################
############## 1. Program #######################
#################################################


#include <stdio.h>
#include <iostream>


struct sizelist {
int sl_size; /* # of blocks */
int sl_no; /* # of the list in this list */
int sl_leafno; /* B-tree leaf number in the volume */
};

int main(void)
{
int iii = -5;
size_t uuu1 = 3; // size_t is insigned
int uuu2 = 3;

//===============================
cout << "Example#1 : iii + uuu1 = " << iii + uuu1 << endl;
// Here operator+ is
// size_t + size_t
// (because uuu1 is the size_t type), so result is the size_t
type

//===============================
cout << "Example#2 : iii + uuu2 = " << iii + uuu2 << endl;
// Here operator+ is
// int + int
// (because iii and uuu2 are int), so result is the int type


//===============================
int i = -1;
// operator sizeof returns the size_t type,
// so here operator< is
// size_t < size_t

if (i < sizeof(struct sizelist)) // i.e. if ((size_t) i <
sizeof(struct sizelist))
cout << "right" << endl;
else
cout << "error!" << endl;


//===============================
cout << "Example#3 : i = " << i << endl;
//===============================
cout << "Example#4 : (size_t) i = " << (size_t) i << endl;


//===============================
// Solution#1
int s = sizeof(struct sizelist);

cout << "Solution#1 : ";
if (i < s)
cout << "right" << endl;
else
cout << "error!" << endl;


//===============================
// Solution#2
cout << "Solution#2 : ";
if (i < (int) sizeof(struct sizelist))
cout << "right" << endl;
else
cout << "error!" << endl;

return 0;
}


#################################################
############## 2. The results of the running ####
#################################################

Example#1 : iii + uuu1 = 4294967294
Example#2 : iii + uuu2 = -2
error!
Example#3 : i = -1
Example#4 : (size_t) i = 4294967295
Solution#1 : right
Solution#2 : right

#################################################
############## 3. Compiler ######################
#################################################
g++ -v : gcc version egcs-2.91.57 19980901 (egcs-1.1 release)
Note! It is useful to compile with option -Wall
g++ -Wall foo.C

#################################################
############## 4. System ########################
#################################################
uname -a : SunOS tibamsun8 5.6 Generic_105181-09 sun4m sparc
SUNW,SPARCstation-5


#################################################
############## END of INFORMATION ###############
#################################################


David Wragg

unread,
Jan 22, 1999, 3:00:00 AM1/22/99
to
Roger@localhost (Roger) writes:
> On Wed, 13 Jan 1999 13:45:40 -0000, "Graham Macrae"
> <graham...@tcam.stratus.com> wrote:
> >I think that sizeof will return an *unsigned* int
>
> Not according to the C standard. sizeof returns an unsigned
> integral type called size_t.

Unfortunately, C9X seems set to introduce the integral type "long
long" which may be longer than long.

> >Try:
> > printf("i=%u sizeof=%u", i, sizeof(struct sizelist));
>
> And what happens if sizeof returns something larger than an int?
>
> printf("sizeof=%lu", (unsigned long) sizeof(struct
> sizelist));
>
> will worth for all values returned by sizeof.

For C9X, since size_t may be bigger than a long:

printf("sizeof=" PRIuMAX, (uintmax_t) sizeof(struct sizelist));

PRIuMAX and uintmax_t are defined in <inttypes.h>

Dave Wragg

0 new messages