473,322 Members | 1,523 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

what's wrong with this code?

hello,

i am very new to C and i am reading K&R and i came
accross this code in the book. K&R says that it is
incorrect to free something after it has been freed
and gives this as an example.

for (p=head; p!= NULL; p->next)
free(p);

what's wrong with it?

Nov 13 '05 #1
5 3867
TDR
Well looks of things the "p->next" part doesn't really advance the pointer p
as a result on the first run of the loop, the pointer p is freed but on the
second (since it was not advance and was already freed) it gets freed
second.
Now I don't really remember what would happen then (usually it just works or
just plain crashes as I rarely met such cases).

hope this helps

TDR
"cc0064263" <ca*************@yahoo.com> wrote in message
news:DM********************@comcast.com...
hello,

i am very new to C and i am reading K&R and i came
accross this code in the book. K&R says that it is
incorrect to free something after it has been freed
and gives this as an example.

for (p=head; p!= NULL; p->next)
free(p);

what's wrong with it?

Nov 13 '05 #2
In article <DM********************@comcast.com>, cc0064263 wrote:
hello,

i am very new to C and i am reading K&R and i came
accross this code in the book. K&R says that it is
incorrect to free something after it has been freed
and gives this as an example.

for (p=head; p!= NULL; p->next)
free(p);

what's wrong with it?

There are at least two problems with this code: p->next doesn't do anything
and p is accessed after it's been freed.

Even if the first problem weren't there (i.e. the code said `p = p->next')
the second would still (possibly) kill your pprogram.

The for loop in while form looks like this:

p = head;
while (p != NULL)
{
free(p);
p = p->next;
}

perhaps it is a bit clearer now why p is accessed after the free?

To do it right, you can do it like this:

p_type p = head;
p_type last = NULL;
while (p != NULL)
{
last = p;
p = p->next;
free(last);
}

(in which p_type is some pointer type). Here, you save the old value of p to
last, get the value you want for p, and *then* free the saved, old value.

HTH

rlc

--
Jail: Just Another Interpreted Language
Just: Jail Uses Silly Terms

Join the discussion on the definition of this language at
ja***********@lists.sourceforge.net http://jail-ust.sourceforge.net
(send mail to ja*********************@lists.sourceforge.net)
Nov 13 '05 #3
TDR
( I made a typo on the first reply)
"TDR" <td*******@hotmail.com> wrote in message
news:3f********@news.tm.net.my...
Well looks of things the "p->next" part doesn't really advance the pointer
p
as a result on the first run of the loop, the pointer p is freed but on the
second run of the loop (since it was not advance and was already freed) it
gets freed a
second time.
Now I don't really remember what would happen then (usually it just works
or
just plain crashes as I rarely met such cases).

hope this helps

TDR
"cc0064263" <ca*************@yahoo.com> wrote in message
news:DM********************@comcast.com...
hello,

i am very new to C and i am reading K&R and i came
accross this code in the book. K&R says that it is
incorrect to free something after it has been freed
and gives this as an example.

for (p=head; p!= NULL; p->next)
free(p);

what's wrong with it?


Nov 13 '05 #4
"cc0064263" <ca*************@yahoo.com> wrote:
hello,

i am very new to C and i am reading K&R and i came
accross this code in the book. K&R says that it is
incorrect to free something after it has been freed
and gives this as an example.

for (p=head; p!= NULL; p->next) ^^^^^^^ should be p = p->next
free(p);

what's wrong with it?


Look at the equivalent with a while loop:

p = head;
while(p != NULL)
{
free(p);
p = p->next;
}

Now you see the problem is that after p has been freed (now the
pointer is invalid and its contents lost), you still try to use
the struct's 'next' member.

One possible fix is to change it to:
for(p = head; p != NULL; p = tmp)
{
tmp = p->next;
free(p);
}

--
Simon.
Nov 13 '05 #5
cc0064263 wrote:
thanks all for pointing it out.
the while loop does make things easier to see.
maybe i just needed my coffee.
for (p=head; p!= NULL; p->next)
free(p);

what's wrong with it?

Nov 13 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
72
by: E. Robert Tisdale | last post by:
What makes a good C/C++ programmer? Would you be surprised if I told you that it has almost nothing to do with your knowledge of C or C++? There isn't much difference in productivity, for...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
51
by: WindAndWaves | last post by:
Can anyone tell me what is wrong with the goto command. I noticed it is one of those NEVER USE. I can understand that it may lead to confusing code, but I often use it like this: is this...
46
by: Keith K | last post by:
Having developed with VB since 1992, I am now VERY interested in C#. I've written several applications with C# and I do enjoy the language. What C# Needs: There are a few things that I do...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
1
by: GS | last post by:
I got a combobox box that I load at load time. the Item and vales ended up in reverse order of each other, what went wrong? the database table has the following row code value ebay ...
98
by: tjb | last post by:
I often see code like this: /// <summary> /// Removes a node. /// </summary> /// <param name="node">The node to remove.</param> public void RemoveNode(Node node) { <...> }
9
by: Pyenos | last post by:
import cPickle, shelve could someone tell me what things are wrong with my code? class progress: PROGRESS_TABLE_ACTIONS= DEFAULT_PROGRESS_DATA_FILE="progress_data" PROGRESS_OUTCOMES=
20
by: Daniel.C | last post by:
Hello. I just copied this code from my book with no modification : #include <stdio.h> /* count characters in input; 1st version */ main() { long nc; nc = 0;
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.