Subject: Re: Types sizes in C
To: khorben <khorben@defora.org>
From: Michael <macallan18@earthlink.net>
List: port-sparc64
Date: 04/07/2006 22:12:08
--Signature_Fri__7_Apr_2006_22_12_08_-0400_1.fmsuku1og1PWyB
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: quoted-printable
Hello,
> I have been recently surprised to see that the following C program
> has a different output on Solaris 10 and NetBSD 3, for the same
> hardware (and both while using gcc):
>=20
> #include <stdio.h>
> #define type(a) printf("sizeof(%s) =3D %d\n", "" # a, sizeof(a));
> int main(void)
> {
> type(char);
> type(short);
> type(int);
> type(float);
> type(double);
> type(long);
> type(long long);
> return 0;
> }
>=20
> So on Solaris 10 I obtain:
> $ ./sizes
> sizeof(char) =3D 1
> sizeof(short) =3D 2
> sizeof(int) =3D 4
> sizeof(float) =3D 4
> sizeof(double) =3D 8
> sizeof(long) =3D 4
> sizeof(long long) =3D 8
>=20
> while NetBSD 3 gives:
>=20
> $ ./sizes
> sizeof(char) =3D 1
> sizeof(short) =3D 2
> sizeof(int) =3D 4
> sizeof(float) =3D 4
> sizeof(double) =3D 8
> sizeof(long) =3D 8
> sizeof(long long) =3D 8
>=20
> ok, the only difference is actually the size of long integers,
> respectively 32 and 64 bits. From my reading of the ANSI C standard I
> understood this is possible.
Check the resulting binary - on NetBSD it will be ELF64, on Solaris it's
apparently 32bit.
> I have a few questions about this though, if appropriate:
> - who sets this?
The compiler.
> - why not keep long as 32 bits and let int default to 64 bits instead?
> This would help the short/long/long long hierarchy coherence, and
> let int default to the native processor size (and 486 would be 16
> bits!)
Only if you run the 486 in real or 16bit protected mode.
> - has this ever been observed to cause portability or stability issues
> on this platform?
People blindly assuming sizeof(long) =3D=3D 4 or - even worse sizeof(int)
=3D=3D sizeof(void *) - cause endless headaches.
> My concern is, I have seen programmers assume sizeof(int) or
> sizeof(long) are 32 bits, even while writing portable 32/64 bits code
> (and I was myself wrong at first, since it is apparently very platform
> dependant even on 64 bits hardware).
Yes, and they're dead wrong. Code that assumes sizeof(long) =3D=3D
sizeof(int) is /not/ portable, it will break when compiled on a 64bit
target. This is also not really platform or OS dependent - in a 64bit
binary running on solaris you'll get sizeof(long) =3D=3D sizeof(void*) =3D=
=3D 8
> Morale =3D> #include <stdint.h> ?
If you depend on size use size-safe types like uint32_t and friends.
have fun
Michael
--Signature_Fri__7_Apr_2006_22_12_08_-0400_1.fmsuku1og1PWyB
Content-Type: application/pgp-signature
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.0 (NetBSD)
iQEVAwUBRDcb+MpnzkX8Yg2nAQJq5Qf/Vju6qLaHijvVvuN8huDq93md2F8opyL5
tFbpCir8Cn/9bHeDUSvRezf5MOlx2ePjRFtijuqi4Eo6jlLwtdknUC5Mg+VXQx1f
Zk3d6Do0ia6OS/jU3/p/v+OrevOGCk/iMTR+jrYD/65p5hb4LH1l81qJR7e98aG8
ZnoLzrF/XTpsGETmk7ovo2zzCqYujiKlInhUdEiVaqyS6LudHb7CVjzGRYhl+XGX
ZDJ+DzM4/R30lAiaqT+4ktHiBU6DawHl+dteNh08YveVe+otFMyn+EISzYhzxHTC
xIs2j1Qsrt2VI4EOzCgp1JpmYBvuZ/WbI5yze5XppmgVMXelw3yiXQ==
=hYZl
-----END PGP SIGNATURE-----
--Signature_Fri__7_Apr_2006_22_12_08_-0400_1.fmsuku1og1PWyB--