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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:28:13+00:00 2026-06-15T17:28:13+00:00

Possible Duplicate: What is the best way to check for memory leaks in c++?

  • 0

Possible Duplicate:
What is the best way to check for memory leaks in c++?

I’m writing a doubly linked list in C and most of it is implemented and working(I only need to fix some minor logic errors in traversing and possibly freeing).

Question:
How can I be absolutely sure that I’m freeing all the memory that I allocate? I would also like to know if there are any techniques to optimizing my allocation. Any tips or hints on how it works or links to tutorials are appreciated as well.

I’m pretty much a beginner, so any other tips in fixing my coding techniques would be appreciated. I use gdb to debug and I’m running on Archbang Linux x86_64.

Thank you for your help.

Here are the structures of the doubly linked list:

typedef struct node_element{
    double data;
} element;

typedef struct node_t{
    struct node_t *prev;
    struct node_t *next; 
    struct node_element element;
} node;

typedef struct list_t{
    struct node_t *head;
    struct node_t *tail;
} list;

This is how I create a list:

list *createList(){
    list *temp = malloc(sizeof(list));

    temp->head = malloc(sizeof(node));
    temp->tail = malloc(sizeof(node));

    temp->head->prev = NULL;
    temp->head->next = temp->tail;
    temp->tail->prev = temp->head;
    temp->tail->next = NULL;

    return temp;
}

New nodes:

node *newNode(element * element){
    node *current = malloc(sizeof(node));
    current->element.data = element->data;

    return current;
}

Removing individual nodes, not very relevant to my question, but may be useful:

node *removeNode(list * list, node * current){
    if (current->prev == NULL)
        list->head = current->next;
    else
        current->prev->next = current->next;

    if (current->next == NULL)
        list->tail = current->prev;
    else
        current->next->prev = current->prev;

    free(current);

    return NULL;
}

And now the important part, when I’m done with a list I call this function:

list *removeList(list * list){
    node *temp; //Revised.
    //node *temp = malloc(sizeof(node));

    while (list->head != NULL){
        temp = list->head->next;
        free(list->head);
        list->head = temp;
    }

    return NULL;
}

like so:

a_list = removeList(a_list);
  • 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-15T17:28:13+00:00Added an answer on June 15, 2026 at 5:28 pm

    Amongst the multitude of features it provides, Valgrind will allow you to check for memory leaks. It does this by dynamically instrumenting the memory management functions.

    You might use a command like this:

    valgrind --tool=memcheck --leak-check=yes my_prog
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: What is the best way to check for memory leaks in c++?
Possible Duplicate: Python: What is the best way to check if a list is
Possible Duplicate: Best way to detect integer overflow in C/C++ how do we check
Possible Duplicate: Best way to detect when user leaves a web page Check if
Possible Duplicate: Best way to check if a Data Table has a null value
Possible Duplicate: More concise way to check to see if an array contains only
Possible Duplicate: Best way to check if a DLL file is a CLR assembly
Possible Duplicate: Best way to find Browser type & version? In HTML/JavaScript, How can
Possible Duplicate: best way to get the key of a key/value javascript object foo
Possible Duplicate: Best way to stop SQL Injection in PHP I am creating a

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.