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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:21:56+00:00 2026-05-26T15:21:56+00:00

If I have a snippit of my program like this: struct Node *node; while(…){

  • 0

If I have a snippit of my program like this:

struct Node *node;
while(...){
    node = malloc(100);
    //do stuff with node
}

This means that every time I loop through the while loop I newly allocate 100 bytes that is pointed to by the node pointer right?

If this is true, then how do I free up all the memory that I have made with all the loops if I only have a pointer left pointing to the last malloc that happened?

Thanks!

  • 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-26T15:21:57+00:00Added an answer on May 26, 2026 at 3:21 pm

    Please allocate exactly the size you need: malloc(sizeof *node); — if you move to a 64-bit platform that doubles the size of all your members, your old 96-byte structure might take 192 bytes in the new environment.

    If you don’t have any pointers to any of the struct Nodes you have created, then I don’t think you should be allocating them with malloc(3) in the first place. malloc(3) is best if your application requires the data to persist outside the calling scope of the current function. I expect that you could re-write your function like this:

    struct Node node;
    while(...){
        //do stuff with node
    }
    

    or

    while(...){
        struct Node node;
        //do stuff with node
    }
    

    depending if you want access to the last node (the first version) or not (the second version).

    Of course, if you actually need those structures outside this piece of code, then you need to store references to them somewhere. Add them to a global list keeping track of struct Node objects, or add each one to the next pointer of the previous struct Node, or add each one to a corresponding struct User that refers to them, whatever is best for your application.

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

Sidebar

Related Questions

In my views.py, i have a snippit of code like this: def clean_post_data(form): for
i have a snippet call like this: [!mysnippet?&content=`[*content*]` !] What happen is that, if
Ok this is the scenario... I have a table in Oracle that acts like
While testing a program for scalability, I came across the situation where I have
I have a program that uses (and must continue to use) an old sorting
I have a program with Driver GUI java file that creates a JFrame and
I have a Navigation-bar in my program that allows you to navigate the different
I have been reading a lot about this - I feel like I'm very
I have a small web.py Python application that I would like to serve under
I'm writing an audio program in Haskell using Portaudio. I have a function that

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.