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. What do you mean?
From a quick re-glance through your code...
if ( head == NULL )
{
==> head = last = (DIGIT *) malloc( sizeof ( DIGIT ) );
head->dgt = last->dgt = i;
head->next = head->prev = last->next = last->prev = NULL;
return;
}
/* Otherwise, find the last element in the list */
for (p = head; p->next != NULL; p = p->next)
; /* null statement */
==> p->next = (DIGIT *) malloc( sizeof ( DIGIT ) );
Yes, but those calls in add_element(int) happen because add_element(int)
is called only by the "for loop" in main().
I realized what you suggest after reply to your email: place a
printf("+") after every malloc(…) call and place a printf("-") after
every free() call.
and
for ( const DIGIT *p = head; p->next != NULL; p = p->next )
if ( p->prev != NULL )
free( p->prev );
==> free( last );
certainly look to me like they're outside of loops.
Yes, that call is outside the loop, but I realized after reply to your
email of your suggestion.
Thank you very much for the tips!
--
Franco Martelli