Subject: Re: asprintf prototype
To: None <tech-userlevel@netbsd.org>
From: Kevin Schoedel <schoedel@kw.igs.net>
List: tech-userlevel
Date: 08/17/1999 01:29:35
On 1999/08/16 at 7:44pm -0700, you wrote:
>On 16 Aug 1999, Johan Danielsson wrote:
>
># Ignatios Souvatzis <ignatios@cs.uni-bonn.de> writes:
>#
># > I've been told: because it destroys contents refered to by the pointer.
>#
># I guess that's the simple explanation, but if you are not allowed to
># use the data after calling free, does it really matter that it
># `destroys' it? Philosophical question.
>
>I think it might be because if you use a (const void *) in a function and
>start doing things with it, that (void *) isn't (const) anymore.

Consider, for example:

	a = *p;
	foo(p);
	bar(a);

If foo() is foo(const void *), the the compiler knows that *p is
unaffected by foo(), i.e. it's the same after the call as before.
Consequently the compiler is free to treat this as if it were:

	foo(p);
	bar(*p);

But if foo() is free(), *p might no longer exist at all. A common
real-life example:

	while (p) {
		q = p->next;
		free(p);
		p = q;
	}

is not the same as

	while (p) {
		free(p);
		p = p->next;
	}

-- 
Kevin Schoedel
schoedel@kw.igs.net