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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:44:29+00:00 2026-05-27T17:44:29+00:00

I have a struct as follows: struct Node{ int *arr; int *sol; struct Node

  • 0

I have a struct as follows:

struct Node{
int *arr;
int *sol;
struct Node *Next;
};

i am creating Node in this way:

Node* MyNode = (Node *)malloc(sizeof (struct Node));
MyNode->arr = malloc(sizeof(int)*N);
MyNode->sol=  malloc(sizeof(int)*N);

I then add MyNode to a linked list. How can i free memory for an element in the list.
is this correct:

pop(){
   free(first->arr);
   free(first->sol);
   first=first->Next; 
}
  • 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-27T17:44:30+00:00Added an answer on May 27, 2026 at 5:44 pm

    For any struct to be a node in a linked-list, you need a self-referential structure variable which should be declared as struct Node *next;

    struct Node{
        int *arr;
        int *sol;
        struct Node *next;
    }
    

    To allocate memory for a node of a linked-list, you need the following:

    /* allocate memory for a node */
    struct Node * MyNode = (struct Node *)malloc((int)sizeof(struct Node));
    if (MyNode == NULL) {
        printf("ERROR: unable to allocate memory \n");
        return 1;
    }
    
    /* allocate memory for the content of a node */
    MyNode->arr = (int *) malloc((int)sizeof(int) * N);
    if (MyNode->arr == NULL) {
        printf("ERROR: unable to allocate memory \n");
        return 1;
    }
    
    MyNode->sol = (int *) malloc((int)sizeof(int) * N);
    if (MyNode->sol == NULL) {
        printf("ERROR: unable to allocate memory \n");
        return 1;
    }
    
    /* add the new node to a list by updating the next variable */
    MyNode->next = ... 
    

    If you are not sure about the operations that you need to perform to delete node in a linked-list, you can use a temp variable to do the same in an easier way.

    pop()
    {
        struct Node * temp = first;
        first = first->next;
        free(temp->arr);
        free(temp->sol);
        free(temp);
    }
    

    Thumb rule for free – for every malloc() there should be a free()

    OTOH, to go through various scenarios in deleting a node in an linked-list, please refer this link.

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

Sidebar

Related Questions

Suppose I have a struct called Node as follows: struct foo { foo *next;
I have this struct: struct Map { public int Size; public Map ( int
I have a struct defined as follows: template <typename T> struct data { int
I have structure as follows: struct data {int no; string name; int id}; I
I have a Struct as follows, struct Location { public int Row; public int
I have a class as follows struct CliHandler { CliHandler(int argc, char** argv); ~CliHandler();
I have a set of structs, defined as follows: typedef struct { int index;
I have a testing struct definition as follows: struct test{ int a, b, c;
I have a struct as follows, with a pointer to a function called length
I have a struct defined like follows as part of an object. I'm trying

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.