473,883 Members | 1,730 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

what will happen after i use free()???

i come from china,and i'm sorry that my english is very poor.

now i'm studing data structure and i met some problem about c language.

could you tell me what will happen after i use free()? i mean once i
use free() on a pointer,what will the pointer points to ?

for example:

#include<stdio. h>
#include<stdlib .h>

void main()

{

int *p;
if(!p) printf("good");
else printf("fail");
p=(int *)malloc(100);
if(p) printf("\n\ngoo d");
else printf("\n\nfai l");

free(p);
if(!p) printf("\n\ngoo d");
else printf("\n\nfai l");

*p=100;
printf("\n\n%d" ,*p);
}

the result is:

fail

good

fail

100

why?

Mar 16 '06
67 3833
On Sun, 19 Mar 2006 21:03:36 GMT, in comp.lang.c , Keith Thompson
<ks***@mib.or g> wrote:
Mark McIntyre <ma**********@s pamcop.net> writes:
On Sun, 19 Mar 2006 14:49:17 +0100, in comp.lang.c , "Richard G.
Riley" <rg****@gmail.c om> wrote:
in this case I would
ask you HOW you intend malloc to return different memory banks for
different types considering you cant tell malloc what it is malloc'ing
for.


You might want to read up on how early Motorola chips worked, before
sounding off...


Richard's error, I think, was in assuming we were talking about
different calls to malloc() returning their results in different
registers. Reading about the M68K wouldn't clear that up.


No, but it might prove instructive nevertheless!
Mark McIntyre
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Mar 20 '06 #61
Jordan Abel opined:
On 2006-03-20, Vladimir S. Oka <no****@btopenw orld.com> wrote:
Arndt Jonasson opined:

CBFalconer <cb********@yah oo.com> writes:
Herbert Rosenau wrote:
> "Vladimir S. Oka" <no****@btopenw orld.com> wrote:
>
... snip ...
>>
>> Yes, since otherwise you won't be alerted by the compiler if
>> you fail to #include <stdlib.h>.
>
> Liar! Never ever cast the return value of a function returning
> void* - except you likes to land in the world of undefined
> behavior.

You are apparently trying to take over Dan Pops place in the
pantheon of diplomatic c.l.c posters. Calling someone a liar is
not going to get you very far. This is possibly a language
barrier
problem (which Dan never had). Maybe you meant to say "false" or
"not so" or even "I disagree" etc.

Possibly he wants to indicate that Vladimir is lying not to the
audience, but to the compiler. But if so, that could be better put.


Quite possibly. For some reason the disagreement continued long
after I have admitted to mistyping "yes" for "no" in the quote above
(that should also have been clear from the rest of the post, now
snipped).


The only other place in the rest of the post that would have
clarified was one word off from confirming the 'yes' and, since it
was sequentially later than the 'yes', was easily misread.


Yes, yes, yes. I do admit that my original post was messed up. My
subsequent ones were (I hope) quite clear, but that didn't seem to be
good enough.

I'll drop it here.

--
BR, Vladimir

Newton's Fourth Law: Every action has an equal and opposite
satisfaction.

Mar 20 '06 #62
"Vladimir S. Oka" wrote:
Jordan Abel opined:

.... snip ...

The only other place in the rest of the post that would have
clarified was one word off from confirming the 'yes' and, since
it was sequentially later than the 'yes', was easily misread.


Yes, yes, yes. I do admit that my original post was messed up.
My subsequent ones were (I hope) quite clear, but that didn't
seem to be good enough.

I'll drop it here.


Not a chance with this crowd. They have their teeth sunk into you,
and will worry this nonsense into the ground and back several times
before letting it die.

--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell. org/google/>
Also see <http://www.safalra.com/special/googlegroupsrep ly/>
Mar 21 '06 #63
On Sun, 19 Mar 2006 21:47:23 UTC, "Vladimir S. Oka"
<no****@btopenw orld.com> wrote:
Herbert Rosenau opined:
On Sun, 19 Mar 2006 13:00:22 UTC, "Vladimir S. Oka"
<no****@btopenw orld.com> wrote:
[snip]
OK, I think where the disconnect is.

We both agree that casting the return from `malloc()` is a Bad Thing.

Where we seem to disagree seems to be where the UB comes from if
<stdlib.h> is not included.
No.
You seem to think it's the cast that does it.
No, the cast hides the error and the diagnostic threout.
I, OTH, think that UB has already happened *before*, and independently
of the cast. Cast may just hide the diagnostic that may have warned
you of the problem. (FWIW, it does seem that a few other people here
share my view.)

< big snip >

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
Mar 21 '06 #64
Herbert Rosenau opined:
On Sun, 19 Mar 2006 21:47:23 UTC, "Vladimir S. Oka"
<no****@btopenw orld.com> wrote:
Herbert Rosenau opined:
> On Sun, 19 Mar 2006 13:00:22 UTC, "Vladimir S. Oka"
> <no****@btopenw orld.com> wrote:

