C++ c++;类分配的动态数组失败(内存泄漏)
edit1:添加一个运行的小版本。 我写了一个包含一些类的cpp文件。当我在单个文件中测试它时,一切正常,但是当我将它与其他c文件链接时,我存储在类中数组中的数据发生了变化。我知道我的内存分配肯定有问题,所以我用新的 但我不知道在哪里或如何修复C++ c++;类分配的动态数组失败(内存泄漏),c++,class,dynamic-memory-allocation,C++,Class,Dynamic Memory Allocation,edit1:添加一个运行的小版本。 我写了一个包含一些类的cpp文件。当我在单个文件中测试它时,一切正常,但是当我将它与其他c文件链接时,我存储在类中数组中的数据发生了变化。我知道我的内存分配肯定有问题,所以我用新的 但我不知道在哪里或如何修复 work in single .cpp file 在名为test.app的文件中 班级委员会 { 公众: 国际**网格; 布尔多; int结果; 公众: 董事会() { 网格=新整数*[3]; 对于(int i=0;i
work in single .cpp file
在名为test.app的文件中
班级委员会
{
公众:
国际**网格;
布尔多;
int结果;
公众:
董事会()
{
网格=新整数*[3];
对于(int i=0;i<3;++i){
网格[i]=新整数[3];
}
对于(int i=0;i您没有遵循5的规则。您在构造函数中有一些内存分配,因此您需要一个非默认析构函数,以便正确地释放所有内容,只要显式地使用move/copy构造函数和赋值运算符
如果可能的话,你应该坚持使用标准容器,比如std::vector
,它可以为你处理所有的角落案例。请创建合适的容器。这不会编译。arr[i][j]=new Board();
至少应该给你一个警告。你认为为什么会出现内存泄漏?arr[i][j]=new Board()编译正常,没有警告。因为以前的版本,当我不使用动态内存分配时,我声明了所有数组,没有指针,并且是新的,但数组中存储的数据在函数之间发生了更改,然后我使用新闻切换到动态分配,不是吗?因为我没有删除内存,因为它不应该被删除在当前步骤中。
Treenode *head; //treat as global variable
void agent_start( int this_player )
{
//nothing to do
//cout << "here all good" << endl;
head = new Treenode();
//cout << head << endl;
m = 0;
return;
}
int agent_second_move( int board_num, int prev_move )
{
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
{
if(head->arr[res_boardx][res_boardy].grid[cordx][cordy] == 1)
{
cout << "here cant print" << endl;
head->move2(i,j,-1);
cout << "here cant print" << endl;
}
else if(head->arr[res_boardx][res_boardy].grid[cordx][cordy] == -1)
{
cout << "here cant print" << endl;
head->move2(i,j,1);
}
}
}
in test.h
extern int port;
extern char *host;
#ifdef __cplusplus
extern "C" {
#endif
extern char *host;
#ifdef __cplusplus
}
#endif
#ifdef __cplusplus
extern "C" {
#endif
// parse command-line arguments
void agent_parse_args( int argc, char *argv[] );
// called at the beginning of a series of games
void agent_init();
// called at the beginning of each game
void agent_start( int this_player );
int agent_second_move(int board_num, int prev_move );
int agent_third_move(int board_num,int first_move,int prev_move);
int agent_next_move( int prev_move );
void agent_last_move( int prev_move );
// called at the end of each game
void agent_gameover( int result, int cause );
// called at the end of the series of games
void agent_cleanup();
#ifdef __cplusplus
}
#endif
in main.cpp
int main(int argc, char *argv[]){
agent_start(1);
int b = agent_second_move(1,1);
}
[1] 26904 illegal hardware instruction
segmentation fault
class Node
{
public:
Board arr[3][3]; ///
class Board
{
public:
int grid[3][3];
bool done;
int result;
public:
Board()
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
grid[i][j]=0;
}
}
done=false;
result=0;
}
}
class Node
{
public:
Board arr[3][3];
//double heuristic;
bool done;
int grid[3][3];
int result;
int prevx, prevy;
int next_turn;
public:
Node()
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
arr[i][j] = Board();
grid[i][j]=0;
}
}
done = false;
//heuristic=0;
result = 0;
prevx = -1;
prevy = -1;
next_turn=1;
}
}
Treenode *head;
head = new Treenode();
void print_map(){
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
{
int res_boardx, res_boardy, cordx, cordy;
res_boardx = (i-1)/3;
res_boardy = (i-1)%3;
cordx = (j-1)/3;
cordy = (j-1)%3;
cout << head->arr[res_boardx][res_boardy].grid[cordx][cordy];
}
cout << endl;
}
}
433000000
107312758200000000
000000000
000000000
000000000
000000000
00000-1000
000000000
000000000