Allocating huge arrays

Hey I need to do a binary search on an array of 2 million elements but my programs bugging out at a char array of 1,100,000. A char array of 1,000,000 is the most my PC is handling. 2,000,000 chars should be 16,000,000 bits. My ram is 8 GB which should be 68,719,476,736(68 billion) bits which I think should be able to hold the 2 million chars. Does anyone know whats going on?

This code bugs out...

#include<iostream>
using namespace std;
int main()
{
char arr[2000000];
return 0;
}
1
2
3
4
5
int main()
{
    // storage duration of arr changed from automatic to static
    static char arr[2000000];
}
Thanks that fixed it.
Thanks that fixed it.

Do you want to know why?
http://www.learncpp.com/cpp-tutorial/79-the-stack-and-the-heap/
http://stackoverflow.com/questions/408670/stack-static-and-heap-in-c

Do you want to use more of C++?
http://www.cplusplus.com/reference/vector/vector/

1
2
3
4
5
6
#include <vector>

int main()
{
    std::vector<char> vc(2 * 1000 * 1000);
}

Last edited on
> Do you want to use more of C++?
> http://www.cplusplus.com/reference/vector/vector/

@Catfish4 +1


> Do you want to know why?
> http://www.learncpp.com/cpp-tutorial/79-the-stack-and-the-heap/
> http://stackoverflow.com/questions/408670/stack-static-and-heap-in-c

With the caveat that we are getting into implementation specific details here.

The C++ standard does not mention either stack storage or heap storage; instead it uses the abstract concept of 'storage durations' - static storage duration, automatic storage duration, dynamic storage duration and in C++11, thread storage duration. In simple terms, the standard is only concerned about the lifetime of objects (and the duration for which the memory for an object would be available). The standard library provides a default implementation of the allocator for objects with a dynamic storage duration. It too does not talk of a heap - conceptually, this memory comes from the 'free store'.

An implementation can store all objects in a program on a run-time stack, or on several different stacks, or on one heap, or many arenas (which may contain individual heaps of their own), or in a memory mapped disk file, or anywhere else that it pleases for that matter - as long as it ensures that the lifetime of objects conform to the storage durations as specified by the IS.
Topic archived. No new replies allowed.