2 C++ 中的维度数组内存段错误
2 Dimension Arrays memory segment fault in C++
我用cpp做了动态分配和数组初始化函数,但是出现分段错误 我写的代码如下。
#include <iostream>
#define User_Height 100
#define User_Width 100
using namespace std;
void Creat_Array(int** _pp_Created_Array)
{
_pp_Created_Array = new int*[User_Width];
for (int x = 0; x < User_Height ; x++)
{
_pp_Created_Array[x] = new int[User_Height];
}
if(_pp_Created_Array == NULL)
{
cout<<"""fail to alloc memory.""" <<endl;
return;
}
else
{
cout << "[_pp_Created_Array] memory first address : ";
cout << _pp_Created_Array << endl << endl;
}
}
void Initialize_Array(int** _pp_Initialized_Array)
{
for (int x = 0; x < User_Width; x++)
{
for (int y = 0; y < User_Height; y++)
{
_pp_Initialized_Array[x][y] = 0; //*segment fault*
}
}
}
并且我检查了创建的函数函数
int main()
{
//debug
int** debugArray = nullptr;
cout << "start creat array" <<endl;
Creat_Array(debugArray);
cout << "start initial array" <<endl;
Initialize_Array(debugArray);
return 0;
}
编译控制台是 (VScode , g++)
start creat array
[_pp_Created_Array] memory first address : 0x8a6f40
start initial array
The terminal process "C:\Windows\System32\cmd.exe /d /c cmd /C
C:\Users\pangpany\project\PathfinderTest\main" failed to launch (exit code:
3221225477).
但是我在 void Initialize_Array(int** _pp_Initialized_Array) 函数中遇到了段错误
我不知道有没有人可以帮忙?
所以问题是您永远不会 return 来自 Creat_Array
函数的数组。因此,当您开始使用数组时,您使用的是未初始化的变量。
用 return 值而不是参数写入 Creat_Array
,像这样
int** Creat_Array()
{
int** _pp_Created_Array = ...;
...
return _pp_Created_Array;
}
int main()
{
cout << "start creat array" <<endl;
int** debugArray = Creat_Array();
...
}
在函数内部更改变量不会更改函数外部的任何变量。 debugArray
和 _pp_Created_Array
是两个不同的变量。
这个代码也是错误的
if(_pp_Created_Array == NULL)
new
永远不会 returns NULL
所以这个测试永远是错误的。如果 new
失败,它会抛出异常,它不会 return NULL
.
我用cpp做了动态分配和数组初始化函数,但是出现分段错误 我写的代码如下。
#include <iostream>
#define User_Height 100
#define User_Width 100
using namespace std;
void Creat_Array(int** _pp_Created_Array)
{
_pp_Created_Array = new int*[User_Width];
for (int x = 0; x < User_Height ; x++)
{
_pp_Created_Array[x] = new int[User_Height];
}
if(_pp_Created_Array == NULL)
{
cout<<"""fail to alloc memory.""" <<endl;
return;
}
else
{
cout << "[_pp_Created_Array] memory first address : ";
cout << _pp_Created_Array << endl << endl;
}
}
void Initialize_Array(int** _pp_Initialized_Array)
{
for (int x = 0; x < User_Width; x++)
{
for (int y = 0; y < User_Height; y++)
{
_pp_Initialized_Array[x][y] = 0; //*segment fault*
}
}
}
并且我检查了创建的函数函数
int main()
{
//debug
int** debugArray = nullptr;
cout << "start creat array" <<endl;
Creat_Array(debugArray);
cout << "start initial array" <<endl;
Initialize_Array(debugArray);
return 0;
}
编译控制台是 (VScode , g++)
start creat array
[_pp_Created_Array] memory first address : 0x8a6f40
start initial array
The terminal process "C:\Windows\System32\cmd.exe /d /c cmd /C
C:\Users\pangpany\project\PathfinderTest\main" failed to launch (exit code:
3221225477).
但是我在 void Initialize_Array(int** _pp_Initialized_Array) 函数中遇到了段错误 我不知道有没有人可以帮忙?
所以问题是您永远不会 return 来自 Creat_Array
函数的数组。因此,当您开始使用数组时,您使用的是未初始化的变量。
用 return 值而不是参数写入 Creat_Array
,像这样
int** Creat_Array()
{
int** _pp_Created_Array = ...;
...
return _pp_Created_Array;
}
int main()
{
cout << "start creat array" <<endl;
int** debugArray = Creat_Array();
...
}
在函数内部更改变量不会更改函数外部的任何变量。 debugArray
和 _pp_Created_Array
是两个不同的变量。
这个代码也是错误的
if(_pp_Created_Array == NULL)
new
永远不会 returns NULL
所以这个测试永远是错误的。如果 new
失败,它会抛出异常,它不会 return NULL
.