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


Groups > comp.lang.c > #60026

Re: Malloc without free in linked list code

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.c
Subject Re: Malloc without free in linked list code
Date 2015-04-14 13:43 +0100
Organization A noiseless patient Spider
Message-ID <87r3rmkhh6.fsf@bsb.me.uk> (permalink)
References <2517ea7a-e00d-44a4-bb3b-9376fc5e3249@googlegroups.com> <877ftf21a8.fsf@bsb.me.uk> <mgifcq$s1j$1@dont-email.me> <75576f60-87ab-4714-b12a-424796ae6e6e@googlegroups.com> <mgin7m$lqk$1@dont-email.me>

Show all headers | View raw


David Brown <david.brown@hesbynett.no> writes:

> On 14/04/15 10:45, Paul wrote:
>> On Tuesday, April 14, 2015 at 8:26:22 AM UTC+1, David Brown wrote:
<snip>
>>> struct node *CopyList(struct node *head)
>>> {
>>> 	struct node *current = head;
>>> 	struct node dummyTail = { .next = newList };
>>> 	struct node *tail = &dummyTail;
>>>
>>> 	while (current != NULL) {
>>> 		tail->next = malloc(sizeof(struct node));
>>> 		(tail->next)->data = current->data;
>>> 		(tail->next)->next = NULL;
>>> 		tail = tail->next;
>>> 	}
>>> 	return dummyTail.next;
>>> }
>>>
>>> That keeps the loop as short as your version, but eliminates the
>>> unpleasant double pointer.

Personally, I don't see anything unpleasant about a double pointer but
you haven't really got rid of it, you've just declared it in a funny way
(that I think might be just as confusing as **) and accessed it using
two ->s instead of a * and a ->.

It's true that in my version, the precedence of * and -> interact "the
wrong way" but that's hardly anything to worry about -- after all,
you've added redundant parentheses to your version "just in case".

>>> However, I agree that the recursive version is the neatest choice - the
>>> source code is clear and easily understood, and the compiler can handle
>>> the details of producing a loop.

Sadly, I don't think existing C compilers turn this recursive pattern
into a loop.

>> Thanks for all the comments.  I would have expected to see a current
>> = current->next; It's not clear to me that these versions which omit
>> current = current->next work. (I'm not saying that they don't work
>> though).
>
> You are correct - there is a "current = current->next" missing from the
> loop.  As I said, I've only written the code - not tested it!  (Ben's
> code is missing it too.)

No it isn't missing!  It's there in the version that has the whole loop
body, but it's obviously not there in the part where I discuss
re-writing the "if" because it was never part of the "if" to start with.

-- 
Ben.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Malloc without free in linked list code Paul <pepstein5@gmail.com> - 2015-04-13 11:21 -0700
  Re: Malloc without free in linked list code Lőrinczy Zsigmond <nospam@for.me> - 2015-04-13 20:45 +0200
  Re: Malloc without free in linked list code Richard Heathfield <rjh@cpax.org.uk> - 2015-04-13 19:53 +0100
  Malloc without free in linked list code asetofsymbols@gmail.com - 2015-04-13 12:05 -0700
  Re: Malloc without free in linked list code Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-04-13 21:58 +0100
    Re: Malloc without free in linked list code David Brown <david.brown@hesbynett.no> - 2015-04-14 09:26 +0200
      Re: Malloc without free in linked list code Paul <pepstein5@gmail.com> - 2015-04-14 01:45 -0700
        Re: Malloc without free in linked list code David Brown <david.brown@hesbynett.no> - 2015-04-14 11:39 +0200
          Re: Malloc without free in linked list code Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-04-14 13:43 +0100
            Re: Malloc without free in linked list code David Brown <david.brown@hesbynett.no> - 2015-04-14 15:10 +0200
              Re: Malloc without free in linked list code Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-04-14 16:10 +0100
                Re: Malloc without free in linked list code David Brown <david.brown@hesbynett.no> - 2015-04-14 17:37 +0200
                Re: Malloc without free in linked list code Keith Thompson <kst-u@mib.org> - 2015-04-14 11:44 -0700
                Re: Malloc without free in linked list code David Brown <david.brown@hesbynett.no> - 2015-04-15 00:00 +0200
                Re: Malloc without free in linked list code Richard Heathfield <rjh@cpax.org.uk> - 2015-04-14 20:12 +0100
                Re: Malloc without free in linked list code David Brown <david.brown@hesbynett.no> - 2015-04-15 00:04 +0200
                Re: Malloc without free in linked list code Richard Heathfield <rjh@cpax.org.uk> - 2015-04-15 00:20 +0100
              Re: Malloc without free in linked list code Tim Rentsch <txr@alumni.caltech.edu> - 2015-04-16 15:46 -0700
                Re: Malloc without free in linked list code David Brown <david.brown@hesbynett.no> - 2015-04-17 10:38 +0200
                Re: Malloc without free in linked list code Tim Rentsch <txr@alumni.caltech.edu> - 2015-04-17 08:30 -0700
        Re: Malloc without free in linked list code James Kuyper <jameskuyper@verizon.net> - 2015-04-14 08:04 -0400
  Re: Malloc without free in linked list code Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-04-14 05:38 -0700
  Re: Malloc without free in linked list code BGB <cr88192@hotmail.com> - 2015-04-14 10:31 -0500
  Malloc without free in linked list code Chad <cdalten@gmail.com> - 2015-04-14 19:51 -0700
    Re: Malloc without free in linked list code James Kuyper <jameskuyper@verizon.net> - 2015-04-14 23:10 -0400

csiph-web