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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:12:06+00:00 2026-06-04T02:12:06+00:00

I am juggling with two ways of free() ‘ing malloc() ‘ed memory in a

  • 0

I am juggling with two ways of free()‘ing malloc()‘ed memory in a linked list structure. Suppose I create a singly linked list with the following C code;

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

struct node_type{
  int data;
  struct node_type *next;
  struct node_type *prev;
}
typedef struct node_type node; 
typedef struct node_type *list; 

void main(void){
  list head,node1,tail;
  head=(list)malloc(sizeof(node));
  tail=(list)malloc(sizeof(node));
  node1=(list)malloc(sizeof(node));
  head->next=node1;tail->prev=node1;
  node1->prev=head;node1->next=tail;node1->data=1;

  /*Method-1 for memory de-allocation*/
  free(head->next->next);
  free(head->next);
  free(head);

  /*OR*/

  /*Method-2 for memory de-allocation*/
  free(tail);
  free(node1);
  free(head);

  /*OR*/

  /*Method-3 for memory de-allocation*/
  free(node1);
  free(tail);
  free(head); 
}

Now, I have the following questions:

Q1) Which of the three methods of memory de-allocation shown in code above are correct/incorrect.

Q2) Is is necessary to follow any order in the free()‘ing memory as used in Methods 1 and 2 for memory de-allocation OR randomly free()‘ing memory is also fine?

  • 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-04T02:12:07+00:00Added an answer on June 4, 2026 at 2:12 am

    All the methods you showed are correct, you should follow a specific order only when the pointer to an allocated memory exists only in another allocated memory, and you will lose it if you free the container first.

    For example, for the allocation:

    int ** ipp;
    ipp = malloc(sizeof(int*));
    *ipp = malloc(sizeof(int));
    

    The correct free order will be:

    free(*ipp);
    free(ipp);
    

    and not:

    free(ipp);
    free(*ipp); // *ipp is already invalid
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a browser MMO using PHP and MySQL. I'm hesitating between two ways
The docs simply say or define custom schema (non-juggling), for example, mongoose. Please note,
I have a collection of PHP scripts that are extremely CPU intensive, juggling millions
I've been reading the PHP Docs on Type Juggling and Booleans but I still
This one's got me stumped. Usually with a little CSS juggling here and there
I have a PDF embedded in a web page using the following code: <object
In a class I'm working on, I am juggling several QList s. I have
I have just upgraded XCode 3.x to 4.0 and ran into following build error.
I am going crazy here. The following simple if-condition does not produce the right
I'm writing an application (A juggling pattern animator ) in PLT Scheme that accepts

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.