sizeof(int)

Discussion in 'C++' started by Richard Cavell, Feb 26, 2005.

  1. Hi,

    Is there some kind of canonical list, or would someone like to give a
    brief rundown, as to:

    sizeof(int)
    sizeof(long int)
    sizeof(long long)
    etc

    or perhaps even the vector types, for current hardware (Pentium
    4/Athlon/G4/G5/Playstation 3/etc) used with current compilers? Or
    perhaps a list of what size int_t you can define and expect the
    processor to handle natively?
    Richard Cavell, Feb 26, 2005
    #1
    1. Advertising

  2. "Richard Cavell" <> wrote...
    > Is there some kind of canonical list, or would someone like to give a
    > brief rundown, as to:
    >
    > sizeof(int)
    > sizeof(long int)
    > sizeof(long long)
    > etc


    sizeof(int) is the size of an int object expressed in bytes.
    sizeof(long int) is the size of a long int object.
    sizeof(long long) is a syntax error since C++ has no "long long" type.

    I am not sure what "canonical list" you're talking about.

    > or perhaps even the vector types, for current hardware (Pentium
    > 4/Athlon/G4/G5/Playstation 3/etc) used with current compilers? Or perhaps
    > a list of what size int_t you can define and expect the processor to
    > handle natively?


    If you need something compiler-specific, please ask in a newsgroup
    dedicated to that compiler. C++ discussed here is compiler-independent.

    If you need something hardware-specific, please ask in a newsgroup
    dedicated to that hardware. C++ is a hardware-independent language.

    V
    Victor Bazarov, Feb 26, 2005
    #2
    1. Advertising

  3. On 27/2/05 3:03 AM, Victor Bazarov wrote:

    > C++ is a hardware-independent language.


    No, it ain't.

    I have to write my program differently depending on what these sizeofs
    are. So it's not hardware-independent at all.
    Richard Cavell, Feb 27, 2005
    #3
  4. "Richard Cavell" <> wrote...
    > On 27/2/05 3:03 AM, Victor Bazarov wrote:
    >
    >> C++ is a hardware-independent language.

    >
    > No, it ain't.
    >
    > I have to write my program differently depending on what these sizeofs
    > are. So it's not hardware-independent at all.


    It's not the language, silly. It's your algorithm, it's what you want
    to do, that makes it hardware-dependent.
    Victor Bazarov, Feb 27, 2005
    #4
  5. "Richard Cavell" <> wrote in message
    news:cvpg29$3ai$...
    > Hi,
    >
    > Is there some kind of canonical list, or would someone like to give a
    > brief rundown, as to:
    >
    > sizeof(int)
    > sizeof(long int)
    > sizeof(long long)
    > etc
    >
    > or perhaps even the vector types, for current hardware (Pentium
    > 4/Athlon/G4/G5/Playstation 3/etc) used with current compilers? Or perhaps
    > a list of what size int_t you can define and expect the processor to
    > handle natively?


    usually an int is the size of the register used, on the 286 this was 16
    bits, a long was 32.
    on 386 protected, 486, int is 32 bits, and long int, is 32 bits as well,
    Double Word is 64 bits.
    A char is always 8 bits. The ratio of Char size to int size will tell you
    the hardware type, ie the register width.
    DHOLLINGSWORTH2, Feb 27, 2005
    #5
  6. Richard Cavell

    Shezan Baig Guest

    DHOLLINGSWORTH2 wrote:
    > A char is always 8 bits.



    Why can't a char be 16 bits?
    Shezan Baig, Feb 27, 2005
    #6
  7. On 27/2/05 2:28 PM, Victor Bazarov wrote:
    > "Richard Cavell" <> wrote...
    >
    >>On 27/2/05 3:03 AM, Victor Bazarov wrote:
    >>
    >>
    >>>C++ is a hardware-independent language.

    >>
    >>No, it ain't.
    >>
    >>I have to write my program differently depending on what these sizeofs
    >>are. So it's not hardware-independent at all.

    >
    >
    > It's not the language, silly. It's your algorithm, it's what you want
    > to do, that makes it hardware-dependent.


    That makes no sense at all. If C++ ran inside a virtual machine then it
    wouldn't matter to me whether my processor could do 16-bit integers at a
    time, or 32, or whatever. But my program actually does different things
    based on sizeof(int) whether I want it to or not.
    Richard Cavell, Feb 27, 2005
    #7
  8. Richard Cavell wrote:
    > On 27/2/05 2:28 PM, Victor Bazarov wrote:
    >> "Richard Cavell" <> wrote...
    >>> On 27/2/05 3:03 AM, Victor Bazarov wrote:


    >>>> C++ is a hardware-independent language.
    >>>
    >>> No, it ain't.
    >>>
    >>> I have to write my program differently depending on what these
    >>> sizeofs are. So it's not hardware-independent at all.

    >>
    >>
    >> It's not the language, silly. It's your algorithm, it's what you
    >> want to do, that makes it hardware-dependent.

    >
    > That makes no sense at all. If C++ ran inside a virtual machine then
    > it wouldn't matter to me whether my processor could do 16-bit
    > integers at a time, or 32, or whatever. But my program actually does
    > different things based on sizeof(int) whether I want it to or not.


    The following is a hardware-dependent program, in your sense:

    #include <iostream>

    int main()
    {
    if (sizeof(int) == 19)
    std::cout << "hello 19 world\n";
    else
    std::cout << "hello non-19 world\n";
    }

    Is this what bothers you? You don't want your program to be able to detect
    anything that varies from system to system?

    It would be more worthwile to ask about ways to write programs which work on a
    variety of platforms. There are lots of ways to do it.

    Jonathan
    Jonathan Turkanis, Feb 27, 2005
    #8
  9. Richard Cavell

    Shezan Baig Guest

    Richard Cavell wrote:
    > On 27/2/05 2:28 PM, Victor Bazarov wrote:
    > > It's not the language, silly. It's your algorithm, it's what you

    want
    > > to do, that makes it hardware-dependent.

    >
    > That makes no sense at all. If C++ ran inside a virtual machine then

    it
    > wouldn't matter to me whether my processor could do 16-bit integers

    at a
    > time, or 32, or whatever. But my program actually does different

    things
    > based on sizeof(int) whether I want it to or not.



    What exactly is your program trying to do? A portable program should
    not need to depend on sizeof(int) being a certain size, unless you're
    doing some form of externalization.
    Shezan Baig, Feb 27, 2005
    #9
  10. Richard Cavell

    Phil Staite Guest

    Richard Cavell wrote:
    > On 27/2/05 2:28 PM, Victor Bazarov wrote:
    > That makes no sense at all. If C++ ran inside a virtual machine then it
    > wouldn't matter to me whether my processor could do 16-bit integers at a
    > time, or 32, or whatever. But my program actually does different things
    > based on sizeof(int) whether I want it to or not.



    Then I suggest you have sizeof(int) in your program, such as:

    if( sizeof(int) == 2 )
    {
    // oh schnitt, a 16 bit machine!!!
    }
    else if( sizeof(int) == 4 )
    {
    // ahh, mainstream life...
    }
    else if( sizeof(int) == 8 )
    {
    // where the {expletive} am I now?
    }

    If you're curious on your machine, just dump them:

    #include<iostream>

    int main( int argc, char* argv[] )
    {
    std::cout << "int " << sizeof(int) << std::endl;
    std::cout << "long " << sizeof(long) << std::endl;
    return 0;
    }
    Phil Staite, Feb 27, 2005
    #10
  11. Richard Cavell

    Ron Natalie Guest

    Shezan Baig wrote:
    > DHOLLINGSWORTH2 wrote:
    >
    >>A char is always 8 bits.

    >
    >
    >
    > Why can't a char be 16 bits?
    >

    It can be, but it is unlikely. Unfortunately one of the
    great defects in C++ is that char has a double role: that
    of the basic character AND that of the smallest addressable
    storage unit. Ideally, bytes and characters should be dissassociated.
    Ron Natalie, Feb 27, 2005
    #11
  12. "Ron Natalie" <> wrote...
    > Shezan Baig wrote:
    >> DHOLLINGSWORTH2 wrote:
    >>
    >>>A char is always 8 bits.

    >>
    >>
    >>
    >> Why can't a char be 16 bits?
    >>

    > It can be, but it is unlikely. Unfortunately one of the
    > great defects in C++ is that char has a double role: that
    > of the basic character AND that of the smallest addressable
    > storage unit. Ideally, bytes and characters should be dissassociated.


    I think you misunderstand the role of a char WRT basic character set.
    Nothing prevents a 32-bit char from containing an element from the basic
    character set. The requirement is only that the char type to be large
    enough for that. There is no requirement that it has to be no larger
    than necessary to contain the basic characters. There is no defect in
    the language, only in your understanding of it.

    V
    Victor Bazarov, Feb 27, 2005
    #12
  13. Richard Cavell

    Carl Muller Guest

    "DHOLLINGSWORTH2" <> wrote in message news:<gpcUd.18605$yr.10961@okepread05>...
    > "Richard Cavell" <> wrote in message
    > news:cvpg29$3ai$...

    [snip]
    > A char is always 8 bits. The ratio of Char size to int size will tell you
    > the hardware type, ie the register width.


    Not always. A char is at least 8 bits, but on the hardware I program
    it is 16 bits (as is int, so sizeof(int)==1). There are other chip
    manufacturers in the world than Intel you know.
    Carl Muller, Feb 27, 2005
    #13
  14. Richard Cavell

    Jerry Coffin Guest

    Richard Cavell wrote:

    [ ... ]

    > That makes no sense at all. If C++ ran inside a virtual machine then

    it
    > wouldn't matter to me whether my processor could do 16-bit integers

    at a
    > time, or 32, or whatever. But my program actually does different

    things
    > based on sizeof(int) whether I want it to or not.


    If you need something you're certain is at least 32 bits, use long. If
    you need something you need to be certain is only 16 bits, use a
    bitfield.

    In the end, you're right: running in a VM could provide greater
    isolation from the hardware, and with it greater portability. OTOH, you
    need to use SOME language to implement the VM, and the operating system
    it runs on, and he device drivers IT uses to talk to the hardware --
    and right now, the languages of choice for all those things UNDER the
    VM are C and C++. If you insist on them running inside of the VM, you
    have to come up with something else to implement all the other things
    under the VM.

    Based on experience, I'd put the chances at roughly 99% that if you
    attempted to create a language to replace C and C++ in those roles,
    you'd end up with something that did NOT provide as good of a tradeoff
    between portability and access to the hardware as C and C++ provide
    right now. Of all the gazillions of attempts at such programming
    languages, there's only about _one_ that's really competitive with C or
    C++ for these tasks.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
    Jerry Coffin, Feb 27, 2005
    #14
  15. Richard Cavell wrote:

    > That makes no sense at all. If C++ ran inside a virtual machine then it
    > wouldn't matter to me whether my processor could do 16-bit integers at a
    > time, or 32, or whatever. But my program actually does different things
    > based on sizeof(int) whether I want it to or not.



    I do not think the VM makes the difference you are thinking it does. A
    VM is a machine as its name implies with its own assembly language.


    For example, .NET is a CLI VM and here is a book about .NET's (CLI) VM
    assembly language:


    http://www.amazon.com/exec/obidos/t...f=sr_1_7/102-2992266-3703320?v=glance&s=books



    CLI assembly language is defined in the freely available CLI standard,
    so you can view in the specification how it looks like:


    http://www.ecma-international.org/publications/standards/Ecma-335.htm



    --
    Ioannis Vranos

    http://www23.brinkster.com/noicys
    Ioannis Vranos, Feb 27, 2005
    #15
  16. "Ron Natalie" <> wrote in message
    news:4221de94$0$28724$...
    > Shezan Baig wrote:
    >> DHOLLINGSWORTH2 wrote:
    >>
    >>>A char is always 8 bits.

    >>
    >>
    >>
    >> Why can't a char be 16 bits?
    >>

    > It can be, but it is unlikely. Unfortunately one of the
    > great defects in C++ is that char has a double role: that
    > of the basic character AND that of the smallest addressable
    > storage unit. Ideally, bytes and characters should be dissassociated.


    You are correct, I should have said "Byte".
    DHOLLINGSWORTH2, Feb 27, 2005
    #16
  17. Ioannis Vranos, Feb 27, 2005
    #17
  18. "DHOLLINGSWORTH2" <> wrote...
    >
    > "Ron Natalie" <> wrote in message
    > news:4221de94$0$28724$...
    >> Shezan Baig wrote:
    >>> DHOLLINGSWORTH2 wrote:
    >>>
    >>>>A char is always 8 bits.
    >>>
    >>>
    >>>
    >>> Why can't a char be 16 bits?
    >>>

    >> It can be, but it is unlikely. Unfortunately one of the
    >> great defects in C++ is that char has a double role: that
    >> of the basic character AND that of the smallest addressable
    >> storage unit. Ideally, bytes and characters should be dissassociated.

    >
    > You are correct, I should have said "Byte".


    In C++ terms 'char' and 'byte' are interchangeable. You should have
    said "octet", maybe.

    V
    Victor Bazarov, Feb 27, 2005
    #18
  19. Richard Cavell

    Ron Natalie Guest


    >
    > I think you misunderstand the role of a char WRT basic character set.


    I understand it perfoectly.

    > Nothing prevents a 32-bit char from containing an element from the basic
    > character set.


    And who cares? The issue is not the maximum size. The issue is that if
    you choose to use a larger character size (imagine UNICODE as the basic
    character set), you can't make the char 32 bits if you want to still be
    able to address 8 bit pieces of memory somewhere.

    > The requirement is only that the char type to be large
    > enough for that. There is no requirement that it has to be no larger
    > than necessary to contain the basic characters. There is no defect in
    > the language, only in your understanding of it.


    My understnading is fine, your understanding of the limitations is what
    is deficient.
    Ron Natalie, Feb 28, 2005
    #19
  20. Richard Cavell

    Jack Klein Guest

    On Sat, 26 Feb 2005 23:20:45 -0700, Phil Staite <>
    wrote in comp.lang.c++:

    > Richard Cavell wrote:
    > > On 27/2/05 2:28 PM, Victor Bazarov wrote:
    > > That makes no sense at all. If C++ ran inside a virtual machine then it
    > > wouldn't matter to me whether my processor could do 16-bit integers at a
    > > time, or 32, or whatever. But my program actually does different things
    > > based on sizeof(int) whether I want it to or not.

    >
    >
    > Then I suggest you have sizeof(int) in your program, such as:
    >
    > if( sizeof(int) == 2 )
    > {
    > // oh schnitt, a 16 bit machine!!!
    > }
    > else if( sizeof(int) == 4 )
    > {
    > // ahh, mainstream life...
    > }
    > else if( sizeof(int) == 8 )
    > {
    > // where the {expletive} am I now?
    > }


    else if (sizeof(int) == 1)
    {
    // either a 16, 24, 32 bit platform (probably DSP)
    // or a Cray???
    }

    > If you're curious on your machine, just dump them:
    >
    > #include<iostream>
    >
    > int main( int argc, char* argv[] )
    > {
    > std::cout << "int " << sizeof(int) << std::endl;
    > std::cout << "long " << sizeof(long) << std::endl;
    > return 0;
    > }


    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Feb 28, 2005
    #20
    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,324
    Ron Natalie
    Oct 14, 2004
  2. blufox

    sizeof( int ) != sizeof( void * )

    blufox, May 22, 2006, in forum: C Programming
    Replies:
    2
    Views:
    554
    Joe Smith
    May 22, 2006
  3. Spiros Bousbouras
    Replies:
    40
    Views:
    1,474
    Keith Thompson
    Jan 20, 2007
  4. Stargazer

    Re: sizeof(const int *) > sizeof(int *)

    Stargazer, Jun 14, 2010, in forum: C Programming
    Replies:
    14
    Views:
    668
    Ben Bacarisse
    Jul 8, 2010
  5. Ben Bacarisse

    Re: sizeof(const int *) > sizeof(int *)

    Ben Bacarisse, Jun 14, 2010, in forum: C Programming
    Replies:
    12
    Views:
    428
    David Thompson
    Jul 2, 2010
Loading...

Share This Page