[snip]

OK, I think where the disconnect is.

We both agree that casting the return from `malloc()` is a Bad
Thing.

Where we seem to disagree seems to be where the UB comes from if
<stdlib.h> is not included.


No.
You seem to think it's the cast that does it.


No, the cast hides the error and the diagnostic threout.


OK. You win.

--
BR, Vladimir

This MUST be a good party -- My RIB CAGE is being painfully pressed up
against someone's MARTINI!!

Mar 21 '06 #65
"Vladimir S. Oka" <no****@btopenw orld.com> writes:
Herbert Rosenau opined:
On Sun, 19 Mar 2006 21:47:23 UTC, "Vladimir S. Oka"
<no****@btopenw orld.com> wrote:
Herbert Rosenau opined:

> On Sun, 19 Mar 2006 13:00:22 UTC, "Vladimir S. Oka"
> <no****@btopenw orld.com> wrote:

[snip]

OK, I think where the disconnect is.

We both agree that casting the return from `malloc()` is a Bad
Thing.

Where we seem to disagree seems to be where the UB comes from if
<stdlib.h> is not included.


No.
You seem to think it's the cast that does it.


No, the cast hides the error and the diagnostic threout.


OK. You win.


In what sense does he "win"? He clearly said previously that the cast
causes undefined behavior. Now he's changed is story, but without
acknowleding his error.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 21 '06 #66
Keith Thompson wrote:
"Vladimir S. Oka" <no****@btopenw orld.com> writes:
Herbert Rosenau opined:
.... snip ...

No, the cast hides the error and the diagnostic threout.


OK. You win.


In what sense does he "win"? He clearly said previously that
the cast causes undefined behavior. Now he's changed is story,
but without acknowleding his error.


I interpret Vladimirs posting as "I'm tired of this pointless
argument".

--
"Churchill and Bush can both be considered wartime leaders, just
as Secretariat and Mr Ed were both horses." - James Rhodes.
"We have always known that heedless self-interest was bad
morals. We now know that it is bad economics" - FDR
Mar 22 '06 #67
CBFalconer opined:
Keith Thompson wrote:
"Vladimir S. Oka" <no****@btopenw orld.com> writes:
Herbert Rosenau opined:
... snip ...
No, the cast hides the error and the diagnostic threout.

OK. You win.


In what sense does he "win"? He clearly said previously that
the cast causes undefined behavior. Now he's changed is story,
but without acknowleding his error.


I interpret Vladimirs posting as "I'm tired of this pointless
argument".


Exactly.

This abominable thread already had my head spinning from trying to
figure out what /is/ his POV after all.

[I also suspect he changed his mind but wwouldn't admit to it.]

--
BR, Vladimir

There was a young man of Ostend
Who let a girl play with his end.
She took hold of Rover,
And felt it all over,
And it did what she didn't intend.

Mar 22 '06 #68

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

Similar topics

220
19262
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have any preconceived ideas about it. I have noticed, however, that every programmer I talk to who's aware of Python is also talking about Ruby. So it seems that Ruby has the potential to compete with and displace Python. I'm curious on what basis it...
92
6579
by: Reed L. O'Brien | last post by:
I see rotor was removed for 2.4 and the docs say use an AES module provided separately... Is there a standard module that works alike or an AES module that works alike but with better encryption? cheers, reed
121
10255
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 support IDEs are DreamWeaver 8 and Zend PHP Studio. DreamWeaver provides full support for Unicode. However, DreamWeaver is a web editor rather than a PHP IDE. It only supports basic IntelliSense (or code completion) and doesn't have anything...
53
4113
by: Deniz Bahar | last post by:
I know the basic definition of a sequence point (point where all side effects guaranteed to be finished), but I am confused about this statement: "Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored." Can someone give me examples of expressions that "barely"...
18
2281
by: Ramasubbu Ramasubramanian XR (AS/EAB) | last post by:
What is memory leakage, could any one explain with sample code
13
5071
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
63
4842
by: Jake Barnes | last post by:
In the course of my research I stumbled upon this article by Alex Russel and Tim Scarfe: http://www.developer-x.com/content/innerhtml/default.html The case is made that innerHTML should never be used. I'm wondering, If I wanted all the content of BODY as a string, how else could I get except through innerHTML?
5
2850
by: Tom | last post by:
I have a function that restricts access to a page to logged in users. When a user who isn't logged in goes to the page, it will dynamically generate a login form. I'm trying to use it in conjunction with the free shared SSL certificate offered by my host. To use SSL, you would change a URL like this http://mydomain.com/page.php
14
1904
by: cat_dog_ass | last post by:
....Microsoft ports the .NET framework to other operating systems? Will the only advantage that Java has over .NET be lost? I've been a Java programmer and will be shifting to .NET soon. Is this a wise choice? Is the learning curve for C# a lot flatter than Java?
0
9781
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10836
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10407
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9567
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5794
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5982
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4606
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4211
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3230
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.