Sizeof(X) on different architectures

M

Martin Roos

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 :)
 
M

Martin Roos

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;
}
 
J

Jonathan Adams

Martin Roos <[email protected]> said:
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
 
K

Kenneth Brody

Martin said:
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
 
K

Kurt Watzka

CBFalconer said:
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
 
G

Goran Larsson

CBFalconer said:
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.
 
D

Derrick Coetzee

Martin said:
#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));
 
K

Keith Thompson

Martin Roos said:
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
 
C

CBFalconer

Kurt said:
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.
 
N

Neil Kurzman

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
 
D

Dan Pop

In said:
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
 
D

Dan Pop

In said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top