sizeof(long) in 64-bit C++

前端 未结 6 1391
清歌不尽
清歌不尽 2020-12-05 18:21

I have downloaded MinGW-64, so I can now compile 64-bit programs for Windows 7, using g++ 4.7.0 (experimental). But the following line:

cout << sizeof(         


        
6条回答
  •  没有蜡笔的小新
    2020-12-05 18:42

    MinGW is designed to build Windows applications, and the Microsoft platform ABI specifies that int and long have the same size of 32 bits. If MinGW defined long differently from MSVC, most existing Windows apps that use long would break when compiled using MinGW.

    Having said that, Cygwin x86_64 does follow the LP64 convention on Windows, just like on Linux (source).

    So you can use that to build a Windows app where the size of long is 8 bytes :)

    Test case:

    #include 
    #include 
    
    int CALLBACK WinMain(HINSTANCE a, HINSTANCE b, LPSTR c, int d)
    {
      char buf[100];
      snprintf(buf, sizeof(buf),
        "sizeof(int)=%d, sizeof(long)=%d, sizeof(long long)=%d\n",
         sizeof(int), sizeof(long), sizeof(long long));
      MessageBox(NULL, buf, "Cygwin Test", MB_OK);
      return 0;
    }
    

    Compile with: C:\cygwin64\bin\gcc.exe -mwindows -m64 cygwin-test.c -o cygwin-test

    Output:

提交回复
热议问题