473,900 Members | 3,343 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 3836
On Sun, 19 Mar 2006 17:58:58 UTC, Jordan Abel <ra*******@gmai l.com>
wrote:
On 2006-03-19, Herbert Rosenau <os****@pc-rosenau.de> wrote:
On Sun, 19 Mar 2006 11:27:39 UTC, Mark McIntyre
<ma**********@s pamcop.net> wrote:
On Sun, 19 Mar 2006 08:54:20 +0000 (UTC), in comp.lang.c , "Herbert
Rosenau" <os****@pc-rosenau.de> wrote:

> (<any cast>) malloc() results in
>undefined behavor, p=malloc() is the only secure call allowed in C.

Rubbish. Inserting a cast before malloc is not needed, but doesn't
result in UB.
You should program under an environment that would use for return
valuses of different types different locations and then use casting on
functions returning other than int, forgetting to #include stdlib you
gets undefined behavior


The undefined behavior is not caused by the cast, and would happen
without it.


So far so right - but it will hide the diagnostic that would at least
help to fix the problem, even as the diagnostic message itself may
irretate an beginner. The cast will noways able to fix the real
problem and is at least a problem on itself.
>There is under absolutely no circomstance a need for a cast of (void*)
>to anything else.

variadic functions?

That'd be more likely to need to be a cast TO void * than FROM void *.
Or a cast of a null pointer constant, which could just as easily be 0 as
(void*)0.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
Mar 19 '06 #51
On 2006-03-19, Herbert Rosenau <os****@pc-rosenau.de> wrote:
On Sun, 19 Mar 2006 17:55:16 UTC, Jordan Abel <ra*******@gmai l.com>
wrote:
> I still don't think casting the result off `malloc()` results in
> undefined behaviour. Why would it? Remember, we were talking about
> casting to a different /pointer/ type, not just /any/ type.


Correct. If the cast masks an error message, the undefined behavior has
already happened.


No, the cast is to hide any diagnostic - it is still a lie to the
compiler.


The cast is not the lie. The implicit declaration is the lie.
Mar 19 '06 #52
On 2006-03-19, Herbert Rosenau <os****@pc-rosenau.de> wrote:
On Sun, 19 Mar 2006 15:12:17 UTC, ri*****@cogsci. ed.ac.uk (Richard
Tobin) wrote:
In article <wm************ *************** @URANUS1.DV-ROSENAU.DE>,
Herbert Rosenau <os****@pc-rosenau.de> wrote:
>You should program under an environment that would use for return
>valuses of different types different locations and then use casting on
>functions returning other than int, forgetting to #include stdlib you
>gets undefined behavior


Using malloc() without a declaration will cause undefined behaviour,
but casting the result of malloc() will not. The main problem with
casting it is that it may disguise the fact that it's not declared.


You're in error. Who guarantees that your compiler will give you
exactly the same bits in the same meaning that malloc returns? There
are many reasons why the cast of the result of the undecleard malloc
give you something that is absolutely independant from the value
malloc has given back to the caller -> undefined behavior is
guaranteed.


The undefined behavior has ALREADY HAPPENED, though. The only undefined
behavior involved is the call to an incorrectly declared function.
Conversion from int to pointer is not itself undefined, it's
implementation-defined.

Mar 19 '06 #53
Herbert Rosenau opined:
On Sun, 19 Mar 2006 13:00:22 UTC, "Vladimir S. Oka"
<no****@btopenw orld.com> wrote:
Herbert Rosenau opined:
> On Sun, 19 Mar 2006 11:27:39 UTC, Mark McIntyre
> <ma**********@s pamcop.net> wrote:
>
>> On Sun, 19 Mar 2006 08:54:20 +0000 (UTC), in comp.lang.c ,
>> "Herbert Rosenau" <os****@pc-rosenau.de> wrote:
>>
>> > (<any cast>) malloc() results in
>> >undefined behavor, p=malloc() is the only secure call allowed in
>> >C.
>>
>> Rubbish. Inserting a cast before malloc is not needed, but
>> doesn't result in UB.
>
> You should program under an environment that would use for return
> valuses of different types different locations and then use
> casting on functions returning other than int,
Again, can you please elaborate /why/ you think this matters
(physical storage of return values)?


C Standard C makes absolutely no assumption how an implementation
should handle such things as returning values from functions. So each
implementation does it in a way best resolved by the underlying
hardware. Intel on on that aspect one of the littlest market ever
seen even as M$ means that they have the mayority.


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.

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

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 >
Apart from calling people names again, you also forgot to breathe.
Your posts are increasingly resembling any of the dozens of
definitions of flaming. You risk becoming a troll...

