473,472 Members | 2,193 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Sizeof(X) on different architectures


hy ppl, i'm trying to create some multiplatformed software here and i'm very curious about the sizes of common variables on different machines.

if you are running something different than a 32-bit x86 box under your desk, please check what this program outputs.

//-- program starts --

#include <stdio.h>

#define showsize(x,y) printf("Size of %s %d \n",x,sizeof(y));

typedef struct db_s{
int offset;
int length;
char* data;
} db;

int main() {
showsize("char",char);
showsize("int",int);
showsize("long",long);
showsize("char*",char*);
showsize("int*",int*);
showsize("long*",long*);
showsize("void*",void*);
showsize("db",db);
showsize("db*",db*);
return 0;
}

//-- program ends --

on an 32bit x86 it reports like this :

Size of char 1
Size of int 4
Size of long 4
Size of char* 4
Size of int* 4
Size of long* 4
Size of void* 4
Size of db 12
Size of db* 4
......

are there any 64bit sun users out there ? 64bit x86 machine owners and mac users ?

if you can please report in :)
Nov 14 '05 #1
16 2657
some c compilers seem to have issues with the #define line,
so here is a defineless version

#include <stdio.h>
typedef struct db_s{
int offset;
int length;
char* data;
} db;

void report(char *name, int size) {
printf("Size of %s %d \n",name,size);
}

int main() {
report("char",sizeof(char));
report("int",sizeof(int));
report("long",sizeof(long));
report("char*",sizeof(char*));
report("int*",sizeof(int*));
report("long*",sizeof(long*));
report("void*",sizeof(void*));
report("db",sizeof(db));
report("db*",sizeof(db*));
return 0;
}

Martin Roos wrote:

hy ppl, i'm trying to create some multiplatformed software here and i'm
very curious about the sizes of common variables on different machines.

if you are running something different than a 32-bit x86 box under your
desk, please check what this program outputs.

//-- program starts --

#include <stdio.h>

#define showsize(x,y) printf("Size of %s %d \n",x,sizeof(y));

typedef struct db_s{
int offset;
int length;
char* data;
} db;

int main() {
showsize("char",char);
showsize("int",int);
showsize("long",long);
showsize("char*",char*);
showsize("int*",int*);
showsize("long*",long*);
showsize("void*",void*);
showsize("db",db);
showsize("db*",db*);
return 0;
}

//-- program ends --

on an 32bit x86 it reports like this :

Size of char 1
Size of int 4
Size of long 4
Size of char* 4
Size of int* 4
Size of long* 4
Size of void* 4
Size of db 12
Size of db* 4
.....

are there any 64bit sun users out there ? 64bit x86 machine owners and
mac users ?

if you can please report in :)

Nov 14 '05 #2
Martin Roos wrote:
#define showsize(x,y) printf("Size of %s %d \n",x,sizeof(y)); showsize("char",char); showsize("char*",char*); showsize("void*",void*); Size of char 1
sizeof(char) is always one.
Size of char* 4 Size of void* 4


sizeof(char*) is equal to sizeof(void*),
as long as they're on the same implementation.

--
pete
Nov 14 '05 #3
In article <41**********@news.estpak.ee>, Martin Roos <ma****@none.ee>
wrote:
hy ppl, i'm trying to create some multiplatformed software here and i'm very
curious about the sizes of common variables on different machines.

if you are running something different than a 32-bit x86 box under your desk,
please check what this program outputs.

//-- program starts --

#include <stdio.h>

#define showsize(x,y) printf("Size of %s %d \n",x,sizeof(y));

typedef struct db_s{
int offset;
int length;
char* data;
} db;
....
on an 32bit x86 it reports like this :

Size of char 1
Size of int 4
Size of long 4
Size of char* 4
Size of int* 4
Size of long* 4
Size of void* 4
Size of db 12
Size of db* 4


32-bit sparc is identical. 64-bit sparc, and all recent 64-bit Unixes
that I know of use the "LP64" model -- longs and pointers are 64-bit,
ints are 32-bit. I think Windows 64-bit uses the (crazy, IMHO) "LLP64"
model -- long longs and pointers are 64-bits, longs and ints are 32-bit.

The typical Unix and Windows 32-bit case has been referred to as "ILP32"
(ints, longs, and pointers are 32-bits).

In any case, the output when compiled 64-bit on Solaris sparc is:

Size of char 1
Size of int 4
Size of long 8
Size of char * 8
Size of int * 8
Size of long * 8
Size of void * 8
Size of db 16
Size of db * 8

