【发布时间】:2023-04-09 08:26:01
【问题描述】:
我在初始化我创建的类型的数组时遇到了一些问题。
我已经创建了“TreeEdge.h”和“TreeNode.h”,可以在下面的代码中看到:
#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;
}
};
和
#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;
}
};
然后我尝试使用以下代码简单地初始化一个 TreeNode 数组,希望对它做一些有用的事情...
#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;
}
但是,我收到一个错误:“MST.exe 中 0x00007FF6E91493D8 处未处理的异常:0xC00000FD:堆栈溢出(参数:0x0000000000000001、0x0000002BFF003000)。”随着程序进入主函数。
感谢您的帮助。
罗伯
【问题讨论】:
-
TreeNode imageTreeNodes[544][1024];在堆栈上分配了太多内存。尝试在堆上分配。 -
作为 Gaurav Sehgal 建议的替代方案,您可以提高由/为您的程序分配的堆栈数量。我记得在 VS20xx 中有这样的选项,可能在其他编译器中也是如此。
标签: c++ exception stack overflow