C++ Stack Overflow initialising array

0

I am having some problems initialising an array of a type I have created.

I have created "TreeEdge.h" and "TreeNode.h", which can be seen in the following code:

#pragma once
#include "TreeEdge.h"

class TreeNode {

TreeEdge northEdge;
TreeEdge eastEdge;
TreeEdge southEdge;
TreeEdge westEdge;
int xCoord;
int yCoord;

public:

// Default constructor
TreeNode() {

}

//constructor 2
TreeNode(int xInput, int yInput) {
    xCoord = xInput;
    yCoord = yInput;
}

void setEastSouthEdges(TreeEdge east, TreeEdge south) {
    eastEdge = east;
    southEdge = south;
}

void setAllTreeEdges(TreeEdge north, TreeEdge east, TreeEdge south, TreeEdge west) {
    northEdge = north;
    eastEdge = east;
    southEdge = south;
    westEdge = west;
}
};

and

#pragma once


class TreeEdge {

float weight;
int coords[4];

public:

TreeEdge() {

}

TreeEdge(int firstXCoord, int firstYCoord) {
    coords[0] = firstXCoord;
    coords[1] = firstYCoord;
}

void setWeight(float inputWeight) {
    weight = inputWeight;
}

float getWeight() {
    return weight;
}

void setStartCoords(int xCoord, int yCoord) {
    coords[0] = xCoord;
    coords[1] = yCoord;
}

int * getCoords() {
    return coords;
}

void setEndCoords(int xCoord, int yCoord) {
    coords[2] = xCoord;
    coords[3] = yCoord;
}
};

I am then trying to simply initialise an array of TreeNode, in the hope of doing something useful with it, using the following code...

#include "stdafx.h"
#include <opencv2/opencv.hpp>
#include <stdio.h>

#include "TreeEdge.h" 
#include "TreeNode.h"

using namespace cv;
using namespace std;

int main()
{
// create 2D array for tree
TreeNode imageTreeNodes[544][1024]; // ???????????????? way to use none fixed values

waitKey(0);
return 0;
}

However, I am getting an error: "Unhandled exception at 0x00007FF6E91493D8 in MST.exe: 0xC00000FD: Stack overflow (parameters: 0x0000000000000001, 0x0000002BFF003000)." as soon as the program enters the main function.

Thanks for your help.

Rob

c++
exception
stack
overflow
asked on Stack Overflow Nov 9, 2017 by Mee

1 Answer

1

You are allocating too much memory on the stack with your array.

 sizeof(TreeNode); // Returns 88 bytes

So when allocating a two dimensional array of 544x1024 elements you are trying to allocate ~49MB on the stack! According to the Windows Thread Documentation the default stack size for processes is 1Mb, so the stack overflow exception you're encountering is because the process is running out of memory.

While you can increase the process stack size it is likely a better idea to allocate the array on the heap instead. This could be done using raw new and delete but perhaps a better option is to use std::vector. So altering your main() to look something like:

int main()
{
  std::vector<std::vector<TreeNode>> array(544, std::vector<TreeNode>(1024,TreeNode())) ;
  std::cout << "Array number of rows: " << array.size() << std::endl; // Prints 544
  std::cout << "Array number of columns: " << array[0].size() << std::endl; // Prints 1022
}
answered on Stack Overflow Nov 9, 2017 by scott-eddy

User contributions licensed under CC BY-SA 3.0