Thread: sizeof int?

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    3

    sizeof int?

    I have a quick question: I just got a 64bit cpu(finally) and want to port my apps over to 64bit, I start up a test project and set it to x64; I code in some vars and one is set to sizeof(int). I get a 4 as the size of int, It was my assumption that int is 8 bytes on x64 and 4 on 32bit. is this the expected behavior, or is something amiss? I'd rather not have to start using __int64 for 64bit vars.


    I also have another question, you know how if you used __int64 on a 32bit project it would in assembler use the slow version of 64bit addition, is there any equivilant __int128 ?


    I'm using Microsoft Windows 7 (x64), Microsoft Visual Studio 2008 Team System

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    I suspect you'll have to use a long, not int, if you want 8 byte integers.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    I think on Linux with gcc amd64 an int is 4 bytes as well. Though a long is 8 bytes, where it is 4 bytes on x86.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Yeah I recall being slightly confused by this as well.

    It's actually the pointers that have doubled in size -- on 32-bit they are 4 bytes, but a 64-bit pointer is 8 bytes.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Maybe this will help.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I would suggest using size types if you want a variable to be of a specific size. They are available in C99 (the stdint.h header can be downloaded from Wikipedia, just look at the stdint.h entry), and also in Boost (called cstdint and lives in the boost namespace [C++ only]).
    The size of variables change depending on whether it's x64 or x86 and platform (OS), as well as compiler.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    int "should" be 64-bit in 64-bit mode, since all the general purpose registers are 64-bit, but for practical reasons (compatibility), on x86-64, it's still 32-bit.

    To get 64-bit everywhere, you can use "long long". To get 32-bit everywhere, use int. long is 32-bit on 32-bit, and 64-bit on 64-bit (like what int should be).

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Define "should". Why "should" int be 64 bits?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    http://www.open-std.org/JTC1/SC22/WG...docs/n1256.pdf

    Page 45.
    5 An object declared as type signed char occupies the same amount of storage as a
    ‘‘plain’’ char object. A ‘‘plain’’ int object has the natural size suggested by the
    architecture of the execution environment (large enough to contain any value in the range
    INT_MIN to INT_MAX as defined in the header <limits.h>).
    x86-64 has 64-bit GP registers and 64-bit pointers. What's a more "natural" size than 64-bit?

  10. #10
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    according to the standard, at least the last version i read, int is the size of the processors native integer type, which means technically on an x64 it should be a 64 bit integer, however, to not break existing code, most implementations retain int as a 32 bit integer and use long long or __int64 etc. for 64 bit integers.

  11. #11
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    For those of us that use MSVS and aren't concerned about the code compiling in another compiler we can just use the signed/unsigned int64 data type provided by the compiler.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Quote Originally Posted by previous quote
    5 An object declared as type signed char occupies the same amount of storage as a
    ‘‘plain’’ char object. A ‘‘plain’’ int object has the natural size suggested by the
    architecture of the execution environment (large enough to contain any value in the range
    INT_MIN to INT_MAX as defined in the header <limits.h>).
    Not mandatory, suggested.
    And since 32-bits is enough to hold the required INT_MIN to INT_MAX then all is well.

    A hell of a lot of code will implicitly depend on int being 32 bits, and changing the compiler would just break a load of things. Such major changes do not happen between patch levels of a compiler.

    Sooner or later (as it was when 16 bits moved to 32 bits), there will be compilers which have 64 bit integers "out of the box".
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    All I see is suggested, not why it should be 64-bit. To the processor, I think it hardly matters.
    And that's an interesting page right there. It claims some interesting stuff, yet that contradicts what everyone has mentioned so far.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Ah, so you are saying int shouldn't be 32-bit on x86-32 as well?

    We can equally well have 16-bit ints on x86-32, but does that make sense?

    Not mandatory, suggested.
    I think there is a subtle difference in our interpretation of the word "suggested" in this context.

    Note that the sentence didn't read
    A ‘‘plain’’ int object is suggested to have the natural size suggested by the
    architecture of the execution environment
    But instead
    A ‘‘plain’’ int object has the natural size suggested by the
    architecture of the execution environment
    So what you are saying is that the intrinsic features of the x86-64 architecture suggests a natural size of 32-bit? What size does x86-32 suggest, then?

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't really refer to the standard, either. Why should it be the 64 bits or 32 bits? Why, really, does it matter?
    Int could as well have been 16 bits on x86, yes, but it wasn't. No shame in that.

    Regardless, it's dangerous to rely on things being specific sizes. That's why we have common typedefs that allows us to create a variable of precise size.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  3. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM