-2
int *tthousand = new int[10000];  
int *hthousand = new int[100000];
static int tmillion[10000000];

Hello there,

I'm trying to dynamically allocate memory for a series of arrays. These arrays are then populated with random numbers ranging from 0 to 10,000,000. From there, they are sorted using a quicksort/selection sort. The problem I'm running into is that whenever the "tmillion" array is reached, the program will run for a time and then a stack overflow occurs.

I've tried writing it as:

int *tmillion = new int[10000000];

and..

static int tmillion[10000000];

I must be missing something simple. Apologies, I'm still a bit new to C++.

Thoughts?

John3136
  • 28,809
  • 4
  • 51
  • 69
  • 1
    Did you check the result of `new`? Have you debugged to find the source of the crash? – John3136 Sep 28 '15 at 04:40
  • 1
    Works for me. http://ideone.com/qmvKpT – R Sahu Sep 28 '15 at 04:44
  • 2
    You could always try to post a minimal but complete example. But that would maybe be too easy on the readers. – Cheers and hth. - Alf Sep 28 '15 at 04:50
  • 2
    Dynamically allocate != `static int tmillion[10000000]`. That is called static allocation. If you leave the `static` off, you are allocating from the stack and 10 million integers will definitely overflow the stack on most machines (that is 40 MB and most stacks are typically 16 MB). – Mark Lakata Sep 28 '15 at 05:15
  • Many systems will let you allocate virtual address space, but may still SIGSEGV when you use too much of it. That famously allows sparse arrays, where only a few indices are used out of a large possible range, to be practical on such systems. There's not much you can do about it except disable "overcommit" at an OS level, if you prefer an early, orderly failure, or buy more memory. The issue's not specific to C++. Still, you'd have to be on a small (embedded?) device to lack 40MB.... – Tony Delroy Sep 28 '15 at 05:41
  • Here's an answer I wrote about how to experimentally determine your thread stack size: https://stackoverflow.com/a/64085509/4561887. If you do something similar except without threads, you can experimentally determine your max stack size for a single-threaded program on your system until the stack overflows. `static int tmillion[10000000];` is static allocation and is likely too large for your stack, causing stack overflow. – Gabriel Staples Feb 13 '21 at 18:06

1 Answers1

-1

Just for future reference. Since C++11 std::array was introduced in Standard Library. It should be preferred than C-like built-in array.

Example:

#include <array>
std::array<int, 10000000> myArray;
// fill the array with data
std::sort(myArray.begin(); myArray.end()); // sorts data using default operator <

More information about this container can be found on Bjarne homepage: http://www.stroustrup.com/C++11FAQ.html#std-array

The standard container array is a fixed-sized random-access sequence of elements defined in . It has no space overheads beyond what it needs to hold its elements, it does not use free store, it can be initialized with an initializer list, it knows its size (number of elements), and doesn't convert to a pointer unless you explicitly ask it to. In other words, it is very much like a built-in array without the problems.

Additional example:

#include <array> // std::array
#include <algorithm> // std::sort
#include <iostream> // std::cout

int main()
{
    std::array<int, 10> myArray = {8, 3, 6, 7}; // array with 10 elements but create it with declaring only first 4 elements

    std::cout << "BEFORE SORT:" << std::endl;
    for (const auto& element : myArray)
    {
        std::cout << element << std::endl;
    }

    std::sort(myArray.begin(), myArray.end()); // sorts data using default operator <

    std::cout << "AFTER SORT:" << std::endl;
    for (const auto& element : myArray)
    {
        std::cout << element << std::endl;
    }
}

Output:

BEFORE SORT:
8
3
6
7
0
0
0
0
0
0
AFTER SORT:
0
0
0
0
0
0
3
6
7
8
dptd
  • 274
  • 1
  • 3
  • 12
  • 2
    Per the text you quote *"it does not use free store"* - so a stack-hosted `std::array` as you've illustrated will still blow the stack with the size mentioned in the question on typical systems. – Tony Delroy Sep 28 '15 at 06:06
  • 1
    That's correct. He can still allocate it on the heap and wrap it around using unique_ptr though. `std::unique_ptr myArray = std::make_unique(10000000);` – dptd Sep 28 '15 at 07:25
  • 1
    This answer alone doesn't really address the nature of the crash in the question, which appears to be stack overflow due to statically allocating more than the stack can handle. I'm downvoting it until it addresses the question more fully instead of just saying to use a different and unnecessary C++ type which will still have the same problem. – Gabriel Staples Feb 13 '21 at 17:53