That doesn't disturb me. Only trolls will name others who tries to
teach a troll.


I may have over-reacted.
Here you'll see nearly dayly an idiot who means that casting to hide
diagnostics is ok. True, many peoples coming here have learned that
by reading defektive books, broken code from idiots, so I try to help
them to get it right whenever I have free time to do so.
You seem to have quite a lot at the moment. ;-)
As this thread is running for more than a week now I get allergic to
read again and again therin casting results returning from functions
like malloc is ok.
I don't think anyone was arguing that. I know I wasn't.
It looks like there is a need to drum into somebody that casting is
almost the wrongest method to resolve a problem.
It does have its uses, though.
Use cast only if you are absolutely sure that yor really needs the
cast and after you have checked and rechecked again and again that
you really knows what you does and not on the way to lie to the
compiler. lie to the compiler and it will get its dreadful revange.
As others have pointed out, in the case we were discussing, it's not
the cast that is lying, rather not including <stdlib.h> is.
Myself had learned that more than 20 years ago even on K&RII by
falling through cede inspections, failing on getting the results I
expected before I learned how to use casts right. There was no usenet
ready to learn from others.


Why does this suddenly reminds me of the Yorkshiremen joke... ;-)

--
BR, Vladimir

SERENDIPITY:
The process by which human knowledge is advanced.

Mar 19 '06 #54
* Herbert Rosenau <os****@pc-rosenau.de> wrote:
Here you'll see nearly dayly an idiot who means that casting to hide
diagnostics is ok. True, many peoples coming here have learned that by
reading defektive books, broken code from idiots, so I try to help
them to get it right whenever I have free time to do so.


Many people learn the malloc cast from K&R 2nd edition and this link
would help more than calling them idiots:

http://cm.bell-labs.com/cm/cs/cbook/2ediffs.html
Stefan Krah
Mar 19 '06 #55
In article <wm************ *************** @URANUS1.DV-ROSENAU.DE>,
Herbert Rosenau <os****@pc-rosenau.de> wrote:
You're in error. Who guarantees that your compiler will give you
exactly the same bits in the same meaning that malloc returns? There
are many reasons why the cast of the result of the undecleard malloc
give you something that is absolutely independant from the value
malloc has given back to the caller -> undefined behavior is
guaranteed.


Using the return value from an undeclared malloc *at all* will produce
undefined behaviour. Even if you don't cast it. The cast doesn't make
the behaviour any more undefined.

Conside the following fragments covering all the possibilities of
whether malloc is declared and whether the result is cast:

(1) #include <stdlib.h>
...
int *p = malloc(sizeof(* p));

(2) #include <stdlib.h>
...
int *p = (int *)malloc(sizeof (*p));

(3) /* no declaration of malloc */
...
int *p = malloc(sizeof(* p));

(4) /* no declaration of malloc */
...
int *p = (int *)malloc(sizeof (*p));

Which ones produce undefined behaviour? Does the presence of the cast
make any difference?

-- Richard
Mar 19 '06 #56
"Herbert Rosenau" <os****@pc-rosenau.de> wrote:
On Sun, 19 Mar 2006 17:58:58 UTC, Jordan Abel <ra*******@gmai l.com>
wrote:
On 2006-03-19, Herbert Rosenau <os****@pc-rosenau.de> wrote:
On Sun, 19 Mar 2006 11:27:39 UTC, Mark McIntyre
<ma**********@s pamcop.net> wrote:

> On Sun, 19 Mar 2006 08:54:20 +0000 (UTC), in comp.lang.c , "Herbert
> Rosenau" <os****@pc-rosenau.de> wrote:
>
> > (<any cast>) malloc() results in
> >undefined behavor, p=malloc() is the only secure call allowed in C.
>
> Rubbish. Inserting a cast before malloc is not needed, but doesn't
> result in UB.

You should program under an environment that would use for return
valuses of different types different locations and then use casting on
functions returning other than int, forgetting to #include stdlib you
gets undefined behavior


The undefined behavior is not caused by the cast, and would happen
without it.


So far so right - but it will hide the diagnostic that would at least
help to fix the problem,


Of course. Nobody has denied this. What has been said all along is that
the cast _hides_, but does not itself _cause_, undefined behaviour.

Richard
Mar 20 '06 #57

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.
Mar 20 '06 #58
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).
See elsethread...

--
BR, Vladimir

Don't I know you?

Mar 20 '06 #59
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.
See elsethread...

Mar 20 '06 #60

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

Similar topics

220
19284
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
6592
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
10276
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
4114
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
5074
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
4847
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
1909
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
9997
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10866
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10976
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
10497
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
9671
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
7204
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4721
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
4301
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3320
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.