Date: Fri, 8 Jun 2001 11:49:57 +0300 From: Peter Pentchev <roam@orbitel.bg> To: John Baldwin <jhb@FreeBSD.org> Cc: hackers@FreeBSD.org Subject: Re: free() and const warnings Message-ID: <20010608114957.C19938@ringworld.oblivion.bg> In-Reply-To: <XFMail.010607102051.jhb@FreeBSD.org>; from jhb@FreeBSD.org on Thu, Jun 07, 2001 at 10:20:51AM -0700 References: <20010607195634.I724@ringworld.oblivion.bg> <XFMail.010607102051.jhb@FreeBSD.org>
next in thread | previous in thread | raw e-mail | index | archive | help
On Thu, Jun 07, 2001 at 10:20:51AM -0700, John Baldwin wrote: > > On 07-Jun-01 Peter Pentchev wrote: > > On Thu, Jun 07, 2001 at 07:07:22PM +0300, Peter Pentchev wrote: > >> Hi, > >> > >> Is free((void *) (size_t) ptr) the only way to free a const whatever *ptr > >> with WARNS=2? (or more specifically, with -Wcast-qual) > > > > Uhm. OK. So size_t may not be enough to hold a pointer. What is it then - > > caddr_t? > > uintptr_t for data pointers. In theory I think code pointers may not fit in a > uintptr_t. > > free((void *)(uintptr_t)ptr) should work. > > Of course, this begs the question of why you are free'ing a const. :) OK, here's a scenario: struct validation_fun { const char *name; valfun *fun; int dyn; }; This is a structure for a set of validation functions, referenced by name from another type of object. There are some predefined functions, present in the code at compile-time, and hardcoded in an array, with names given as "strings". Thus, the 'const'. However, some of the functions may be defined at runtime, with both name and code sent by a server. In that case, the name is a dynamically allocated char *, which needs to be freed upon cleanup. So I have: [cleanup function] ... if (val->dyn) free(val->name); Any suggestions on how to improve the design to avoid this, if possible, would be greatly welcome. G'luck, Peter -- I am the meaning of this sentence. To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20010608114957.C19938>