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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:47:14+00:00 2026-05-27T22:47:14+00:00

I have created an array of pointers. I have used some of them as

  • 0

I have created an array of pointers. I have used some of them as roots for singly linked lists.
The size of lgroup(the array of pointers) is 10 and j can get up to 10 as well. I malloc every lptr like this:

lgroup[i].lptr[j] = (node *) malloc (sizeof(node));

and if I’m not going to use it I set curr = lgroup[i].jptr[j]; and curr->next = NULL;.
Otherwise I just start using malloc on every curr->next and then I do curr = curr->next;. When I don’t want to add any more nodes to a list I just put curr->next = NULL; and move on to the next lptr. Pretty standard stuff.

At the end of my program I want to free all of the memory that I have claimed for my lists. This is how I try to do it:

for (i = 0; i < 10; i++) {
  for (j = 0; j < 10; j++) {
    if (lgroup[i].lptr[j] == NULL) {
      free(lgroup[i].lptr[j]);
      continue;
    } else {
      curr = lgroup[i].lptr[j];
      while(curr->next != NULL) {
        curr2 = curr->next;
        free(curr);
        curr = curr2;
      }
    }
  }
}

The thing is that I ended up with this code after a lot of trial and error and a lot of messages of “double freeing” and other stuff like that, so I’m not completely sure if it actually frees all the memory that I have claimed or that it just happens to compile and run without error but not doing all the things I want it to. I tried to run it on gdb but I really can’t understand a lot of things by looking at memory addresses so I was curious if there is a way to check if it works as I expected or if I’m doomed to do it using pen and paper and running the code in my head. If it happens to actually do what it was designed for, would there be an easier and cleaner way to achieve the same results?

Thank you in advance and if you need any clarification don’t hesitate to ask for it in the comments.

  • 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-27T22:47:15+00:00Added an answer on May 27, 2026 at 10:47 pm

    The first call to free in the code you have posted does not have any effect, since the argument is a NULL pointer (using continue in that context is also unnecessary).

    It also seems you are not calling free for lgroup[i].lptr[j] when it is not NULL and lgroup[i].lptr[j]->next is NULL.

    I would rewrite it to:

    for (i = 0; i < 10; i++) {
        for (j = 0; j < 10; j++) {
            curr = lgroup[i].lptr[j];
            while (curr != NULL) {
                curr2 = curr->next;
                free(curr);
                curr = curr2;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

for example: I have created one javascript property array model with some properties like
i have created Array of Linkbutton and when user click on link button it
I have created an array square(40) as picturebox. I have to assign 40 picture
I have created an array Man: public main blah blah{ man = man[10]; }
I have created an array in php that prints this Array ( [mark] =>
I am new to Knockout.js, I have created an observable array and initialized with
I have an array created with this code: var widthRange = new Array(); widthRange[46]
I have created an empty json object having an array itemlist(which further contains itemid
I have created 4 buttons via Interface Builder. I have an array which has
If I have an array created like this: MyType *array = new MyType[10]; And

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.