Article View: comp.std.c
Article #4478Re: Should free() take a pointer to const argument?
From: Scott Mayo
Date: Thu, 14 Dec 1995 20:58
Date: Thu, 14 Dec 1995 20:58
32 lines
1735 bytes
1735 bytes
In comp.std.c, Lawrence Kirby <fred@genesis.demon.co.uk> wrote:
<Is there any good reason why free shouldn't be declared as:
<void free(const void *ptr);
<in the standard? I don't believe there is any notion of altering a variable
<when it is destroyed - after all const auto variables are destroyed just like
<any other auto variable when they go out of scope.
I like implementations of free() that scribble on the memory they free,
at least as a compile time option. I have the sense that, as const
promises no modification to the object, that there would be some
murky water there.
I certainly would not want a compiler to assume that
int *p;
p = malloc(sizeof(*p)); /* assume nonNULL return */
*p = 3;
free(p); /* goodbye *p, free will piddle on you */
use(*p); /* hey! The compiler planted use(3) here! Blast that const! */
could safely assume that free(p) didn't modify *p, which the const
would imply. I'd rather have a free() that deliberately mucked *p and
then take the consequences, without the compiler hiding them.
(Yes, I know this code fragment is immoral and undefined. That's the
point. I want the runtime to have the right to trip me up and the
compiler not to "optimize" that tripping-up out of existance.)
--
Alt.devilbunnies - it's not a job, it's an adventure
Technical Details
Message-ID:
<bcb0.smail.smayo@iii.net>Path:
arc1-ca.pugleaf.net!iii2.iii.net!not-for-mailReferences:
<818626119snz@genesis.demon.co.uk>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!iii2.iii.net!not-for-mail From: smayo@iii.net (Scott Mayo) Newsgroups: comp.std.c Subject: Re: Should free() take a pointer to const argument? Date: 14 Dec 1995 15:58:18 -0500 Organization: Asylum III - purveyors of wierdnesses Sender: smayo@iii2.iii.net Message-ID: <bcb0.smail.smayo@iii.net> References: <818626119snz@genesis.demon.co.uk> NNTP-Posting-Host: iii2.iii.net