Cheers,
- jonathan
Nov 14 '05 #4
Martin Roos wrote:

hy ppl, i'm trying to create some multiplatformed software here and i'm
very curious about the sizes of common variables on different machines.

[...]

On a Cray Y/MP EL under UNICOS 9.0:

Size of char 1
Size of int 8
Size of long 8
Size of char* 8
Size of int* 8
Size of long* 8
Size of void* 8
Size of db 24
Size of db* 8
--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Nov 14 '05 #5
pete wrote:
.... snip ...
sizeof(char*) is equal to sizeof(void*),
as long as they're on the same implementation.


sizeof(void*) is undefined in standard C.

--
Some useful references:
<http://www.ungerhu.com/jxh/clc.welcome.txt>
<http://www.eskimo.com/~scs/C-faq/top.html>
<http://benpfaff.org/writings/clc/off-topic.html>
<http://anubis.dkuug.dk/jtc1/sc22/wg14/www/docs/n869/> (C99)
<http://www.dinkumware.com/refxc.html> C-library
Nov 14 '05 #6
CBFalconer wrote:
pete wrote:

... snip ...

sizeof(char*) is equal to sizeof(void*),
as long as they're on the same implementation.


sizeof(void*) is undefined in standard C.


Why do you think so? While "void" is an incomplete type
that cannot be completed, "void *" is a pointer type, and
it is not incomplete.

Kurt Watzka

Nov 14 '05 #7
In article <41***************@yahoo.com>,
CBFalconer <cb********@worldnet.att.net> wrote:
sizeof(void*) is undefined in standard C.


How can we have a void* with the same representation (and interchangeability)
as char* [ISO/IEC9899:1999 6.2.5 §26] and at the same time have sizeof(void*)
undefined?

A void* must have the same size as char*. The void type, on the other hand,
doesn't have a size so sizeof(void) is undefined.

--
Göran Larsson http://www.mitt-eget.com/
Nov 14 '05 #8
Martin Roos wrote:
#define showsize(x,y) printf("Size of %s %d \n",x,sizeof(y));


A slightly cleverer way of doing this is with stringizing:

#define showsize(t) printf("Size of %s %d \n", #t, sizeof(t));

--
Derrick Coetzee
I grant this newsgroup posting into the public domain. I disclaim all
express or implied warranty and all liability. I am not a professional.
Nov 14 '05 #9
Martin Roos <ma****@none.ee> writes:
some c compilers seem to have issues with the #define line,
so here is a defineless version

#include <stdio.h>
typedef struct db_s{
int offset;
int length;
char* data;
} db;

void report(char *name, int size) {
printf("Size of %s %d \n",name,size);
}

int main() {
report("char",sizeof(char));
report("int",sizeof(int));
report("long",sizeof(long));
report("char*",sizeof(char*));
report("int*",sizeof(int*));
report("long*",sizeof(long*));
report("void*",sizeof(void*));
report("db",sizeof(db));
report("db*",sizeof(db*));
return 0;
}


I'll use the system identification strings reported by the GNU
"config.guess" script.

mips-sgi-irix6.5
Size of char 1
Size of int 4
Size of long 4
Size of char* 4
Size of int* 4
Size of long* 4
Size of void* 4
Size of db 12
Size of db* 4

ia64-unknown-linux-gnu:
Size of char 1
Size of int 4
Size of long 8
Size of char* 8
Size of int* 8
Size of long* 8
Size of void* 8
Size of db 16
Size of db* 8

alphaev68-dec-osf5.1b:
Size of char 1
Size of int 4
Size of long 8
Size of char* 8
Size of int* 8
Size of long* 8
Size of void* 8
Size of db 16
Size of db* 8

sv1-cray-unicos10.0.1.X:
Size of char 1
Size of int 8
Size of long 8
Size of char* 8
Size of int* 8
Size of long* 8
Size of void* 8
Size of db 24
Size of db* 8

powerpc-ibm-aix5.2.0.0:
Size of char 1
Size of int 4
Size of long 4
Size of char* 4
Size of int* 4
Size of long* 4
Size of void* 4
Size of db 12
Size of db* 4

VAX/VMS (no config.guess script):
Size of char 1
Size of int 4
Size of long 4
Size of char* 4
Size of int* 4
Size of long* 4
Size of void* 4
Size of db 12
Size of db* 4

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #10
Kurt Watzka wrote:
CBFalconer wrote:
pete wrote:

... snip ...

