-3

We can store elements only up to a [10000000] (10^7) in a array of integers.Is there a way to store even more number of data's.And also what is the maximum size of character array.Please forgive if This question is repeated,I'll delete it then.

mozilla-firefox
  • 864
  • 1
  • 15
  • 23
  • 3
    Where did you get the 10000000 figure from? I'd think the only limit would be the available memory. – Carcigenicate Aug 15 '17 at 14:31
  • "_We can store elements only up to a [10000000] (10^7) in a array of integers_" Where did you get such number, from? The size of the array is only limitted by the stack size, if it is allocated as a variable with automatic storage duration, or the size of your RAM, if it's allocated dynamically (via use of `new`, `std::vector`, etc..), and even then, there's such OS features as swapping.. – Algirdas Preidžius Aug 15 '17 at 14:33
  • @Carcigenicate global declaration – mozilla-firefox Aug 15 '17 at 14:33
  • @Mukesh Then just increase the constant size? – Carcigenicate Aug 15 '17 at 14:34
  • @Carcigenicate I have tried to increase it to 10^8 but then Segmentation fault is coming SIGSEGV – mozilla-firefox Aug 15 '17 at 14:36
  • You don't have enough RAM in that case - why do you need all the numbers in one array? – doctorlove Aug 15 '17 at 14:37
  • @doctorlove I am coding on geeksforgeeks ide for a problem which requires elements upto 10^9.Hence I am stuck.My o/p for larger data is not correct. – mozilla-firefox Aug 15 '17 at 14:39
  • 1
    @Mukesh Typically, such sites impose certain memory/execution time constraints on your submitted programs. I doubt that it would allow your program to run with 3.75GB-ish memory usage. Refine your algorithm, so you wouldn't need arrays of such size. – Algirdas Preidžius Aug 15 '17 at 14:42
  • @Algirdas Preidzius I will refine it. Thanks. – mozilla-firefox Aug 15 '17 at 14:44
  • One data point: on my Ubuntu 15.10, 64 bit, desktop, the default stack size as reported by Posix 'pthread_attr_getstacksize()' is 8,720,384 bytes. Furthermore, each std::thread instantiated in 'main context' is also started with its own 8,720,384 bytes of stack. Not all the 8 Megs is available for a single big object (like a std::array), because the stack is also used for function call parameters, function return values, and other automatic variables. – 2785528 Aug 15 '17 at 17:31
  • "Hence I am stuck" - eh? your array can be new'd in dynamic memory. My system has ~3.5 GBytes of available. Lack of physical memory generally constrains heap, not automatic memory. – 2785528 Aug 15 '17 at 17:36

1 Answers1

4

What is the maximum number of elements that can be stored in array in C++?

In theory, an upper limit is the maximum value representable by std::size_t. This value is implementation defined. This is for objects of size 1 (bytes). You can only have half as many objects of size 2. The size of an integer type is implementation defined (other than the narrow character type).

In practice, other limitations exist. It depends on the storage duration, and the system.

The amount of memory for automatic variables is limited on most systems. Exactly how much depends on the system, and is sometimes configurable, but one to few megabytes is typical default on desktops.

Static variables and dynamic objects typically do not have such limitation, and the upper bound is the amount of main memory + swap space that the operating system gives to the process. The upper bound to that is the amount of memory + swap on the system minus the memory used by other processes and the operating system kernel.

Larger arrays can be stored on disk, and accessed using a paging system. The upper bound to such objects is limited by the amount of virtual memory, the file system, and the size of available storage.

eerorika
  • 232,697
  • 12
  • 197
  • 326