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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:26:14+00:00 2026-06-15T05:26:14+00:00

My question is regarding when using free() is appropriate in C. I’m using gcc

  • 0

My question is regarding when using free() is appropriate in C. I’m using gcc 4.3.2.

Suppose, if had to deallocate a bunch of memory, in a linked list, the ideal way to go about it is(I’m guessing):

int freeLL(node *a)
{
    if(a->next != NULL)
         freeLL(a->next);
    free(a);
    return 0;
}

Now, suppose I were to do a similar thing on the following ADT:

A pointer “VertexNode” which has 2 more pointers: “Vertex” and “Edge”(say). Equivalent to saying:

struct vertexnode
{
     vertex *v;
     edge *e;
}
typedef struct vertexnode* VertexNode;

Later, when initializing an instance, I’ll do something like –

VertexNode V = malloc(sizeof(struct vertexnode));
V->v = malloc(sizeof(vertex));

So, ultimately while freeing: I used the same analogy as I used for the linked list.

free(V->v);
free(V);

This gave a runtime error, and when I commented out “free(V->v)”, the program worked fine.
My questions are:

a) Is simply doing free(V) sufficient? I mean, does free() work recursively on all pointers inside a given pointer?

b) If not, is there a memory leak in this case? And how do I ideally prevent that?

c) Lastly, is there a way I can keep track of how many bytes were allocated by malloc() and
how many of them were freed by free()?

I’m very sorry for the long question. Thank you in advance for your time and patience.

  • 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-15T05:26:15+00:00Added an answer on June 15, 2026 at 5:26 am

    No, free does not work recursively, therefore you would indeed have memory leaks. The runtime error you are occuring is probably realated to a logic error (maybe V->v is NULL or you did not alloc it before freeing).

    If you are working with linux, using valgrind can help you profiling your program and mention leak errors. Compile using cc *.c -ggdb and then run valgrind --leakcheck=full ./a.out will output leak errors.

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

Sidebar

Related Questions

Yesterday I posted this question regarding using lambdas inside of a Join() method to
I have a question regarding using Semaphores HANDLE WINAPI CreateSemaphore(...); Is there anyway I
I have a question regarding the method i am using for doing Business Rule
I have a question regarding the best practise of handling formated text when using
I have a question regarding animations via javascript in HTML using DOM. In this
I have a question regarding the integration of business objects developed using Linq To
I have a question regarding use of FFT. Using function getBand(int i) with Minim
another newbie-question regarding rails and bootstrap. I am using something like this: <div class=tabbable
I have a simple question regarding jQuery selectors. I'm using the tablesorter and I
I am using smarty tags with php and my question / issue is regarding

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.