Coffeehouse Thread

23 posts

Who changed C++ while I wasn't looking!?

Back to Forum: Coffeehouse
  • User profile image
    DoomBringer

    I'm porting a binary communication format from Java to C++ (its all over the wire stuff...) and I am working on some conversion stuff.
    So I'm working on converting ints and longs and stuff properly, and I noticed that sizeof(long) == sizeof(int)

    :O

    (This is all in VS2005, of course, finally I'm using it, and it is very nice)
    I've been doing a lot of work in Java land up until now, and I guess C++ went and changed on me (at least the MSVC++ parts...).  Now, to get a 8byte variable, its long long.

    Perplexed

    I'm stuck back in my raw C roots I guess, when memory management was malloc() (which I still use often enough) and free(), and pointers were king.

  • User profile image
    Antitorgo

    int and long in C++ have been synonymous for as long as I can remember. I think maybe you've been Java-tized too much and remember incorrectly or something.

    long long or __int64 are the 64-bit equivalents to Java that you are looking for... Also, IIRC endianess will differ (at least on x86 systems, since Java is always big-endian and x86 is little-endian (which always made me wonder if that caused a performance hit))

  • User profile image
    amotif

    If I recall correctly VC sizeof(int) tracked the register size up to the 32-bit processors. But--going forward to x64 code--int gets stuck at 32 bits. (If I've gotten it right, of course.) I don't know what rhyme or reason was applied in that decision. Smiley

    Edit: To be clear, I've worked with 16 bit ints. The landscape changes every few years.

  • User profile image
    blowdart

    Antitorgo wrote:
    int and long in C++ have been synonymous for as long as I can remember.



    Youngster.

    I remeber sizeof(int) == sizeof(char) != sizeof(long)

    Mind you that was under DOS. And Netware. on 8086s.

  • User profile image
    Cybermagell​an

    Sorry I changed C++, I'll put it back the way I left it when I'm done.

  • User profile image
    Sven Groot

    Nobody changed C++. The sizes of the integral data types are specifically left implementation defined in the standard. All the standard says is that char should be big enough to contain all characters of the default codepage used by the implementation, and that short <= int <= long. Long long is not in fact part of the standard at all.

    On 32 bit windows, it has always been sizeof(int) == sizeof(long) == 4.

  • User profile image
    Antitorgo

    blowdart wrote:
    
    Antitorgo wrote: int and long in C++ have been synonymous for as long as I can remember.



    Youngster.

    I remeber sizeof(int) == sizeof(char) != sizeof(long)

    Mind you that was under DOS. And Netware. on 8086s.


    Hmm... I'm pretty sure that in C++ ints and longs were always 32-bit, but my memory of that time ('91-'92) is fuzzy and hazy. Are you sure you aren't thinking of C? Seeing as how the 80386 was out when C++ was introduced (to the masses), I think it had been 32-bit by then...

    Anyway, I always tended to not use longs because they were nubulous and ill-defined. Seeing as how any C++ work I do is in MS-land, I guess I'd use __int64 because long long just doesn't flow for me... either that, or maybe a #define long64 __int64 or something then if it ever changed, i could just change that line... blah.

  • User profile image
    blowdart

    Antitorgo wrote:
    
    blowdart wrote:
    Antitorgo wrote: int and long in C++ have been synonymous for as long as I can remember.



    Youngster.

    I remeber sizeof(int) == sizeof(char) != sizeof(long)

    Mind you that was under DOS. And Netware. on 8086s.


    Hmm... I'm pretty sure that in C++ ints and longs were always 32-bit, but my memory of that time ('91-'92) is fuzzy and hazy. Are you sure you aren't thinking of C? Seeing as how the 80386 was out when C++ was introduced (to the masses), I think it had been 32-bit by then...


    But it depends on the architecture surely? Ah so long ago

  • User profile image
    Massif

    I'm with Sven and Blowdart - I think int's been 32bit on x86 since the 386.

    I can't wait to see how many of the apps we've got here fail once a sizeof(pointer) no longer equals sizeof(DWORD) - there are too many relying on that. (Not that I care - got a new job in a couple of weeks.)

  • User profile image
    Sven Groot

    Massif wrote:
    I'm with Sven and Blowdart - I think int's been 32bit on x86 since the 386.

    Not quite. It's been 32 bit since 32 bit windows (i.e. Windows NT, Windows 95). With 16 bit Windows, int was 16 bit, even if you ran it on a 486.

    And like I said, the standard says it's implementation defined. With that they mean the compiler. The fact that all compilers targetting Win32 use the same sizes is just lucky. I could just as easy make a C++ compiler for Win32 where an int is 412 bits. It wouldn't make sense, but I could, and it would still be a conforming implementation according to the standard.

  • User profile image
    Cairo

    #include <stdint.h>

    ... then you can use things like int32_t, uint64_t, etc.

  • User profile image
    wkempf

    Cairo wrote:
    #include <stdint.h>

    ... then you can use things like int32_t, uint64_t, etc.



    With the exception of Sven, every single one of you need to purchase the standard.  Sheesh.

  • User profile image
    Cairo

    wkempf wrote:
    
    Cairo wrote:#include <stdint.h>

    ... then you can use things like int32_t, uint64_t, etc.



    With the exception of Sven, every single one of you need to purchase the standard.  Sheesh.


    Please elaborate.

  • User profile image
    Sven Groot

    Cairo wrote:
    Please elaborate.

    stdint.h is not part of the C++ standard (it's part of C99, but not of any currently released version of C++, maybe they plan to include it in C++0x, but that's besides the point).

    More importantly, VC doesn't have the stdint.h header.

  • User profile image
    Cairo

    Sven Groot wrote:
    
    Cairo wrote:Please elaborate.

    stdint.h is not part of the C++ standard (it's part of C99, but not of any currently released version of C++, maybe they plan to include it in C++0x, but that's besides the point).

    More importantly, VC doesn't have the stdint.h header.


    Ahhh. Thanks, I see now. Too bad VC doesn't ship stdint.h, or something like it; it's handy.

    How about using something like Paul Hsieh's "portable stdint.h"?


  • User profile image
    wkempf

    Cairo wrote:
    
    Sven Groot wrote: 
    Cairo wrote: Please elaborate.

    stdint.h is not part of the C++ standard (it's part of C99, but not of any currently released version of C++, maybe they plan to include it in C++0x, but that's besides the point).

    More importantly, VC doesn't have the stdint.h header.


    Ahhh. Thanks, I see now. Too bad VC doesn't ship stdint.h, or something like it; it's handy.

    How about using something like Paul Hsieh's "portable stdint.h"?




    In this case, it's not handy.  It's not part of the standard, so it's not portable.  If you're not writing portable code, you have no use for what stdint.h provides.  A "portable" substitute is not a bad idea, but VC including a non-portable version would be pointless.

    But <stdint.h> was the least of the issues with what others have been posting.  "int was always 32 bit" type quotes are very telling about the quality of software engineers we have in this thread.

  • User profile image
    DoomBringer

    I must be thinking of C then.  My first programming language was C, and I'm almost certain ints and longs were different.

    And you're all right that it is implementation specific, generally varying by hardware. 

    I was just thinking of old days I suppose. 

  • User profile image
    Mog0

    I learned C++ using Visual C++ 1.0 and that did have 16bit ints so it isn't a straight forward C vs C++.

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.