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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:16:56+00:00 2026-06-17T09:16:56+00:00

I’m fairly new to Pointers and the memory model so excuse me if this

  • 0

I’m fairly new to Pointers and the memory model so excuse me if this is obvious, but I am writing a program to test a function reverse that reverses a list. Anyway I have it in three files, C5.c, C5-driver.c, and C5.h. Here they are in that order:

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

struct node *cons(int fst, struct node *rst) {
    struct node *new = malloc(sizeof(struct node));
    if (new == NULL) {
        printf("cons: out of memory\n");
        abort();
    }
    (*new).first = fst; /* same as (*new).first = fst */
    (*new).rest = rst;
    return new;
}

struct node *reverse(struct node *lst) {
    struct node *ans = NULL;
    while (lst != NULL) {
        ans = cons((*lst).first, ans);
        lst = (*lst).rest;
    }    
    return ans;
}

void free_list(struct node *lst) {
    struct node *p;
    while (lst != NULL) {
        p = lst->rest;
        free(lst);
        lst = p;
    }
}

void print_list(struct node *lst) {
    printf("( "); 
    while (lst != NULL) {
        printf("%d ", (*lst).first);
        lst = (*lst).rest;
    }
    printf(")\n");
}

C5-driver.c

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

int main() {
    struct node *lst1 = cons(5, NULL);
    struct node *lst2 = cons(3, lst1);
    struct node *lst3 = cons(1, lst2);
    print_list(lst3);
    lst3 = reverse(lst3);
    print_list(lst3);
    free_list(lst3);
}

C5.h

struct node {
int first;
struct node *rest;
};

struct node *cons(int ,struct node *);
struct node *reverse(struct node *);
void print_list(struct node *);
void free_list(struct node *);

However I’m told by XCode that there are memory leaks.

I’m assuming it’s after cons is used however I’ve tried creating a new struct node *ans = new and free(new); with return ans; but that doesn’t work. I’ve also tried free_list as you can see above.

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-06-17T09:16:57+00:00Added an answer on June 17, 2026 at 9:16 am

    The reverse function calls cons which allocates memory, then it overwrites the lst3 pointer. The memory leak is that lst3 is overwritten which makes it impossible to recover that memory.

    You should probably make a new variable like struct node *lst3_reverse and lst3_reverse = reverse(lst3). Then you can safely do free_list(lst3) and free_list(lst3_reverse) to free the memory.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I want to construct a data frame in an Rcpp function, but when I
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

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.