2

I need to create an array of size 1000000000.

#include<iostream>
#include<cstdlib>
#include<math.h>
#include<cmath>
using namespace std;
unsigned long long int arr[1000000000];
int ZofF(long long int n)
{
    for(unsigned long long int i=1;i<=1000000000;i++)
{
}
}
int main()
{
    return 0;
}

I get an error such as this:

C:\Users\TARUNM~1\AppData\Local\Temp\cccgeKSu.s Assembler messages: 63 C:\Users\TARUNM~1\AppData\Local\Temp\cccgeKSu.s Error: value of 00000001dcd6504e too large for field of 4 bytes at 000000000000004e 87 C:\Users\TARUNM~1\AppData\Local\Temp\cccgeKSu.s Error: value of 00000001dcd6507e too large for field of 4 bytes at 000000000000007e


There was a solution: How to handle an array with size 1,000,000,000 in C++?

I have tried to divide it into multiple arrays, I didn't actually understand the solution from the above link. Here is the code:

    #include<iostream>
    #include<cstdlib>
    #include<math.h>
    #include<cmath>
    using namespace std;
    unsigned long int arr[125000000];
    unsigned long int ar2[125000000];
    unsigned long int ar3[125000000];
    unsigned long int ar4[125000000];
    unsigned long int ar5[125000000];
    unsigned long int ar6[125000000];
    unsigned long int ar7[125000000];
    unsigned long int ar8[125000000];
    int ZofF(long long int n)
    {
        for(unsigned long long int i=1;i<=250000000;i++)
        {
        }
    }
    int main()
    {
        return 0;
    }

But this code gave me the following error.

c:\program files (x86)\dev-cpp\mingw64\x86_64-w64-mingw32\lib\crt2.o In function check_managed_app': 357 h:\crossdev\src\mingw-w64-v3-svn\mingw-w64-crt\crt\crtexe.c (.text+0x21): relocation truncated to fit: R_X86_64_PC32 against symbolmingw_initltsdrot_force'

defined in .bss section in C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\lib/libmingw32.a(lib64_libmingw32_a-tlssup.o) 358
h:\crossdev\src\mingw-w64-v3-svn\mingw-w64-crt\crt\crtexe.c (.text+0x2b): relocation truncated to fit: R_X86_64_PC32 against symbol `mingw_initltsdyn_force'

defined in .bss section in C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\lib/libmingw32.a(lib64_libmingw32_a-tlssup.o) 359
h:\crossdev\src\mingw-w64-v3-svn\mingw-w64-crt\crt\crtexe.c (.text+0x35): relocation truncated to fit: R_X86_64_PC32 against symbol `mingw_initltssuo_force'

defined in .bss section in C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\lib/libmingw32.a(lib64_libmingw32_a-tlssup.o) 360
h:\crossdev\src\mingw-w64-v3-svn\mingw-w64-crt\crt\crtexe.c (.text+0x3f): relocation truncated to fit: R_X86_64_PC32 against symbol `mingw_initcharmax' defined in

.bss section in C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\lib/libmingw32.a(lib64_libmingw32_a-charmax.o) c:\program files (x86)\dev-cpp\mingw64\x86_64-w64-mingw32\lib\crt2.o In function pre_c_init': 129
h:\crossdev\src\mingw-w64-v3-svn\mingw-w64-crt\crt\crtexe.c (.text+0x51): relocation truncated to fit: R_X86_64_PC32 against symbol
mingw_app_type' defined in

.bss section in C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\lib/libmingw32.a(lib64_libmingw32_a-mingw_helpers.o) 135
h:\crossdev\src\mingw-w64-v3-svn\mingw-w64-crt\crt\crtexe.c (.text+0x71): relocation truncated to fit: R_X86_64_PC32 against symbol `_fmode' defined in .bss

section in C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\lib/libmingw32.a(lib64_libmingw32_a-xtxtmode.o) 133
h:\crossdev\src\mingw-w64-v3-svn\mingw-w64-crt\crt\crtexe.c (.text+0x78): relocation truncated to fit: R_X86_64_PC32 against symbol `__onexitend' defined in COMMON

section in C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\lib/libmingw32.a(lib64_libmingw32_a-atonexit.o) 133
h:\crossdev\src\mingw-w64-v3-svn\mingw-w64-crt\crt\crtexe.c (.text+0x7f): relocation truncated to fit: R_X86_64_PC32 against symbol `__onexitbegin' defined in

