Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8721381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:12:45+00:00 2026-06-13T07:12:45+00:00

I have created a graph datastructure using linked lists. With this code: typedef struct

  • 0

I have created a graph datastructure using linked lists. With this code:

typedef struct vertexNode *vertexPointer;
typedef struct edgeNode *edgePointer;

void freeGraph(vertexPointer); /* announce function */

struct edgeNode{
    vertexPointer connectsTo;
    edgePointer next;
};

struct vertexNode{
    int vertex;
    edgePointer next;
};

Then I create a graph in which I have 4 nodes, lets say A, B, C and D, where:
A connects to D via B and A connects to D via C. With linked lists I imagine it looks like this:

what the graph looks like

Finally, I try to free the graph with freeGraph(graph).

void freeEdge(edgePointer e){
    if (e != NULL) {
        freeEdge(e->next);
        freeGraph(e->connectsTo);
        free(e);
        e = NULL;
    }
}

void freeGraph(vertexPointer v){
    if (v != NULL) {
        freeEdge(v->next);
        free(v);
        v = NULL;
    }
}

That’s where valgrind starts complaining with “Invalid read of size 4”, “Address 0x41fb0d4 is 4 bytes inside a block of size 8 free’d” and “Invalid free()”. Also it says it did 8 mallocs and 9 frees.

I think the problem is that the memory for node D is already freed and then I’m trying to free it again. But I don’t see any way to do this right without having to alter the datastructure.

What is the best way to prevent these errors and free the graph correctly, if possible without having to alter the datastructure? Also if there are any other problems with this code, please tell. Thanks!

greets,
semicolon

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T07:12:46+00:00Added an answer on June 13, 2026 at 7:12 am

    The lack of knowing all the references makes this a bit difficult. A bit of a hack, but faced with the same issue I would likely use a pointer set (a list of unique values, in this case pointers).

    • Walk the entire graph, pushing nodes pointers into the set only if
      not already present (this the definition of ‘set’)
    • Walk the set, freeing each pointer (since they’re unique, no issue of
      a double-free)
    • Set graph to NULL.

    I’m sure there is an elegant recursive solution to this, but faced with the task as stated, this seems doable and not overtly complicated.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a graph using core plot. Graph is shown in on the
Actually, in my app, I have created a graph using achartEngine . In these
I have a cyclic graph I created using dosync and ref-set . When I
I have created web project in struts2.in that i have plotted a graph using
I have this graph created with gnuplot However the red line at the bottom
We have created an invite function at our site in JavaScript using the Graph
I have created a graph using ggplot2. Now I want to have the legend
I have created a horizontal bar graph using gRaphael charting library. I want to
I have created a graph using jpgraph - it works however due to the
I have created the event successfully using Graph Api as follows. Bundle params =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.