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 8380425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:27:20+00:00 2026-06-09T16:27:20+00:00

I have these two files: test.h #ifndef TEST_H #define TEST_H typedef struct _vertex_node {

  • 0

I have these two files:

test.h

#ifndef TEST_H
#define TEST_H

typedef struct _vertex_node
{
  double x;
  double y;
  struct _vertex_node *next;
} vertex_node;

static void add_vertex(vertex_node **node, const double x, const double y);
static void dty_vertex(vertex_node **node);

#endif // TEST_H

test.c

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

#include "test.h"


static void add_vertex(vertex_node **node, const double x, const double y)
{
  if(NULL == (*node))
  {
    (*node) = malloc(sizeof(vertex_node));
    (*node)->x = x;
    (*node)->y = y;
    (*node)->next = NULL;
  }
  else
    add_vertex(&((*node)->next), x, y);
}

static void dty_vertex(vertex_node **node)
{
  if(NULL != (*node)->next)
    dty_vertex(&((*node)->next));

  free(*node);
}

int main(int argc, char **argv)
{
  vertex_node *node;
  vertex_node *iterator;

  add_vertex(&node, 0, 0);
  add_vertex(&node, 1, 0);
  add_vertex(&node, 1, 1);
  add_vertex(&node, 0, 1);

  iterator = node;

  while(NULL != iterator)
  {
    printf("x: %f, y: %f\n", iterator->x, iterator->y);

    iterator = iterator->next;
  }

  dty_vertex(&node);

  return 0;
}

I am using the gcc -Wall -ggdb test.c -o test command to compile it.

When I try to run it, it gives me a segmentation fault when freeing memory, what is wrong here?

  • 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-09T16:27:21+00:00Added an answer on June 9, 2026 at 4:27 pm

    You need to initialize the node:

    vertex_node *node = NULL;
    

    If you do not initialize like this, the check:

    if(NULL == (*node))
      { /* */ }
    

    will fail for the first call and this part is executed:

    add_vertex(&((*node)->next), x, y);
    

    with an arbitrary value in node. You can get seg-fault at this time, or when you try to free the node (if you are unlucky).

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

Sidebar

Related Questions

I have these two simple files that define a C++ class with a tryME
I have two excel files. Among many columns in both these files, they have
I have some PDF's all with two attached files with static names. I would
I have two html files (for add and edit) which contains js codes. These
I have two files which both contain a list of words. Is there an
Say I have two files where there is one number per line File 1
Suppose i have one folder and inside that another two files are there .
I have these two lines in my web.xml <url-pattern>/</url-pattern> : Index Servlet and <url-pattern>/login</url-pattern>
I have these two tables people ============ id, name and answer_sheets ============ id, person_id,
I have these two modules : package G1; sub new { my $class =

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.