How can i create an array of 10^9 size?

I have a project on school and i need to store 10^9 random numbers in an array for this purpose. I have tried a lot to do that but nothing is working. How can i store such big size random numbers? Please help.

std::vector<int> vec(1'000'000'000);
How large are the random numbers (range-wise)? Consider using short or signed char instead.
Last edited on
If an array must be used, try:

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

int main() {

    const std::size_t SZ = 1'000'000'000 ; // 10^9
    static int array[SZ] {} ; // *** important: static storage duration

    std::cout << "   sizeof(int) "  << sizeof(int) << '\n'
              << "            SZ " << SZ << '\n'
              << "sizeof(int)*SZ " << sizeof(int)*SZ << '\n'
              << " sizeof(array) " << sizeof(array) << '\n' ;
} 
That's a significant amount of space just to store random numbers.

What exactly are you trying to do? Perhaps we can suggest a better way.
If you're compiling a 32 bit program, there's a good chance that the solutions provided won't work because you're getting close to the maximum size of a 32 bit program.

As Duthomhas said, please explain the probem. There's a good chance that you don't have to store all of the those numbers at the same time.
JLBorges wrote:
static int array[SZ] {} ; // *** important: static storage duration

@JLBorges, could you please explain me better this point? Why is ‘static’ so important in this situation? It seems I can pass and modify the array the usual way even if it’s not static. Is it just because it’s so big that the risk of losing it if it falls out of scope is too expensive or there’s something I can’t spot?
In almost every implementation, storage for objects with automatic storage duration is allocated from the run-time stack of the thread. The amount of storage available in the run-time stack is limited; the program can run out of stack space.
JLBorges wrote:
The amount of storage available in the run-time stack is limited

Many thanks, JLBorges, I didn't know that.
Topic archived. No new replies allowed.