I have 2 pieces code:
gb_Graph = (int **)malloc(sizeof(int*)*gb_nVertices);
if (gb_Graph == NULL)
return false;
gb_Open = (VERTEX *)malloc(sizeof(VERTEX*)*gb_nVertices);
if (gb_Open == NULL)
return false;
gb_Close = (VERTEX *)malloc(sizeof(VERTEX*)*gb_nVertices);
if (gb_Close == NULL)
return false;
for (i = 0; i < gb_nVertices; i++)
{
gb_Graph[i] = (int*)malloc(sizeof(int)*gb_nVertices);
if (gb_Graph[i] == NULL)
return false;
for (j = 0; j<gb_nVertices; j++)
fscanf(gb_fInput, "%d", &(gb_Graph[i][j]));
}
for (i = 0 ; i<gb_nVertices; i++)
{
gb_Open[i].Exist = false;
gb_Open[i].ParentName = -1;
gb_Open[i].CostPath = 0;
fscanf(gb_fInput, "%d", &(gb_Open[i].CostHeuristic));
gb_Close[i].Exist = false;
gb_Close[i].ParentName = -1;
gb_Close[i].CostPath = 0;
gb_Close[i].CostHeuristic = gb_Open[i].CostHeuristic;
}
gb_Open[gb_nStart].Exist = true;
And
gb_Graph = (int **)malloc(sizeof(int*)*gb_nVertices);
if (gb_Graph == NULL)
return false;
for (i = 0; i < gb_nVertices; i++)
{
gb_Graph[i] = (int*)malloc(sizeof(int)*gb_nVertices);
if (gb_Graph[i] == NULL)
return false;
for (j = 0; j<gb_nVertices; j++)
fscanf(gb_fInput, "%d", &(gb_Graph[i][j]));
}
gb_Open = (VERTEX *)malloc(sizeof(VERTEX*)*gb_nVertices);
if (gb_Open == NULL)
return false;
gb_Close = (VERTEX *)malloc(sizeof(VERTEX*)*gb_nVertices);
if (gb_Close == NULL)
return false;
for (i = 0 ; i<gb_nVertices; i++)
{
gb_Open[i].Exist = false;
gb_Open[i].ParentName = -1;
gb_Open[i].CostPath = 0;
fscanf(gb_fInput, "%d", &(gb_Open[i].CostHeuristic));
gb_Close[i].Exist = false;
gb_Close[i].ParentName = -1;
gb_Close[i].CostPath = 0;
gb_Close[i].CostHeuristic = gb_Open[i].CostHeuristic;
}
gb_Open[gb_nStart].Exist = true;
In first code, it causes a error. If I put a breakpoint after read value from file into gb_Graph variable in 2 codes, there is no difference. But after that, put a breakpoint in gb_Open[gb_nStart].Exist = true; , in first code, values of gb_Graph are modified.
I think that is order of memory allocation. Right?
With variables:
VERTEX *gb_Open;
VERTEX *gb_Close;
int **gb_Graph;
Please explain me why it wrong? I use VS C++ 2012