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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:58:18+00:00 2026-06-13T02:58:18+00:00

#include <stdlib.h> struct strt; typedef struct { int i; struct strt *next; } strt;

  • 0
#include <stdlib.h>

struct strt;
typedef struct {
    int i;
    struct strt *next;
} strt;

strt *s1 = malloc(sizeof(strt));
strt *s2=s1->next = malloc(sizeof(strt));

free(s1);

Now, does free(s1) deallocate the block pointed to by s1 only or those pointed to by s1 and s1->next/s2?

I understand this must be a question asked a thousand times, but I
couldn’t manage to describe this the the search engine, neither could
I find direct mention of the same issue in documentation of the free() function.

  • 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-13T02:58:18+00:00Added an answer on June 13, 2026 at 2:58 am

    You should have a free per malloc. So you have to free s1 and s2 separately. Either:

    free(s1);
    free(s2);
    

    Or:

    free(s1->next);
    free(s1);
    

    In the second case, the statements’s order is important, because you can’t acceed to the objet referenced by s1 if s1 is freed.

    C11 (n1570), § 7.22.3.3 The free function
    The free function causes the space pointed to by ptr to be deallocated, that is, made available for further allocation.

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

Sidebar

Related Questions

people, i've an issue now.. #include <stdio.h> #include <stdlib.h> typedef struct a { int
#include<stdio.h> #include<stdlib.h> struct node { int data; struct node *next; }; void insert( struct
#include<iostream> #include<time.h> #include<list> #include<stdlib.h> #include<fstream> using namespace std; typedef struct diskBtNode { int parent;
#include <stdio.h> #include <stdlib.h> typedef struct { char *Name; int grade; int cost; }Hotel;
#include<stdio.h> #include<stdlib.h> struct node { char str[200]; int nn; struct node* next; }; int
#include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <stdint.h> #include <inttypes.h> typedef struct tmp_num{ int
This produces an incompatibility warning: #include <stdlib.h> #include <stdio.h> typedef struct { int key;
#include <stdio.h> #include <pthread.h> #include <time.h> #include <stdlib.h> typedef struct pr_struct{ int owner; int
#include <stdio.h> #include <stdlib.h> typedef int element; struct cell { element e; struct cell
#include <stdio.h> typedef int element_type; typedef struct Cell{ element_type e; struct Cell *next; }

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.