I made dynamic allocation and array initialization functions with cpp, but I got a segmentation error The code I wrote is below.
#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*
}
}
}
And I checked the function function created
int main()
{
//debug
int** debugArray = nullptr;
cout << "start creat array" <<endl;
Creat_Array(debugArray);
cout << "start initial array" <<endl;
Initialize_Array(debugArray);
return 0;
}
and compile console is (VScode , g++)
start creat array
[_pp_Created_Array] memory first address : 0x8a6f40
start initial array
The terminal process "C:WindowsSystem32cmd.exe /d /c cmd /C
C:UserspangpanyprojectPathfinderTestmain" failed to launch (exit code:
3221225477).
But I got a segment fault error in void Initialize_Array(int** _pp_Initialized_Array) function
I can't figure out is there anyone who can help?
question from:
https://stackoverflow.com/questions/66059010/2-dimension-arrays-memory-segment-fault-in-c 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…