Article View: comp.std.c
Article #4501Re: Should free() take a pointer to const argument?
From: Lawrence Kirby
Date: Sun, 17 Dec 1995 18:37
Date: Sun, 17 Dec 1995 18:37
39 lines
2203 bytes
2203 bytes
In article <4av5ge$obs@hermes.acs.unt.edu>
johnw@jove.acs.unt.edu "John Robert Williams" writes:
>This seems to me to miss the point of "const", which is really a promise
>on the part of a function writer that the object pointed to by a pointer
>will survive the function call intact.
That is not a view that is supported by the text of the standard.
66.5.3 Type qualifiers
"If an attempt is made to modify an object defined with a const-qualified
type through use of an lvalue with non-const-qualified type, the
behaviour is undefined"
There is no way to allocate an object from the heap qualified in any
way. So freeing such an object is valid however qualified the type is
that the pointer passed to free is derived from.
>I don't think a function can make
>that claim if it destroys the object!
Think of it a different way: a function that takes a pointer to an object
of const qualified type promises not to write to the object by any means
derived from that pointer (although this is strictly beyond what the
language guarantees). Destroying an object does not involve writing to
it.
So really this is subjective. You believe it is desirable to be able to
assume that a function taking a pointer to a const type can't destroy
the object being pointed to, whereas I believe it makes sense to destroy a
const object (back to my const auto example) and hence it makes sense to
be able to destroy an object through a pointer to const type.
--
-----------------------------------------
Lawrence Kirby | fred@genesis.demon.co.uk
Wilts, England | 70734.126@compuserve.com
-----------------------------------------
Technical Details
Message-ID:
<819225428snz@genesis.demon.co.uk>Path:
arc1-ca.pugleaf.net!mail2news.demon.co.uk!genesis.demon.co.ukReferences:
<818626119snz@genesis.demon.co.uk> <4am3dn$6s9@natasha.rmii.com> <4av5ge$obs@hermes.acs.unt.edu>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!mail2news.demon.co.uk!genesis.demon.co.uk From: Lawrence Kirby <fred@genesis.demon.co.uk> Newsgroups: comp.std.c Subject: Re: Should free() take a pointer to const argument? Date: Sun, 17 Dec 95 18:37:08 GMT Organization: none Message-ID: <819225428snz@genesis.demon.co.uk> References: <818626119snz@genesis.demon.co.uk> <4am3dn$6s9@natasha.rmii.com> <4av5ge$obs@hermes.acs.unt.edu> Reply-To: fred@genesis.demon.co.uk X-NNTP-Posting-Host: genesis.demon.co.uk X-Newsreader: Demon Internet Simple News v1.27