2

I'm trying to run this code:

#include <iostream>
#include <time.h>
#include <stdlib.h>
#include<stdint.h>
#include "BSTTemplate.h"
#include "functions.h"

using namespace std;

int main()
{
    const __int64 SIZE = 1000000LL;
    __int64 randNum;
    binarySearchTree<__int64> bst1;
    __int64 numArray[SIZE];


    //I have more code after but the problem is occurring at the above code.

    return 0;
}

I'm not getting any syntax errors because it compiles but it crashes as soon it runs. I commented all the code that came after these declarations and it crashed in the same way. I tried using Step Into to debug it but it won't go past the first bracket '{' and instead it opens up some assembly code and an error that says the stack overflowed. Any help as to what I'm doing wrong? This is my first time using data types greater than ints. I tried looking up solutions but they either didn't help or I didn't understand them. I'm also using a 64 bit computer.

And I know that 1 million can fit into a long int but I'll be using numbers in the billions so I wanted to just make everything a long long int (or a __64int).

At EdMaster's request here is the code for BSTTemplate.h:

template <class type>
class binarySearchTree
{
private:
    template<class type>
    struct nodeType
    {
        type info;
        nodeType<type> * lLink;
        nodeType<type> * rLink;
    };

    nodeType<type> * root;

public:
    binarySearchTree();
    int height(nodeType<type> *p);
    bool search(const type &searchItem) const;
    void insert(const type &newData);
    void deleteNode(const type &deleteItem);
    void deleteFromTree(nodeType<type> * &p);

    int numDups;
};

Not sure how this would be the cause of the problem. None of the method definitions are allocating memory, except for the insert method one at a time.

David Velasquez
  • 2,346
  • 1
  • 26
  • 46
  • You're most likely running out of stack space. Try using `std::vector` for `numArray` as it allocates its memory from the heap. – Jonathan Potter Jun 23 '15 at 01:55
  • @JonathanPotter Answering in comments? – πάντα ῥεῖ Jun 23 '15 at 02:03
  • What machine are you using? the stack of normal computers should not have overflowed with a mere 1 million 64 bit integer array. I suspect you're corrupting the stack in the bst1 variable initialization, and using a vector seems to work only because it hides the problem in the stack by using the heap. Please provide the stack trace and the code for BSTTemplate.h – TheCppZoo Jun 23 '15 at 02:42
  • @EdMaster I'm not sure how to provide the stack trace but I did post the class code of BSTTemplate.h – David Velasquez Jun 23 '15 at 03:17
  • @EdMaster the OP is using `__int64`, which is not a standard type but MSVC's extension. On Windows the default stack size is 1MB which may not even able to store 1 million chars. Even on Linux, where default stack size is often 8MB, you still can't store 1 million 64-bit ints because the stack is also use for other variables and functions, not your array alone – phuclv Jun 23 '15 at 04:29
  • I don't know if you know this, but your code is implicitly calling the constructor of binarySearchTree for the variable bst1, as well as any global variables in functions.h and other globals in your program. You posted the declaration of `binarySearchTree::binarySearchTree` but not its implementation. One key question that you have not said is what kind of machine are you running this on. I reiterate a normal computer with enough megabytes of RAM won't stack-overflow with merely one million longs – TheCppZoo Jun 23 '15 at 04:29
  • @LưuVĩnhPhúc I did not know the stack size was so small in Windows, thanks – TheCppZoo Jun 23 '15 at 04:36
  • possible duplicate of [Segmentation fault on large array sizes](http://stackoverflow.com/questions/1847789/segmentation-fault-on-large-array-sizes) – phuclv Jun 23 '15 at 04:43
  • @Edmaster I have a pretty good computer. An i7 processor and 12GB of memory. Also, the constructor implementation is only setting root = NULL and numDups = 0. That is it. Plus I think Lưu Vĩnh Phúc answered your question. – David Velasquez Jun 23 '15 at 05:05

1 Answers1

2

"... and an error that says the stack overflowed ..."

const __int64 SIZE = 1000000LL;
// ...
__int64 numArray[SIZE];

is likely to require more stack memory than is available as configured for your process (you would need 7812 KB at least: (1000000 * 64 / 8) / 1024 ). Try using a dynamic memory allocation instead:

std::vector<__int64> numArray(SIZE);
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • Ah okay. I did not know you had to use vectors for large numbers. Thanks! – David Velasquez Jun 23 '15 at 02:00
  • 1
    @Ghost_Stark Not generally for large numbers, but for large memory allocations that won't fit the available stack size. – πάντα ῥεῖ Jun 23 '15 at 02:02
  • 1
    @Ghost_Stark that's called [*stack overflow*](http://stackoverflow.com/), which is one of the basic things you must learn. You need to use dynamic allocation instead of static allocation on stack for large variables. That said you can use any type of dynamic allocation, not that you must use vectors for them. Read more about it: [tag:stack-overflow] [Declare large array on Stack](http://stackoverflow.com/a/17029874/995714), [What and where are the stack and heap?](http://stackoverflow.com/q/79923/995714) – phuclv Jun 23 '15 at 04:36
  • @LưuVĩnhPhúc I know what stack overflow means, I just did not know how the stack was overflowing for allocating a million ints to an array. I wasn't even sure if that's where the problem was occurring. I didn't know the limits of the static allocation was that small. – David Velasquez Jun 23 '15 at 05:09