sizeof(char*) is equal to sizeof(void*),
as long as they're on the same implementation.


sizeof(void*) is undefined in standard C.


Why do you think so? While "void" is an incomplete type
that cannot be completed, "void *" is a pointer type, and
it is not incomplete.


Woops. I was overhasty. I must have been thinking of *voidptr.

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 14 '05 #11
You realize those sizes come from the compiler, not the CPU.
compile the code with a 16bit windows compiler and it will report the same numbers in 16,32, and 64 bit systems.
The sizes are listed in one of the header files



Nov 14 '05 #12
In <41**************@mail.asb.com> Neil Kurzman <ns*@mail.asb.com> writes:
You realize those sizes come from the compiler, not the CPU.


In most cases, the implementor's chices are *strongly* influenced by the
CPU. In some cases, they are also influenced by the OS.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #13
In <41***************@yahoo.com> CBFalconer <cb********@yahoo.com> writes:
pete wrote:

... snip ...

sizeof(char*) is equal to sizeof(void*),
as long as they're on the same implementation.


sizeof(void*) is undefined in standard C.


Engage your brain and explain why.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #14
In <cj**********@news-int2.gatech.edu> Derrick Coetzee <dc****@moonflare.com> writes:
Martin Roos wrote:
#define showsize(x,y) printf("Size of %s %d \n",x,sizeof(y));


A slightly cleverer way of doing this is with stringizing:

#define showsize(t) printf("Size of %s %d \n", #t, sizeof(t));

^^ ^^^^^^^^^
Your time would have been better spent in removing the undefined
behaviour: cleverness is no substitute for correctness.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #15
Da*****@cern.ch (Dan Pop) writes:
In <41***************@yahoo.com> CBFalconer <cb********@yahoo.com> writes:
pete wrote:

... snip ...

sizeof(char*) is equal to sizeof(void*),
as long as they're on the same implementation.


sizeof(void*) is undefined in standard C.


Engage your brain and explain why.


CBFalconer acknowledged his mistake two days ago:

] Woops. I was overhasty. I must have been thinking of *voidptr.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #16
In <ln************@nuthaus.mib.org> Keith Thompson <ks***@mib.org> writes:
Da*****@cern.ch (Dan Pop) writes:
In <41***************@yahoo.com> CBFalconer <cb********@yahoo.com> writes:
pete wrote:

... snip ...

sizeof(char*) is equal to sizeof(void*),
as long as they're on the same implementation.

sizeof(void*) is undefined in standard C.


Engage your brain and explain why.


CBFalconer acknowledged his mistake two days ago:

] Woops. I was overhasty. I must have been thinking of *voidptr.


I got that acknowledgment, in the meantime, too.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #17

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

Similar topics

7
by: nzanella | last post by:
Hello, I just thought I would share the following observation with the rest of the group. The sizeof operator seems to act differently according to whether the number of elements in the array is...
37
by: thushianthan15 | last post by:
Greetings!! Currently i am learning C from K&R.I have DOS & Linux in my system.When i used sizeof() keyword to compute the size of integer , it shows different results in DOS and Linux.In DOS it...
18
by: Mockey Chen | last post by:
My friend ask me a question as following: give a union SU define as: typedef union _SU { short x; struct y{ char a; short b; char c;
8
by: Tony Houghton | last post by:
I'm writing a python program which reads input device events so it needs to know sizeof(struct timeval). By using the struct module I should be able to work out sizeof(long) from python, but I...
2
by: blufox | last post by:
I read somewhere that standard Ansi C on 32 bit architecures assumes a lot of things which lead to poor code. For example sizeof( int ) = sizeof( void * ) sizeof( long ) = sizeof( int ) Why...
38
by: James Brown | last post by:
All, I have a quick question regarding the size of pointer-types: I believe that the sizeof(char *) may not necessarily be the same as sizeof(int *) ? But how about multiple levels of pointers...
72
by: goacross | last post by:
char ch='a'; int v=sizeof ++ch; cout<<ch<<endl;// output: 'a' why not 'b'? thanks
56
by: William Xu | last post by:
I test it with char, short, int, long, float, double and struct pointers, all return 4 bytes. Does the standard say anything about sizeof a pointer? (I didn't find it in the standard...) --...
40
by: Boltar | last post by:
Hi Why - using gcc on linux - does this return 0 in C but returns 1 in C+ +? I don't get it. #include <stdio.h> struct foo { };
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
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...
0
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...
0
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,...
1
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
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.