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

2024-12-18 Thread Jeffrey Walton
On Wed, Dec 18, 2024 at 10:51 AM Franco Martelli wrote: > > On 17/12/24 at 22:09, Jeffrey Walton wrote: > > [...] > > 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

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

2024-12-18 Thread Franco Martelli
On 17/12/24 at 22:09, Jeffrey Walton wrote: On Tue, Dec 17, 2024 at 2:39 PM Franco Martelli 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 )

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

2024-12-18 Thread tomas
On Wed, Dec 18, 2024 at 10:45:43AM +, Kevin Chadwick wrote: > 18 Dec 2024 05:03:12 to...@tuxteam.de: > > > I'm all for concise code, but I usually revert some things in a second > > pass when they seem to hurt clarity. After all, you write your code for > > other people to read it. > > As you

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

2024-12-18 Thread Kevin Chadwick
18 Dec 2024 05:03:12 to...@tuxteam.de: > I'm all for concise code, but I usually revert some things in a second > pass when they seem to hurt clarity. After all, you write your code for > other people to read it. As you wrote the code then uness that second pass is weeks or months later then cla

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

2024-12-18 Thread Anssi Saari
Franco Martelli writes: > Peter A. Darnell, Philip E. Margolis - "C A Software Engineering Approach": > > https://www.google.it/books/edition/_/1nsS5q9aZOUC?hl=it&gbpv=0 > > Do you have it too? It's pretty old, with some typo, but it looks to > me good. Sorry, no, doesn't look familiar. I rememb

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

2024-12-17 Thread tomas
On Tue, Dec 17, 2024 at 04:18:17PM -0500, Greg Wooledge wrote: > On Tue, Dec 17, 2024 at 16:09:09 -0500, Jeffrey Walton wrote: > > I would rewrite the cleanup code like so: > > > > void dealloc() > > { > > DIGIT *next, *p = head; > > while( p ) > > next = p->nex

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

2024-12-17 Thread Jean-François Bachelet
Hello :) Le 17/12/2024 à 12:20, Anssi Saari a écrit : Franco Martelli writes: 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. Just curious, whi

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

2024-12-17 Thread Greg Wooledge
On Tue, Dec 17, 2024 at 16:09:09 -0500, Jeffrey Walton wrote: > I would rewrite the cleanup code like so: > > void dealloc() > { > DIGIT *next, *p = head; > while( p ) > next = p->next, free( p ), p = next; > } The logic looks good, but I'm not a fan of thi

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

2024-12-17 Thread Jeffrey Walton
On Tue, Dec 17, 2024 at 2:39 PM Franco Martelli 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 ); >

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

2024-12-17 Thread Franco Martelli
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

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

2024-12-17 Thread Franco Martelli
On 17/12/24 at 12:20, Anssi Saari wrote: Franco Martelli writes: 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. Just curious, which handbook is

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

2024-12-17 Thread Anssi Saari
Franco Martelli writes: > 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. Just curious, which handbook is it?

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

2024-12-16 Thread songbird
Franco Martelli wrote: ... > 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? comp

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

2024-12-16 Thread Franco Martelli
On 16/12/24 at 20:49, Jeffrey Walton wrote: On Mon, Dec 16, 2024 at 2:22 PM Franco Martelli 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 an

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

2024-12-16 Thread Franco Martelli
On 16/12/24 at 20:42, Michael Kjörling wrote: On 16 Dec 2024 17:21 +0100, from martelli...@gmail.com (Franco Martelli): Put in something to count the number of calls to malloc() and free() respectively. Don't forget calls outside of loops. There isn't calls to malloc() or free() outside loops.

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

2024-12-16 Thread Franco Martelli
On 16/12/24 at 17:50, Greg Wooledge wrote: On Mon, Dec 16, 2024 at 17:34:36 +0100, Franco Martelli wrote: void dealloc() { for ( const DIGIT *p = head; p->next != NULL; p = p->next ) if ( p->prev != NULL ) free( p->prev ); free( last ); }

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

2024-12-16 Thread Jeffrey Walton
On Mon, Dec 16, 2024 at 2:22 PM Franco Martelli 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

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

2024-12-16 Thread Michael Kjörling
On 16 Dec 2024 17:21 +0100, from martelli...@gmail.com (Franco Martelli): >> Put in something to count the number of calls to malloc() and free() >> respectively. Don't forget calls outside of loops. > > There isn't calls to malloc() or free() outside loops. What do you mean? >From a quick re-gla

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

2024-12-16 Thread Charles Curley
On Mon, 16 Dec 2024 16:05:26 +0100 Franco Martelli wrote: > I'm doing the exercises of a C language handbook. By all means do the exercises in your handbook as a learning experience. After that, I have found very useful Roger Sessions, Reusable Data Structures For C, Prentice Hall (1989). -- D

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

2024-12-16 Thread Greg Wooledge
On Mon, Dec 16, 2024 at 17:34:36 +0100, Franco Martelli wrote: > > > void dealloc() > > > { > > > for ( const DIGIT *p = head; p->next != NULL; p = p->next ) > > > if ( p->prev != NULL ) > > > free( p->prev ); > > > free( last ); > > > } > > > > I think you might ha

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

2024-12-16 Thread Franco Martelli
On 16/12/24 at 16:58, Greg Wooledge wrote: On Mon, Dec 16, 2024 at 16:05:26 +0100, Franco Martelli wrote: void add_element( unsigned int i ) { DIGIT *p; /* If the first element (the head) has not been * created, create it now. */ if ( head == NULL )

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

2024-12-16 Thread Franco Martelli
On 16/12/24 at 16:43, Michael Kjörling wrote: On 16 Dec 2024 16:05 +0100, from martelli...@gmail.com (Franco Martelli): 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.

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

2024-12-16 Thread Greg Wooledge
On Mon, Dec 16, 2024 at 16:05:26 +0100, Franco Martelli wrote: > void add_element( unsigned int i ) > { > DIGIT *p; > /* If the first element (the head) has not been > * created, create it now. > */ > if ( head == NULL ) > { > head = last = (DIGIT *

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

2024-12-16 Thread Michael Kjörling
On 16 Dec 2024 16:05 +0100, from martelli...@gmail.com (Franco Martelli): > 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. Put in something to count the number of call