-6

I tried using 10^6 by 10^6 array ,it is giving me segmentation fault

Shubham Singhal
  • 17
  • 1
  • 1
  • 2
  • 2
    Depends on where you put the array. If that place has enough memory it should be fine, otherwise it will not be. – nwp Mar 01 '18 at 11:45
  • 7
    Well. even if each element was only one byte, the overall array's size was 10¹². Did you try to run it on a machine with at least 1000GB RAM? – bipll Mar 01 '18 at 11:45
  • 2
    The maximum size does not depend on the programming language, but on the machine, its current load, etc.: the execution context. – Cheers and hth. - Alf Mar 01 '18 at 11:50

3 Answers3

9

If the array is declared using automatic storage duration, then the size limit is remarkably small, at around 1Mb.

If you're using dynamic storage duration (using new and new[]), then then limit is much higher.

But either will be insufficient for your gigantic array (at the time of my writing)! You might be able to instantiate a sparse matrix of this rank though - see the BLAS library in the Boost distribution.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
3

You need a machine with at least four terabytes of RAM. Do you have such an expensive computer (perhaps costing a million euros, or at least several dozens of thousands of euros).

Since 1012 numbers of four bytes (e.g. int-s) each needs 4 terabytes. Adding all of them might take one or several hours (since a typical operation takes a nanosecond).

If you have an ordinary laptop with 16 Gbytes, you can at most have about a square matrix of about 40000 * 40000 elements, e.g. 1600 million elements (each taking perhaps four or eight bytes; since sizeof(long) and sizeof(double) are 8 bytes on my Linux x86-64 desktop).

If most elements of your matrix are zero, it is called a sparse matrix and you'll use different data structures to represent it. There are many resources (books, software libraries, mathematical theories, programming techniques, numerical analysis issues) related to them, and you could dedicate your professional life to sparse matrices.

Read also about hash-tables, databases, virtual memory, thrashing...

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
1

It simply depends on your computer's memory size and how much memory your O/S is allocating to your process. You can request your o/s how much memory your process need at the time of you are (executing) submitting the process to your O/S.

Abhijit Pritam Dutta
  • 5,521
  • 2
  • 11
  • 17