Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
368 views
in Technique[技术] by (71.8m points)

2 Dimension Arrays memory segment fault in C++

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

So the problem is that you never return the array from the Creat_Array function. and so when you come to use the array you are using an uninitialised variable.

Write Creat_Array with a return value instead of a parameter, like this

int** Creat_Array()
{
    int** _pp_Created_Array = ...;
    ...
    return _pp_Created_Array;
}

int main()
{
    cout << "start creat array" <<endl;
    int** debugArray = Creat_Array();
    ...
}

Changing a variable inside a function does not change any variable outside the function. debugArray and _pp_Created_Array are two different variables.

Also this code is wrong

if(_pp_Created_Array == NULL)

new never returns NULL so this test will always be false. If new fails it throws an exception, it doesn't return NULL.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...