簡體   English   中英

使用指向結構的雙指針和結構指針重新分配動態二維數組

[英]Reallocation of dynamic 2 dimensional array with double Pointers to structs and struct Pointer

我問了一個關於結構中動態指針的分配和重新分配的問題。 但是現在我在一個結構體中有一個雙指針組件。

我想在添加頂點后重新分配圖的鄰接矩陣。

雙指針的重新分配有效,但是當我想在循環中重新分配指針(雙指針指向的指針)時,程序在第一個循環運行中停止並出現分段錯誤...

這是我的想法(用油漆繪制...) myIdea

為了更好的可讀性,我划分了我的代碼

這是代碼第 1 部分:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

struct Vertices
{
    int id;
    char name[15];
    float xPos;
    float yPos;
};

struct Edge
{
    int id;
    struct Vertices *start;
    struct Vertices *end;
};


struct Graph
{
    int VertexCounter;
    struct Vertices *vertex;
    struct Edge **adjMat;
};

第 2 部分與 updateAdjMat() 中的重新分配問題:

//Initializing graph
void initGraph(struct Graph **graph)
{
    *graph = calloc (1, sizeof(struct Graph **));
    if (!*graph) {
        perror ("calloc-*graph");
        exit (EXIT_FAILURE);
    }

    (*graph)->vertex = NULL;
    (*graph)->adjMat = NULL;
    /*
    (*graph)->adjMat = calloc (1, sizeof (struct Edge **));
    if (!(*graph)->adjMat) {
        perror ("calloc-(*graph)->adjMat");
        exit (EXIT_FAILURE);
    }
    */

    (*graph)->VertexCounter = 0;
}


void updateAdjMat (struct Graph *graph)
{
    int i;

    void *tmp = realloc (graph->adjMat, graph->VertexCounter * sizeof (struct Edge *));
    if (!tmp) 
    {
        perror ("realloc-graph->adjMat");
        exit (EXIT_FAILURE);
    }

    graph->adjMat = tmp;

    for (i = 0; i < graph->VertexCounter; i++)
    {
        void *tmp = realloc (*(graph->adjMat + i), (graph->VertexCounter) * sizeof (struct Edge));
        if(!tmp)
        {
            perror ("realloc-*(graph->adjMat + i)");
            exit (EXIT_FAILURE);
        }
        *(graph->adjMat + i) = tmp;
    }

}

第 3 部分與工作代碼:

//reallocating the memory for the vertex pointer
void addVertex (struct Graph *graph, char name[15], float x, float y)
{
    void *tmp = realloc (graph->vertex, (graph->VertexCounter + 1) * sizeof(*graph->vertex));
    if (!tmp) {
        perror ("realloc-(*graph)->vertex");
        exit (EXIT_FAILURE);
    }
    graph->vertex = tmp;

    (graph->vertex + graph->VertexCounter)->id = graph->VertexCounter + 1;
    strcpy((graph->vertex + graph->VertexCounter)->name, name);
    (graph->vertex + graph->VertexCounter)->xPos = x;
    (graph->vertex + graph->VertexCounter)->yPos = y;

    graph->VertexCounter++;

    updateAdjMat(graph);
}

void menu_addVertex (struct Graph *graph)
{
    char name[15];
    float xPos, yPos;

    printf ("\nWhats the name of the vertex?\n");
    scanf ("%s", &name);
    printf ("\nX Coordinate?\n");
    scanf ("%f", &xPos);
    printf ("\nY Coordinate?\n");
    scanf ("%f", &yPos);
    printf ("\n");

    addVertex (graph, name, xPos, yPos);
}

//free the allocated memory
void freeGraph (struct Graph *graph)
{
    free (*(graph->adjMat));
    free (graph->adjMat);
    free (graph->vertex);
    free (graph);
}

在我的 Main 中,我只有一個菜單,可以通過調用 addVertex 添加帶有名稱和 x、y 坐標的新頂點

您的圖形初始化錯誤。

首先,您傳入一個 Graph ** 以便您可以為其分配一個 Graph * 值。 但是在里面你被分配了一個 Graph **,而不是一個 Graph *。 碰巧的是,指針的大小通常相同,所以結果是可以的,但是代碼是錯誤的。

*graph = calloc (1, sizeof(struct Graph **));

您實際上想要分配一個 Graph *。

即便如此,之后,您還沒有分配 Graph。 你所分配的只是一個指針!

所以這里你有遵循兩個指針的代碼,首先是 *graph ,然后是 ->

(*graph)->VertexCounter = 0;

這通常會讓您訪問 Graph,但您尚未分配 Graph,您只分配了一個指向圖的指針。 所以這很可能在這一點上崩潰。 接下來的任何事情都可能會崩潰。

我找到了解決方案。 問題是,我想在 for 循環中重新分配的內存沒有初始化。

有了這個 for 循環,它開始工作:

    for (i = 0; i < graph->VertexCounter; i++)
    {
        //if the new column wasn't initialized
        if (i + 1 == graph->VertexCounter) {
            *(graph->adjMat + i) = calloc(graph->VertexCounter, sizeof **graph->adjMat);
        }
        else {
            void *tmp = realloc (*(graph->adjMat + i), graph->VertexCounter * sizeof (**graph->adjMat));
            if(!tmp)
            {
                perror ("realloc-*(graph->adjMat + i)");
                exit (EXIT_FAILURE);
            }
            *(graph->adjMat + i) = tmp;
        }

        for (j = 0; j < graph->VertexCounter; j++)
        {
            if (i + 1 == graph->VertexCounter || j + 1 == graph->VertexCounter)
            {
                //Doing some initialization to the new edges
            }

        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM