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

  • Home
  • SEARCH
  • 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 6964191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:53:36+00:00 2026-05-27T15:53:36+00:00

I am pretty new to C, and am doing toy programs to learn it.

  • 0

I am pretty new to C, and am doing toy programs to learn it.
The following code compiles, outputs correctly, but Valgrind reports a memory leak:

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "graph.h"

void add_vertex(vertex *v_p, char *name) {
    if(strlen(name) == 0) { 
        printf("error");
    }
    v_p->name = (char *)malloc(strlen(name) + 1);
    if(v_p->name == NULL) {
        printf("error");
    }
    strcpy(v_p->name, name);
    v_p->cluster = -1;
    v_p->deleted = 0;
    printf("added vertex.\n");
}

void free_vertex(vertex *ver) {
    if(ver->name) {free(ver->name);printf("free'd name\n");}
    free(ver);

}

int main() {
    int Nu = 0;
    vertex *arr = (vertex *)malloc(2 * sizeof(vertex));
    vertex *_temp;
    int i =0;
    add_vertex(arr, "Hello");
    add_vertex(arr+1, "World");
    _temp = (vertex *)realloc(arr, 4*sizeof(vertex));
    printf("reallocated\n");
    if (_temp != NULL) {
        arr = _temp;
        add_vertex(arr+2, "this");
        add_vertex(arr +3, "worked");
        Nu=4;
    }
    else{
        printf("FAIL\n");
        Nu=2;
    }
    for (; i <Nu; i++) {
        printf("%s\n",(arr+i)->name);
    }
    for (; i <Nu; i++) {
       free_vertex(arr+i);
    }
    free(arr);
    return 0;
}

The vertex is coded in a header file,

typedef struct vertex_t
{
    char*   name;
    int     cluster;
    int    deleted;
}vertex

The output is:

added vertex.
added vertex.
reallocated
added vertex.
added vertex.
Hello
World
this
worked

It doesn’t print “free’d memory”, so where was it free’d?
here is what Valgrind had to say about it:

==1436== HEAP SUMMARY:
==1436==     in use at exit: 24 bytes in 4 blocks
==1436==   total heap usage: 6 allocs, 2 frees, 120 bytes allocated
==1436== 
==1436== 5 bytes in 1 blocks are definitely lost in loss record 1 of 4
==1436==    at 0x4C2893D: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==1436==    by 0x4006C3: add_vertex (graph.c:10)
==1436==    by 0x4007EF: main (graph.c:37)
==1436== 
==1436== 6 bytes in 1 blocks are definitely lost in loss record 2 of 4
==1436==    at 0x4C2893D: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==1436==    by 0x4006C3: add_vertex (graph.c:10)
==1436==    by 0x400797: main (graph.c:31)
==1436== 
==1436== 6 bytes in 1 blocks are definitely lost in loss record 3 of 4
==1436==    at 0x4C2893D: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==1436==    by 0x4006C3: add_vertex (graph.c:10)
==1436==    by 0x4007AC: main (graph.c:32)
==1436== 
==1436== 7 bytes in 1 blocks are definitely lost in loss record 4 of 4
==1436==    at 0x4C2893D: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==1436==    by 0x4006C3: add_vertex (graph.c:10)
==1436==    by 0x400804: main (graph.c:38)
==1436== 
==1436== LEAK SUMMARY:
==1436==    definitely lost: 24 bytes in 4 blocks
==1436==    indirectly lost: 0 bytes in 0 blocks
==1436==      possibly lost: 0 bytes in 0 blocks
==1436==    still reachable: 0 bytes in 0 blocks
==1436==         suppressed: 0 bytes in 0 blocks

What is the problem with the code? Should I allocate the name of the vertex differently?
Thanks for the help!

EDIT: Thanks to all of you! I’ve fixed the code, now I am not freeing individual elements, i.e.

void free_vertex(vertex *ver) {
    if(ver->name) {free(ver->name);printf("free'd name\n");}
}

and of course re-setting the i to 0. can’t believe I overlooked it. Thanks a lot!

  • 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-05-27T15:53:37+00:00Added an answer on May 27, 2026 at 3:53 pm

    You are not resetting i in the loop that frees the code. Those loops should look like this:

    for (i = 0; i <Nu; i++) {
        printf("%s\n",(arr+i)->name);
    }
    for (i = 0; i <Nu; i++) {
       free_vertex(arr+i);
    }
    

    Added another point:

    free_vertex() should not have the:

    free(ver)
    

    line, which is what others are saying.

    free_vertex() and add_vertex() should be parallel in the sense that free frees only what was allocated in add.

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

Sidebar

Related Questions

I'm pretty new to iPhone application development and I'm doing well so far. But
I'm pretty new to C#, I've been doing a bunch of stuff but I'm
I am pretty new to Backbone.js, but I am pretty sure I am doing
What I'm doing: I'm pretty new to doing data migrations with Core Data, but
I'm pretty new still at C# so I might be doing something stupid, but
I'm pretty new to MSBuild, so I might be doing something obviously-wrong, but a
Im pretty new to Java Web Services, but I cant find a good explanation
I am pretty new to php, but I am learning! I have a simple
I'm pretty new doing my own CSS changes and I am a little stuck
Pretty new here. But before I ask my question, I'm not looking for teh

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.