Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.debian.user > #275851

Re: OT: Possible memory leak in an exercise of a C handbook

From Jeffrey Walton <noloader@gmail.com>
Newsgroups linux.debian.user
Subject Re: OT: Possible memory leak in an exercise of a C handbook
Date 2024-12-17 22:20 +0100
Message-ID <JUN90-iMd-11@gated-at.bofh.it> (permalink)
References <JUkTn-hz8W-9@gated-at.bofh.it> <JUpq2-1DO-11@gated-at.bofh.it> <JULJT-hN1-3@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


On Tue, Dec 17, 2024 at 2:39 PM Franco Martelli <martellif67@gmail.com> wrote:
>
> On 16/12/24 at 20:49, Jeffrey Walton wrote:
> > Here's the problem:
> >
> > void dealloc()
> > {
> >      for ( const DIGIT *p = first; p->next != NULL; p = p->next )
> >          if ( p->prev != NULL )
> >              free( p->prev );
> >      free( last );
> > }
> >
> > You seem to be checking backwards (p->prev) but walking the list
> > forwards (p = p->next) at the same time.
> >
> > Try something like this. It allows you to walk the list forward.
> >
> >      void dealloc()
> >      {
> >          for ( const DIGIT *next, *p = head; p->next != NULL; )
> >              if ( p != NULL )
> >                  next = p->next, free( p ), p = next;
> >          free( last );
> >      }
> >
> > The use of 'next' stashes away the pointer so you can free 'p' and
> > still access the next pointer.
>
> Thanks, your code works, Valgrind says 0 errors and: "All heap blocks
> were freed -- no leaks are possible".
> GCC gives me a warning, so I've to remove the "const" modifier in the
> "for loop":
>
> $ gcc -Wall e09-01.c
> e09-01.c: In function ‘dealloc’:
> e09-01.c:57:47: warning: passing argument 1 of ‘free’ discards ‘const’
> qualifier from pointer target type [-Wdiscarded-qualifiers]
>     57 |                         next = p->next, free( p ), p = next;
>        |                                               ^
> In file included from e09-01.c:5:
> /usr/include/stdlib.h:568:25: note: expected ‘void *’ but argument is of
> type ‘const DIGIT *’ {aka ‘const struct digit *’}
>    568 | extern void free (void *__ptr) __THROW;
>
> after done this, all works nicely. Thanks again.

There may be one logic error in the code -- if you insert one item,
then you may double free the node because you free 'p' and then you
free 'last'.

I would rewrite the cleanup code like so:

    void dealloc()
    {
        DIGIT *next, *p = head;
        while( p )
            next = p->next, free( p ), p = next;
    }

Then add three test cases instead of one. Have a test case for 0
items, 1 item, and N items (like 8).

Jeff

Back to linux.debian.user | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

OT: Possible memory leak in an exercise of a C handbook Franco Martelli <martellif67@gmail.com> - 2024-12-16 16:10 +0100
  Re: OT: Possible memory leak in an exercise of a C handbook Michael Kjörling <c9bc136c6063@ewoof.net> - 2024-12-16 16:50 +0100
    Re: OT: Possible memory leak in an exercise of a C handbook Franco Martelli <martellif67@gmail.com> - 2024-12-16 17:30 +0100
      Re: OT: Possible memory leak in an exercise of a C handbook Michael Kjörling <c9bc136c6063@ewoof.net> - 2024-12-16 20:50 +0100
        Re: OT: Possible memory leak in an exercise of a C handbook Franco Martelli <martellif67@gmail.com> - 2024-12-16 22:00 +0100
  Re: OT: Possible memory leak in an exercise of a C handbook Greg Wooledge <greg@wooledge.org> - 2024-12-16 17:00 +0100
    Re: OT: Possible memory leak in an exercise of a C handbook Franco Martelli <martellif67@gmail.com> - 2024-12-16 17:40 +0100
      Re: OT: Possible memory leak in an exercise of a C handbook Greg Wooledge <greg@wooledge.org> - 2024-12-16 18:00 +0100
        Re: OT: Possible memory leak in an exercise of a C handbook Franco Martelli <martellif67@gmail.com> - 2024-12-16 21:20 +0100
  Re: OT: Possible memory leak in an exercise of a C handbook Charles Curley <charlescurley@charlescurley.com> - 2024-12-16 19:40 +0100
  Re: OT: Possible memory leak in an exercise of a C handbook Jeffrey Walton <noloader@gmail.com> - 2024-12-16 21:00 +0100
    Re: OT: Possible memory leak in an exercise of a C handbook Franco Martelli <martellif67@gmail.com> - 2024-12-16 22:40 +0100
      Re: OT: Possible memory leak in an exercise of a C handbook songbird <songbird@anthive.com> - 2024-12-17 05:00 +0100
      Re: OT: Possible memory leak in an exercise of a C handbook Anssi Saari <anssi.saari@debian-user.mail.kapsi.fi> - 2024-12-17 12:30 +0100
        Re: OT: Possible memory leak in an exercise of a C handbook Franco Martelli <martellif67@gmail.com> - 2024-12-17 15:30 +0100
          Re: OT: Possible memory leak in an exercise of a C handbook Anssi Saari <anssi.saari@debian-user.mail.kapsi.fi> - 2024-12-18 11:20 +0100
        Re: OT: Possible memory leak in an exercise of a C handbook Jean-François Bachelet <jfbachelet@free.fr> - 2024-12-18 05:50 +0100
    Re: OT: Possible memory leak in an exercise of a C handbook Franco Martelli <martellif67@gmail.com> - 2024-12-17 20:50 +0100
      Re: OT: Possible memory leak in an exercise of a C handbook Greg Wooledge <greg@wooledge.org> - 2024-12-17 22:20 +0100
        Re: OT: Possible memory leak in an exercise of a C handbook <tomas@tuxteam.de> - 2024-12-18 06:10 +0100
          Re: OT: Possible memory leak in an exercise of a C handbook Kevin Chadwick <m8il1ists@gmail.com> - 2024-12-18 11:50 +0100
            Re: OT: Possible memory leak in an exercise of a C handbook <tomas@tuxteam.de> - 2024-12-18 12:00 +0100
      Re: OT: Possible memory leak in an exercise of a C handbook Jeffrey Walton <noloader@gmail.com> - 2024-12-17 22:20 +0100
        Re: OT: Possible memory leak in an exercise of a C handbook Franco Martelli <martellif67@gmail.com> - 2024-12-18 17:00 +0100
          Re: OT: Possible memory leak in an exercise of a C handbook Jeffrey Walton <noloader@gmail.com> - 2024-12-18 17:20 +0100

csiph-web