Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | "Morten W. Petersen" <morphex@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Linked lists, message passing |
| Date | 2015-06-19 09:08 +0200 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <mm0f4e$kbe$1@speranza.aioe.org> (permalink) |
| References | <mm03eu$tnt$1@speranza.aioe.org> <slrn3vfsmo7f16.kvv.ike@iceland.freeshell.org> |
On 19.06.2015 08:51, Ike Naar wrote:
> On 2015-06-19, Morten W. Petersen <morphex@gmail.com> wrote:
>> [...]
>> https://github.com/morphex/smash_xml/blob/c7a6017a75021432dbdb52178800e15dd4bc0061/message.c
>> [...]
>> Do you see any obvious problems with what in the code above?
>
> This part has a problem:
>
> struct message_linked_list *working_free = head;
> while (working_free) {
> printf("Free: %s\n", working_free->data);
> free(working_free);
> working_free = working_free->next;
> }
>
> The statement
>
> working_free = working_free->next;
>
> has undefined behaviour because the value of a pointer that refers to
> space deallocated by the free() function is used.
Ah yes, I spotted that as well then I applied a fix which
itself was broken.
Interestingly enough, the program ran fine with the originally
broken code.
> One possible workaround would be
>
> struct message_linked_list *working_free = head;
> while (working_free) {
> printf("Free: %s\n", working_free->data);
> struct message_linked_list *next = working_free->next;
> free(working_free);
> working_free = next;
> }
>
> And I would probably move this code to a separate function.
OK. Well now the code looks like this:
struct message_linked_list *working_free = head;
struct message_linked_list *working_free_old = NULL;
while (working_free) {
printf("Free: %s\n", working_free->data);
working_free_old = working_free;
working_free = working_free->next;
free(working_free_old);
}
Isn't it better to define the pointer before the loop? Or
is it general best to delay declaration? I assume that
something that's defined somewhere in the loop will also
be there on the next iteration.
I was thinking of moving the code to a function yeah..
-Morten
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Linked lists, message passing "Morten W. Petersen" <morphex@gmail.com> - 2015-06-19 05:49 +0200
Re: Linked lists, message passing Ike Naar <ike@iceland.freeshell.org> - 2015-06-19 06:51 +0000
Re: Linked lists, message passing "Morten W. Petersen" <morphex@gmail.com> - 2015-06-19 09:08 +0200
Re: Linked lists, message passing Barry Schwarz <schwarzb@dqel.com> - 2015-06-19 02:08 -0700
Re: Linked lists, message passing "Morten W. Petersen" <morphex@gmail.com> - 2015-06-19 14:11 +0200
Re: Linked lists, message passing raltbos@xs4all.nl (Richard Bos) - 2015-06-19 13:47 +0000
Re: Linked lists, message passing "Morten W. Petersen" <morphex@gmail.com> - 2015-06-19 15:52 +0200
Re: Linked lists, message passing Robert Wessel <robertwessel2@yahoo.com> - 2015-06-19 22:07 -0500
Re: Linked lists, message passing raltbos@xs4all.nl (Richard Bos) - 2015-06-27 12:25 +0000
Re: Linked lists, message passing "Morten W. Petersen" <morphex@gmail.com> - 2015-06-29 09:03 +0200
Re: Linked lists, message passing raltbos@xs4all.nl (Richard Bos) - 2015-07-03 17:28 +0000
Re: Linked lists, message passing "Morten W. Petersen" <morphex@gmail.com> - 2015-07-03 22:16 +0200
Re: Linked lists, message passing Morris Dovey <mrdovey@iedu.com> - 2015-07-03 16:44 -0500
Re: Linked lists, message passing "Morten W. Petersen" <morphex@gmail.com> - 2015-07-04 01:12 +0200
Re: Linked lists, message passing Morris Dovey <mrdovey@iedu.com> - 2015-07-03 18:50 -0500
Re: Linked lists, message passing "Morten W. Petersen" <morphex@gmail.com> - 2015-07-04 09:39 +0200
Re: Linked lists, message passing Morris Dovey <mrdovey@iedu.com> - 2015-07-04 04:34 -0500
Re: Linked lists, message passing Bartc <bc@freeuk.com> - 2015-07-04 11:01 +0100
Re: Linked lists, message passing Geoff <geoff@invalid.invalid> - 2015-07-04 11:43 -0700
Re: Linked lists, message passing "Morten W. Petersen" <morphex@gmail.com> - 2015-07-05 18:49 +0200
Re: Linked lists, message passing Geoff <geoff@invalid.invalid> - 2015-07-05 12:30 -0700
Re: Linked lists, message passing "Morten W. Petersen" <morphex@gmail.com> - 2015-07-05 22:39 +0200
Re: Linked lists, message passing Geoff <geoff@invalid.invalid> - 2015-07-05 15:08 -0700
Re: Linked lists, message passing glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-07-06 01:05 +0000
Re: Linked lists, message passing Geoff <geoff@invalid.invalid> - 2015-07-05 23:55 -0700
Re: Linked lists, message passing "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-07-04 11:48 -0700
Re: Linked lists, message passing Morris Dovey <mrdovey@iedu.com> - 2015-07-03 18:51 -0500
Re: Linked lists, message passing G G <gdotone@gmail.com> - 2015-07-03 18:13 -0700
Re: Linked lists, message passing Richard Heathfield <rjh@cpax.org.uk> - 2015-06-19 08:12 +0100
Re: Linked lists, message passing "Morten W. Petersen" <morphex@gmail.com> - 2015-06-19 09:30 +0200
Re: Linked lists, message passing Richard Heathfield <rjh@cpax.org.uk> - 2015-06-19 08:59 +0100
Re: Linked lists, message passing Noob <root@127.0.0.1> - 2015-06-19 10:14 +0200
Re: Linked lists, message passing Richard Heathfield <rjh@cpax.org.uk> - 2015-06-19 09:40 +0100
Re: Linked lists, message passing Ian Collins <ian-news@hotmail.com> - 2015-06-20 15:14 +1200
Re: Linked lists, message passing jacobnavia <jacob@jacob.remcomp.fr> - 2015-06-19 18:10 +0200
Re: Linked lists, message passing Richard Heathfield <rjh@cpax.org.uk> - 2015-06-19 17:32 +0100
Re: Linked lists, message passing Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-06-29 00:56 -0700
Re: Linked lists, message passing Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-06-29 00:49 -0700
Re: Linked lists, message passing Tim Rentsch <txr@alumni.caltech.edu> - 2015-07-11 08:32 -0700
csiph-web