2017-11-09 10 views
0

작성한 유형의 배열을 초기화하는 데 문제가 있습니다.C++ 스택 오버플로 초기화 배열

다음 코드에서 볼 수있다 "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의 배열을 만듭니다. ...

그러나, "처리되지 않은 예외가 MST.exe의 0x00007FF6E91493D8에 있습니다. 0xC00000FD : 스택 오버플로 (매개 변수 : 0x0000000000000001, 0x0000002BFF003000).는 "즉시 프로그램은 메인 함수를 입력한다.

감사를 도와.

+0

'의 TreeNode의 imageTreeNodes [544] [1024] : 그래서 뭔가를 찾도록 main()을 변경. –

+0

Gaurav Sehgal이 제안한 것과 달리 프로그램에 의해 할당 된 스택의 양을 늘릴 수 있습니다. VS20xx에는 아마 다른 컴파일러에서 그런 옵션이 있다는 것을 기억합니다. – Scheff

+0

[SO : GNU 컴파일러로 컴파일하는 동안 Linux에서 C++ 응용 프로그램의 스택 크기 변경] (https://stackoverflow.com/q/2275550/7478597) 및이 [MSDN :/F (스택 크기 설정)] (https://msdn.microsoft.com/en-us/library/tdkhxaks.aspx). – Scheff

답변

1

당신은 당신의 배열로 스택에 너무 많은 메모리를 할당됩니다.

sizeof(TreeNode); // Returns 88 bytes 

그래서 544x1024 요소의 2 차원 배열을 할당 할 때 ~ 49MB를 스택에 할당하려고합니다! Windows Thread Documentation에 따르면 프로세스의 기본 스택 크기는 1Mb이므로 스택 o 발생하는 verflow 예외는 프로세스의 메모리가 부족하기 때문입니다.

프로세스 스택 크기를 늘릴 수 있지만 대신 힙에 배열을 할당하는 것이 좋습니다. 이것은 newdelete을 사용하여 수행 할 수 있지만 더 좋은 방법은 std::vector을 사용하는 것입니다. ;`힙이 할당 stack.Try에 너무 많은 메모리를 할당한다

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 
} 
+0

도움을 주셔서 감사합니다. 저는 벡터를 사용하기 시작했으며 훨씬 적은 오류로 동일한 결과를 얻고 있습니다! – Mee