From owner-freebsd-hackers Fri Jun 8 5:44:15 2001 Delivered-To: freebsd-hackers@freebsd.org Received: from ringworld.nanolink.com (ringworld.nanolink.com [195.24.48.13]) by hub.freebsd.org (Postfix) with SMTP id A839137B405 for ; Fri, 8 Jun 2001 05:44:05 -0700 (PDT) (envelope-from roam@ringworld.nanolink.com) Received: (qmail 7693 invoked by uid 1000); 8 Jun 2001 12:42:49 -0000 Date: Fri, 8 Jun 2001 15:42:49 +0300 From: Peter Pentchev To: Thomas David Rivers Cc: jhb@FreeBSD.ORG, hackers@FreeBSD.ORG Subject: Re: free() and const warnings Message-ID: <20010608154249.A7671@ringworld.oblivion.bg> Mail-Followup-To: Thomas David Rivers , jhb@FreeBSD.ORG, hackers@FreeBSD.ORG References: <20010608114957.C19938@ringworld.oblivion.bg> <200106081055.GAA49069@lakes.dignus.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.2.5i In-Reply-To: <200106081055.GAA49069@lakes.dignus.com>; from rivers@dignus.com on Fri, Jun 08, 2001 at 06:55:50AM -0400 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 On Fri, Jun 08, 2001 at 06:55:50AM -0400, Thomas David Rivers wrote: > 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? GCC complains when I try to initialize the structure with something like: struct validation_fun val_init[] = { {"init", valfun_init, 0} }; This can be avoided by: struct validation_fun val_init[] = { {(char *) (uintptr_t) "init", valfun_init, 0} }; ..but as a matter of fact, static, pre-initialized valfun structs are the rule rather than the exception in this program, so having this syntax for all of them seems.. well.. ugly :) G'luck, Peter -- You have, of course, just begun reading the sentence that you have just finished reading. To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message