-1

I have a problem where my malloc breaks my program. Removing it will make it work but I need it furter on. Can someone please explain what i'm doing wrong. Thanks in advance!!

I have this function in my graph.c

bool graph_initialise(graph_t *graph, unsigned vertex_count)
    {
      assert(graph != NULL);
        graph = (struct graph_s*) malloc(sizeof(struct graph_s));
        if (graph == NULL){return true;}
        graph->vertex_count = vertex_count;

        graph->adjacency_lists = (struct adjacency_list_s*) malloc(vertex_count * sizeof(struct adjacency_list_s));
        if (graph->adjacency_lists == NULL){
        return true;
        }

        int i;
        for (i = 1; i < vertex_count; ++i){
        graph->adjacency_lists[i].first = NULL;
        }
      return false;

and this in my graph.h

typedef struct edge_s
{
  /* Points to the next edge when this edge is part of a linked list. */
  struct edge_s *next;

  unsigned tail;    /* The tail of this edge. */
  unsigned head;    /* The head of this edge. */
  unsigned weight;  /* The weight of this edge. */
} edge_t;

typedef struct adjacency_list_s
{
  edge_t *first; /* Pointer to the first element of the adjacency list */
} adjacency_list_t;

/* Type representing a graph */
typedef struct graph_s
{
  unsigned vertex_count; /* Number of vertices in this graph. */
  unsigned edge_count;   /* Number of edges in this graph. */

  /* Pointer to the first element of an array of adjacency lists. The array
   * is indexed by vertex number
   */
  adjacency_list_t *adjacency_lists;
} graph_t;
  • 1
    What does _breaks the program_ mean? What is the message? – Kaiyakha Dec 18 '21 at 21:45
  • the assert is odd, why do you care if its null or not, the next line overwrites it, i would have thought it should be null if anything – pm100 Dec 18 '21 at 21:46
  • 1
    also you are going to leak here, you allocate memory but lose the pointer to it when you return. changing 'graph' in the function doesnt do anything once this function exits. I suspect you need **graph as a param – pm100 Dec 18 '21 at 21:47
  • Also, you do not need the structs to be named. Leave the structs anonymous and use the defined type instead, just like `adjacency_list_t` instead of `struct adjacency_list_s`. It's too much redundancy here. – Kaiyakha Dec 18 '21 at 21:48
  • Does this answer your question? [Changing address contained by pointer using function](https://stackoverflow.com/questions/13431108/changing-address-contained-by-pointer-using-function) – kaylum Dec 18 '21 at 21:55
  • In C all functions parameters are passed by value. So `graph` is a local variable in the function so setting it does not change the caller's value. See the duplicate post for the correct way to pass and set the pointer such that the caller will see the result. – kaylum Dec 18 '21 at 21:56

1 Answers1

0

I suspect this issue is that you are expecting this function to allocate grph for you then operating on the allocated graph from the calling code (you dont show the calling code)

ie you are doing something like

graph *gptr;
graph_initialise(gptr,42);
printf("vc = %d", gptr->vertex_count);

trouble is that grpah_initialize doesnt set gptr. You need instead

bool graph_initialise(graph_t **gptr, unsigned vertex_count)
    {
        *gptr = (struct graph_s*) malloc(sizeof(struct graph_s));
        graph_t *graph = *gptr;
        if (graph == NULL){return true;}
        graph->vertex_count = vertex_count;

        graph->adjacency_lists = (struct adjacency_list_s*) malloc(vertex_count * sizeof(struct adjacency_list_s));
        if (graph->adjacency_lists == NULL){
        return true;
        }

        int i;
        for (i = 1; i < vertex_count; ++i){
        graph->adjacency_lists[i].first = NULL;
        }
      return false;

and call it like this

graph *gptr;
graph_initialise(&gptr,42);
printf("vc = %d", gptr->vertex_count);

also that for loop should probably start at 0 not 1

pm100
  • 48,078
  • 23
  • 82
  • 145