COMMON section in C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\lib/libmingw32.a(lib64_libmingw32_a-atonexit.o) 135
h:\crossdev\src\mingw-w64-v3-svn\mingw-w64-crt\crt\crtexe.c (.text+0x86): relocation truncated to fit: R_X86_64_PC32 against symbol `__imp__fmode' defined in

.idata$5 section in C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\lib/libmsvcrt.a(dqows00253.o) c:\program files (x86)\dev-cpp\mingw64\x86_64-w64-mingw32\lib\crt2.o In function pre_cpp_init': 157
h:\crossdev\src\mingw-w64-v3-svn\mingw-w64-crt\crt\crtexe.c (.text+0x136): relocation truncated to fit: R_X86_64_PC32 against symbol
_newmode' defined in .bss

section in C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\lib/libmingw32.a(lib64_libmingw32_a-_newmode.o) 162
h:\crossdev\src\mingw-w64-v3-svn\mingw-w64-crt\crt\crtexe.c (.text+0x13d): additional relocation overflows omitted from the output [Error] ld returned 1 exit status

Please help me understand why this happens and how to create an array of size 1000000000. I use a 64 bit Windows 8.1 on i5 core with 4Gb RAM. Do I have to upgrade my hardware?

Community
  • 1
  • 1
Tarun Maganti
  • 3,076
  • 2
  • 35
  • 64

3 Answers3

0

Why do you need to create an array of size 1000000000? What problem are you trying to solve here? Maybe a std::vector would be a better choice depending on what type of data you're trying to store. Again, it really depends on what kind of problem you're trying to solve here.

Andrew
  • 1,581
  • 3
  • 18
  • 31
  • I'm trying to store number of zeros at the end of a particular number's factorial. To do that, I am using the indices of an array as the number and array's content as the number of zeros. For the problem I need to solve the input is as large as 1000000000. So, I had a need to use the arrays with size as large as 1000000000. But, I'll try using std::vector. Thank you. – Tarun Maganti Apr 20 '15 at 13:40
0

long long int has at least 64 bits in size. 1000000000 * 64 is about 8 Gb, which is larger than your RAM. This would be your physical memory limit.

Another limit is the maximum size of std::size_t set in your system. You can find it out by following:

#include <iostream>
#include <limits>

int main () {
    std::cout << std::numeric_limits<size_t>::max()<<std::endl;
    return 0;
}

BUT, like Andrew said, it is never a common way to solve any problem like this. I recommend you to upgrade your code rather than your hardware :)

KinoP
  • 1,532
  • 15
  • 23
0

Your question is not quite clear as to when the errors are reported, but it looks to me like it is during compile or link, not run time.

If true, it follows that this is nothing to do with your hardware, but a limitation of your compiler/linker/loader.

You could check the documentation to determine whether there are options to allow larger arrays.

However, as per other linked answers, you should almost certainly be using dynamic memory allocation here, not a statically defined array.

When doing so, make sure to check for an allocation failure.

    const size_t numElements = 1000000000;
    unsigned long long* arrayData = new unsigned long long[numElements];
    if (arrayData == nullptr)
    {
        // Problem.
    }
    else
    {
        // All good.
        // Use it.
        // Clean up using array delete.
        delete[] arrayData;
    }

Now, you might still need to keep the overall size below 4GB, so chunk into multiple underlying arrays as per your other code.

If you do all this, you will still hit a problem due to lack of system memory at run time.

You can either buy more RAM ( and you'll want 8GB for this as well as more for the rest of the system), or rethink your approach, dividing the problem into small pieces.

Keith
  • 6,756
  • 19
  • 23
  • The errors were compiler errors. I'm sorry I didn't mention it before. But, dynamic allocation helped. Thank you. I may be able to solve the problem. There were no compilation errors. – Tarun Maganti Apr 20 '15 at 14:04
  • I have understood this but I have a doubt. According to Mr.@KinoP (comment below this one) If my 4Gb RAM couldn't allocate required memory. How did it allocate when I have dynamically allocated it. Am, I missing something about dynamic allocation? Is it possible that an error will occur if the memory limit exceeds again i.e., the dynamically allocated memory will reach 4Gb by filling up the data? – Tarun Maganti Apr 20 '15 at 14:11
  • Yes, see bolded text. – Keith Apr 20 '15 at 23:17