During a recent discussion (see comments on this answer ), R .. recommended never creating aliases for const pointer types since you won '(remember: free() takes a non const pointer argument and C99 6.3.2.3 permits transitions from unskilled to qualified )
The C language explicitly assumes the existence of an owner for any allocated object, that is, someone somewhere must store a pointer not const for the object, and this person is responsible for the release.
Now consider a library that allocates and initializes objects that do not change from user space code, so function calls always return const -qualified pointers.
Obviously, the library is the owner of the object and should not contain a pointer to const , which is somewhat stupid, since the user already supplies an absolutely correct, but const copy of the pointer to each library call.
To free such an object, the library must abandon the const classifier; as far as I can judge the following
void dealloc_foo(const struct foo *foo) { free((void *)foo); }
matters C; this would not be true if the foo parameter were additionally restrict -qualified.
However, casting const seems somewhat hacky.
Is there any other way, besides dropping const from all the return values ββof library functions, which will lose any information about the variability of the object?
c pointers const
Christoph Oct 22 '10 at 18:50 2010-10-22 18:50
source share