1

I wrote a really simple program which returns errors like that:

C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\lib\crt2.o    In function `__tmainCRTStartup':
254     C:\crossdev\src\mingw-w64-v3-git\mingw-w64-crt\crt\crtexe.c (.text+0x1fe): relocation truncated to fit: R_X86_64_PC32 against symbol `__imp_Sleep' defined in .idata$5 section in C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\lib/libkernel32.a(dgnms01196.o)
281     C:\crossdev\src\mingw-w64-v3-git\mingw-w64-crt\crt\crtexe.c (.text+0x286): relocation truncated to fit: R_X86_64_PC32 against symbol `__imp_SetUnhandledExceptionFilter' defined in .idata$5 section in C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\lib/libkernel32.a(dgnms01183.o)
289     C:\crossdev\src\mingw-w64-v3-git\mingw-w64-crt\crt\crtexe.c (.text+0x2b4): relocation truncated to fit: R_X86_64_PC32 against symbol `__mingw_winmain_hInstance' defined in COMMON section in C:/Program Files (x86)/Dev-Cpp/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/../../../../x86_64-w64-mingw32/lib/../lib/crt2.o
315     C:\crossdev\src\mingw-w64-v3-git\mingw-w64-crt\crt\crtexe.c (.text+0x313): relocation truncated to fit: R_X86_64_PC32 against symbol `__mingw_winmain_lpCmdLine' defined in COMMON section in C:/Program Files (x86)/Dev-Cpp/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/../../../../x86_64-w64-mingw32/lib/../lib/crt2.o
241     C:\crossdev\src\mingw-w64-v3-git\mingw-w64-crt\crt\crtexe.c (.text+0x495): relocation truncated to fit: R_X86_64_PC32 against symbol `__imp_GetStartupInfoA' defined in .idata$5 section in C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\lib/libkernel32.a(dgnms00627.o)
C:\Program Files (x86)\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.9.2\libstdc++.a(ios_init.o) ios_init.cc:(.text$_ZNSt8ios_base4InitC2Ev+0x3c): relocation truncated to fit: R_X86_64_PC32 against symbol `__imp___iob_func' defined in .idata$5 section in C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\lib/libmsvcrt.a(dgrds00083.o)
C:\Program Files (x86)\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.9.2\libstdc++.a(ios_init.o) ios_init.cc:(.text$_ZNSt8ios_base15sync_with_stdioEb+0xc0): relocation truncated to fit: R_X86_64_PC32 against symbol `__imp___iob_func' defined in .idata$5 section in C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\lib/libmsvcrt.a(dgrds00083.o)
C:\Program Files (x86)\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.9.2\libstdc++.a(basic_file.o)   basic_file.cc:(.text$_ZN12_GLOBAL__N_1L6xwriteEiPKcx+0xd): relocation truncated to fit: R_X86_64_PC32 against symbol `__imp__errno' defined in .idata$5 section in C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\lib/libmsvcrt.a(dgrds00203.o)
C:\Program Files (x86)\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.9.2\libstdc++.a(basic_file.o)   basic_file.cc:(.text$_ZNSt12__basic_fileIcE8sys_openEP6_iobufSt13_Ios_Openmode+0x1b): relocation truncated to fit: R_X86_64_PC32 against symbol `__imp__errno' defined in .idata$5 section in C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\lib/libmsvcrt.a(dgrds00203.o)
C:\Program Files (x86)\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.9.2\libstdc++.a(basic_file.o)   basic_file.cc:(.text$_ZNSt12__basic_fileIcE5closeEv+0x33): relocation truncated to fit: R_X86_64_PC32 against symbol `__imp__errno' defined in .idata$5 section in C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\lib/libmsvcrt.a(dgrds00203.o)
C:\Program Files (x86)\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.9.2\libstdc++.a(basic_file.o)   basic_file.cc:(.text$_ZNSt12__basic_fileIcE6xsgetnEPcx+0xd): additional relocation overflows omitted from the output
C:\Users\admin\Desktop\nauka\informatyka\oki\collect2.exe   [Error] ld returned 1 exit status

Here is the code:

#include <bits/stdc++.h>
using namespace std;

const int j = 1e9+7;
int numbers[j];

int main(){ 
    int n;
    cin >> n;
    for (int i = 0; i<n; i++){
        cin >> numbers[i];  
    }
    return 0;
}

I know the bug is in the third line (with this big number because there's no bugs when I change 1e9 into 1e8), but I don't know how to fix it. Sorry for my English and thanks for help!

sahadamiral
  • 11
  • 1
  • 4
  • How many contiguous bytes of memory will `numbers` require? – Richard Critten May 30 '20 at 15:08
  • Does this answer your question? [Very large array - C array vs C++ array. Visual Studio - exceeds max (268435456)](https://stackoverflow.com/questions/31224825/very-large-array-c-array-vs-c-array-visual-studio-exceeds-max-268435456) – Raymond Chen Jul 16 '21 at 02:09
  • @RaymondChen: I think that question is about a completely different limitation. – Nate Eldredge Jul 16 '21 at 03:03

1 Answers1

1

gcc normally uses the "small" or "small PIC" code model on x86-64, which limits code plus static data (i.e. global or static) to 2 GB. Your array is nearly 4 GB, so you are exceeding that. You can try -mcmodel=medium or -mcmodel=large, but it has a performance penalty (for your entire program in the case of large), and I am not sure if there are issues with supporting it on MinGW.

A better solution for such a large array is to allocate it dynamically instead with malloc or new as appropriate. It avoids this issue, since the 2 GB limit applies only to static data. Other benefits include:

  • If insufficient memory is available, your program can detect the error and give a useful message or fall back to some other mode of operation. With a static array, if there's not enough memory, the program will just fail to start at all, and the system's error message may not be informative.

  • You can free the memory if you finish with it before your program terminates.

  • In this program, you could allocate enough space for just n ints, which would save memory if n is smaller than your constant j, and let the program still work if n is larger (as it stands you would write off the end of your array and cause undefined behavior, probably an exploitable security vulnerability).

Nate Eldredge
  • 48,811
  • 6
  • 54
  • 82