Sizeof(X) on different architectures

Discussion in 'C Programming' started by Martin Roos, Sep 27, 2004.

  1. Martin Roos

    Martin Roos Guest

    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 :)
    Martin Roos, Sep 27, 2004
    #1
    1. Advertising

  2. Martin Roos

    Martin Roos Guest

    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 :)
    Martin Roos, Sep 27, 2004
    #2
    1. Advertising

  3. Martin Roos

    pete Guest

    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
    pete, Sep 27, 2004
    #3
  4. In article <4157b92f$>, 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;
    >

    ....
    >
    > 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
    Jonathan Adams, Sep 27, 2004
    #4
  5. 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> |
    +-------------------------+--------------------+-----------------------------+
    Kenneth Brody, Sep 27, 2004
    #5
  6. Martin Roos

    Kurt Watzka Guest

    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
    Kurt Watzka, Sep 27, 2004
    #6
  7. Martin Roos

    CBFalconer Guest

    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
    CBFalconer, Sep 27, 2004
    #7
  8. In article <>,
    CBFalconer <> 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/
    Goran Larsson, Sep 27, 2004
    #8
  9. 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.
    Derrick Coetzee, Sep 28, 2004
    #9
  10. Martin Roos <> 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) <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.
    Keith Thompson, Sep 28, 2004
    #10
  11. Martin Roos

    CBFalconer Guest

    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?
    CBFalconer, Sep 28, 2004
    #11
  12. Martin Roos

    Neil Kurzman Guest

    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


    >
    Neil Kurzman, Sep 29, 2004
    #12
  13. Martin Roos

    Dan Pop Guest

    In <> Neil Kurzman <> 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:
    Currently looking for a job in the European Union
    Dan Pop, Sep 29, 2004
    #13
  14. Martin Roos

    Dan Pop Guest

    In <> CBFalconer <> 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:
    Currently looking for a job in the European Union
    Dan Pop, Sep 29, 2004
    #14
  15. Martin Roos

    Dan Pop Guest

    In <cjaa81$i40$> Derrick Coetzee <> 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:
    Currently looking for a job in the European Union
    Dan Pop, Sep 29, 2004
    #15
  16. (Dan Pop) writes:
    > In <> CBFalconer <> 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) <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.
    Keith Thompson, Sep 29, 2004
    #16
  17. Martin Roos

    Dan Pop Guest

    In <> Keith Thompson <> writes:

    > (Dan Pop) writes:
    >> In <> CBFalconer <> 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:
    Currently looking for a job in the European Union
    Dan Pop, Sep 30, 2004
    #17
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Derek
    Replies:
    7
    Views:
    24,323
    Ron Natalie
    Oct 14, 2004
  2. Trevor

    sizeof(str) or sizeof(str) - 1 ?

    Trevor, Apr 3, 2004, in forum: C Programming
    Replies:
    9
    Views:
    626
    CBFalconer
    Apr 10, 2004
  3. Vinu
    Replies:
    13
    Views:
    1,409
    Lawrence Kirby
    May 12, 2005
  4. blufox

    sizeof( int ) != sizeof( void * )

    blufox, May 22, 2006, in forum: C Programming
    Replies:
    2
    Views:
    554
    Joe Smith
    May 22, 2006
  5. Angus
    Replies:
    1
    Views:
    241
    James Kanze
    Aug 3, 2008
Loading...

Share This Page