3

I have a assignment where I need to take a 7 digit input (a phone number) and check if it's found in the digits of pi. The digits of pi are stored in a supplied space separated text file. It seems reasonably straightforward: break the input into an array, read the digits of pi into an array, and check if a match is found. Long story short, I got the program working to my satisfaction. We were supplied text documents with the digits of pi in multiples of 10, 100, and so on up to 1 million digits. My program works up to 100,000 digits. But for whatever reason, on the 1 million digit file, it crashes with a generic windows error. I have no information on why it crashes and no error message is given (except the generic "a problem caused this program to stop working" message).

Noting that limits on the assignment state I cannot use any object-orientated code except for cin, cout, and the file stream objects (this limitation is because we've yet to get into classes and they don't want us using functions without knowing how they work).

Anyway, I'm looking for insight as to why the program is crashing. I'm using long ints on every variable that should need them (including counters and function returns), which should be sufficient, since they can go up to roughly 2 billion and there should not be any numbers larger than a million here.

Thanks for any help. I've been at this the past few hours with no success.

const long int numberOfDigits = 1000000;
int digitsOfPi[numberOfDigits];
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Kat
  • 4,645
  • 4
  • 29
  • 81
  • 2
    The stack isn't usually that big. Definitely not big enough to hold a million numbers. – chris Jan 16 '13 at 04:20
  • 1
    This sounds like a good assignment containing a valuable lesson. – Beta Jan 16 '13 at 04:23
  • Note that you can improve memory performance by realising that a single digit does not require a 32-bit number. You can use the `char` datatype here instead, which is 8-bits -- more than enough to hold the values 0 to 9 (which in fact only require 4 bits). I am *NOT* suggesting that this would make it okay to put a 1 million-byte array on the stack =) The given answer still applies. – paddy Jan 16 '13 at 04:38

1 Answers1

7
int digitsOfPi[numberOfDigits];

The stack does not have enough room to hold such a large array. The stack is where automatic variables (AKA local variables) are stored. Memory is automatically allocated for local variables when execution enters a function and is freed when the function returns. The stack is great because of this automatic memory management, but one restriction is that its size is limited.

Large objects should go on the heap.1 The heap is a gigantic pool of memory from which you can allocate pieces dynamically whenever you like. The difference between the heap and the stack is that you're responsible for allocating and freeing heap memory. It does not get automatically freed for you.

To allocate memory on the heap in C++, use the new operator, with each new having a corresponding delete to free the memory once it's no longer needed. (Or in our case, we use new[] and delete[] since we're dealing with an array.)

// Allocate memory on the heap.
int *digitsOfPi = new int[numberOfDigits];

// Use it.

// Then free it.
delete[] digitsOfPi;

// Or better yet, once you're allowed to use the STL...
std::vector<int> digitsOfPi;

The larger question, though, is why you need to read all the digits of π into memory at once. A better design, though trickier to code, would only need a fixed O(1) amount of memory—say, 7 digits at a time.

See also


1 You could explore your compiler's options to increase the stack size, but that's not the right solution.

Community
  • 1
  • 1
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • Thanks for your answer. That definitely works, and looking at what the "new" keyword is supposed to do, that makes sense, but can you explain what this "stack" is? – Kat Jan 16 '13 at 04:36
  • 3
    probably worth mention `delete []` as well? in case he doesn't know it? – billz Jan 16 '13 at 04:39
  • @mike Read this: http://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap – paddy Jan 16 '13 at 04:40
  • @KarthikT You don't have excessive seeking when reading a sequential file, and with input buffering, you don't have wasteful reads. It's fine to read 1 byte at a time. – paddy Jan 16 '13 at 04:43