Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > linux.debian.user > #275799
| From | Franco Martelli <martellif67@gmail.com> |
|---|---|
| Newsgroups | linux.debian.user |
| Subject | Re: OT: Possible memory leak in an exercise of a C handbook |
| Date | 2024-12-16 22:40 +0100 |
| Message-ID | <JUqYN-48s-13@gated-at.bofh.it> (permalink) |
| References | <JUkTn-hz8W-9@gated-at.bofh.it> <JUpq2-1DO-11@gated-at.bofh.it> |
| Organization | linux.* mail to news gateway |
On 16/12/24 at 20:49, Jeffrey Walton wrote:
> On Mon, Dec 16, 2024 at 2:22 PM Franco Martelli <martellif67@gmail.com> wrote:
>>
>> I'm doing the exercises of a C language handbook. I'm using Valgrind to
>> check for memory leak since I use the malloc calls. In the past I was
>> used to using "valkyrie" but sadly isn't available anymore for Bookworm
>> (does anybody know for a replacement?).
>>
>> I suppose that Valgrind detects a memory leak for my source code, here
>> its output:
>>
>> $ valgrind --track-origins=yes --leak-check=full -s ./a.out
>> ==101348== Memcheck, a memory error detector
>> ==101348== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al.
>> ==101348== Using Valgrind-3.19.0 and LibVEX; rerun with -h for copyright
>> info
>> ==101348== Command: ./a.out
>> ==101348==
>> 12345678
>> ==101348==
>> ==101348== HEAP SUMMARY:
>> ==101348== in use at exit: 24 bytes in 1 blocks
>> ==101348== total heap usage: 9 allocs, 8 frees, 1,216 bytes allocated
>> ==101348==
>> ==101348== 24 bytes in 1 blocks are definitely lost in loss record 1 of 1
>> ==101348== at 0x48407B4: malloc (in
>> /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
>> ==101348== by 0x10924B: add_element (in a.out)
>> ==101348== by 0x1093B3: main (in a.out)
>> ==101348==
>> ==101348== LEAK SUMMARY:
>> ==101348== definitely lost: 24 bytes in 1 blocks
>> ==101348== indirectly lost: 0 bytes in 0 blocks
>> ==101348== possibly lost: 0 bytes in 0 blocks
>> ==101348== still reachable: 0 bytes in 0 blocks
>> ==101348== suppressed: 0 bytes in 0 blocks
>> ==101348==
>> ==101348== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
>>
>> Is there a memory leak? What it sounds strange to me is that Valgrind
>> reports: "total heap usage: 9 allocs, 8 frees, …" when for me the calls
>> to "malloc" should be 8, not 9.
>
> Yes.
>
>> Is there any C guru here that can take a look to my source code in
>> attachment?
>
> 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.
>
> If you don't want to use the comma operator, then this:
>
> if ( p != NULL ) {
> next = p->next;
> free( p );
> p = next;
> }
>
Thank you very much, I'll try that code tomorrow
>> In order to avoid off topic messages in the future does
>> anybody know where to ask for these topics?
>
> Stack Overflow is usually a good place for programming questions.
I'd prefer a mailing-list instead, once finished all the exercises, I'd
like to looking for somebody that he has my same handbook and to ask him
for exchange the exercises for comparison purpose.
Does anybody know a mailing-list for C language questions?
--
Franco Martelli
Back to linux.debian.user | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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