From owner-freebsd-hackers Fri Jun 8 3:53:36 2001 Delivered-To: freebsd-hackers@freebsd.org Received: from fjord.dignus.com (sdsl-66-80-58-206.dsl.lax.megapath.net [66.80.58.206]) by hub.freebsd.org (Postfix) with ESMTP id 71C2137B407; Fri, 8 Jun 2001 03:53:32 -0700 (PDT) (envelope-from rivers@dignus.com) Received: from lakes.dignus.com (lakes.dignus.com [10.0.0.3]) by fjord.dignus.com (8.11.1/8.11.1) with ESMTP id f58AsTf99215; Fri, 8 Jun 2001 06:54:29 -0400 (EDT) (envelope-from rivers@dignus.com) Received: (from rivers@localhost) by lakes.dignus.com (8.9.3/8.6.9) id GAA49069; Fri, 8 Jun 2001 06:55:50 -0400 (EDT) Date: Fri, 8 Jun 2001 06:55:50 -0400 (EDT) From: Thomas David Rivers Message-Id: <200106081055.GAA49069@lakes.dignus.com> To: jhb@FreeBSD.ORG, roam@orbitel.bg Subject: Re: free() and const warnings Cc: hackers@FreeBSD.ORG In-Reply-To: <20010608114957.C19938@ringworld.oblivion.bg> Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG Peter Pentchev wrote: > > 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 Since some strings are non-constant (the are allocated) - I believe the `const' qualifier in the structure declaration is incorrect. What happens if you simply don't have it in the structure declaration? - Dave Rivers - -- rivers@dignus.com Work: (919) 676-0847 Get your mainframe programming tools at http://www.dignus.com To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message