Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > free() question

Reply
Thread Tools

free() question

 
 
sandeep
Guest
Posts: n/a
 
      05-27-2010
It's a simple question..... can the free() function sometimes fail??
 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      05-27-2010
sandeep <> writes:

> It's a simple question..... can the free() function sometimes fail??


No.
--
Ben Pfaff
http://benpfaff.org
 
Reply With Quote
 
 
 
 
bart.c
Guest
Posts: n/a
 
      05-27-2010

"sandeep" <> wrote in message
news:htmq7n$bej$...
> It's a simple question..... can the free() function sometimes fail??


It can sometimes give undefined behaviour with invalid input (according to
the docs). But it doesn't return any status or set any error code.

--
Bartc

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      05-27-2010
sandeep <> writes:
> It's a simple question..... can the free() function sometimes fail??


Yes, but it can do so only in cases where the program's behavior
is undefined, which is why it has no mechanism for reporting failure.

For example:

int *p = malloc(sizeof *p); /* assume this succeded */
free(p); /* ok */
free(p); /* undefined behavior */

Another example:

int i;
int *p = &i;
free(p); /* undefined behavior */

I suppose it's debatable whether the undefined free() calls "fail";
it might be more accurate to say that the program failed by calling
free() with an invalid argument.

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      05-27-2010
On 5/27/2010 6:03 PM, sandeep wrote:
> It's a simple question..... can the free() function sometimes fail??


It depends on what you mean by "fail."

If you mean "attempt an operation and report whether the operation
was or was not carried out," the answer is No. That is, free() cannot
"fail" the way malloc() can, or fopen(), or even the way strchr() can.

If you include "exhibit undefined behavior" in your definition of
"fail," then the answer is Yes. If you try to free() something not
obtained from malloc() et al., or free() something more than once, or
scribble on your memory, or invoke U.B. somewhere else in the program,
the entire program -- including free() -- exhibits U.B.

--
Eric Sosman
lid
 
Reply With Quote
 
Seebs
Guest
Posts: n/a
 
      05-27-2010
On 2010-05-27, Eric Sosman <> wrote:
> On 5/27/2010 6:03 PM, sandeep wrote:
>> It's a simple question..... can the free() function sometimes fail??


> It depends on what you mean by "fail."


> If you mean "attempt an operation and report whether the operation
> was or was not carried out," the answer is No. That is, free() cannot
> "fail" the way malloc() can, or fopen(), or even the way strchr() can.


> If you include "exhibit undefined behavior" in your definition of
> "fail," then the answer is Yes. If you try to free() something not
> obtained from malloc() et al., or free() something more than once, or
> scribble on your memory, or invoke U.B. somewhere else in the program,
> the entire program -- including free() -- exhibits U.B.


The most interesting question, I think, is:

Can there ever be a valid input to free(), such that the implementation
cannot actually free the memory? I can easily describe an implementation
in which this could be the case. You could construct an implementation in
which there was some sort of "free list" which could require allocation
in order to add items to that list, and if you had precisely filled available
memory, it could be that you couldn't free something because there'd be no
way to add it to the available-things list.

That would be an unusual implementation, at best, but I'm not sure that it
is prohibited.

Of course, there'd be no way to find OUT that this had happened.

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / usenet-
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
 
Reply With Quote
 
Seebs
Guest
Posts: n/a
 
      05-28-2010
On 2010-05-28, Keith Thompson <kst-> wrote:
> Well, it would violate the requirement that "The free function causes
> the space pointed to by ptr to be deallocated, that is, made available
> for further allocation." (C99 7.20.3.2p2), so one could argue that
> it would be as non-conforming as this implementation:


> But yes, as long as there are no visible symptoms of the failure,
> it can't be detected. (Failure of later allocations doesn't count,
> since that can happen for any arbitrary reasons.)


Exactly. I'm not sure how you could tell. It seems like it'd be a pretty
bad implementation, but...

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / usenet-
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      05-28-2010
Seebs <usenet-> writes:
[...]
> The most interesting question, I think, is:
>
> Can there ever be a valid input to free(), such that the implementation
> cannot actually free the memory? I can easily describe an implementation
> in which this could be the case. You could construct an implementation in
> which there was some sort of "free list" which could require allocation
> in order to add items to that list, and if you had precisely filled available
> memory, it could be that you couldn't free something because there'd be no
> way to add it to the available-things list.
>
> That would be an unusual implementation, at best, but I'm not sure that it
> is prohibited.
>
> Of course, there'd be no way to find OUT that this had happened.


Well, it would violate the requirement that "The free function causes
the space pointed to by ptr to be deallocated, that is, made available
for further allocation." (C99 7.20.3.2p2), so one could argue that
it would be as non-conforming as this implementation:

void free(void *ptr)
{
/* nyaah nyaah! */
}

But yes, as long as there are no visible symptoms of the failure,
it can't be detected. (Failure of later allocations doesn't count,
since that can happen for any arbitrary reasons.)

Practically speaking, of course, the implementation could use the
space being freed to hold any bookkeeping data for the free list,
but implementations aren't required to behave sanely.

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Sjouke Burry
Guest
Posts: n/a
 
      05-28-2010
sandeep wrote:
> It's a simple question..... can the free() function sometimes fail??

Just give it a mangled pointer, and try it.
 
Reply With Quote
 
spinoza1111
Guest
Posts: n/a
 
      05-28-2010
On May 28, 6:25*am, Keith Thompson <ks...@mib.org> wrote:
> sandeep <nos...@nospam.com> writes:
> > It's a simple question..... can the free() function sometimes fail??

>
> Yes, but it can do so only in cases where the program's behavior
> is undefined, which is why it has no mechanism for reporting failure.
>
> For example:
>
> * * int *p = malloc(sizeof *p); /* assume this succeded */
> * * free(p); /* ok */
> * * free(p); /* undefined behavior */
>
> Another example:
>
> * * int i;
> * * int *p = &i;
> * * free(p); /* undefined behavior */
>
> I suppose it's debatable whether the undefined free() calls "fail";
> it might be more accurate to say that the program failed by calling
> free() with an invalid argument.
>
> --
> Keith Thompson (The_Other_Keith) ks...@mib.org *<http://www.ghoti.net/~kst>
> Nokia
> "We must do something. *This is something. *Therefore, we must do this."
> * * -- Antony Jay and Jonathan Lynn, "Yes Minister"


Good reply.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
question row filter (more of sql query question) =?Utf-8?B?YW5kcmV3MDA3?= ASP .Net 2 10-06-2005 01:07 PM
Quick Question - Newby Question =?Utf-8?B?UnlhbiBTbWl0aA==?= ASP .Net 4 02-16-2005 11:59 AM
Question on Transcender Question :-) eddiec MCSE 6 05-20-2004 06:59 AM
Question re: features of the 831 router (also a 924 question) Wayne Cisco 0 03-02-2004 07:57 PM
Syntax Question - Novice Question sean ASP .Net 1 10-20-2003 12:18 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57