Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1758998AbYARLrV (ORCPT ); Fri, 18 Jan 2008 06:47:21 -0500 Received: (majordomo@vger.kernel.org) by vger.kernel.org id S1754552AbYARLrN (ORCPT ); Fri, 18 Jan 2008 06:47:13 -0500 Received: from orion2.pixelized.ch ([195.190.190.13]:40223 "EHLO mail.pixelized.ch" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754540AbYARLrM (ORCPT ); Fri, 18 Jan 2008 06:47:12 -0500 Message-ID: <479091B5.4060700@cateee.net> Date: Fri, 18 Jan 2008 12:47:01 +0100 From: "Giacomo A. Catenazzi" User-Agent: Thunderbird 2.0.0.9 (Windows/20071031) MIME-Version: 1.0 To: Jakob Oestergaard , Linus Torvalds , David Schwartz , Johannes Weiner , Linux Kernel Mailing List , clameter@sgi.com, penberg@cs.helsinki.fi Subject: Re: Why is the kfree() argument const? References: <20080118094826.GN25527@unthought.net> In-Reply-To: <20080118094826.GN25527@unthought.net> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 1858 Lines: 75 Jakob Oestergaard wrote: > On Thu, Jan 17, 2008 at 01:25:39PM -0800, Linus Torvalds wrote: > ... >> Why do you make that mistake, when it is PROVABLY NOT TRUE! >> >> Try this trivial program: >> >> int main(int argc, char **argv) >> { >> int i; >> const int *c; >> >> i = 5; >> c = &i; >> i = 10; >> return *c; >> } >> >> and realize that according to the C rules, if it returns anything but 10, >> the compiler is *buggy*. > > That's not how this works (as we obviously agree). > > Please consider a rewrite of your example, demonstrating the usefulness and > proper application of const pointers: > > extern foo(const int *); > > int main(int argc, char **argv) > { > int i; > > i = 5; > foo(&i); > return i; > } > > Now, if the program returns anything else than 5, it means someone cast away > const, which is generally considered a bad idea in most other software > projects, for this very reason. > > *That* is the purpose of const pointers. "restrict" exists for this reason. const is only about lvalue. You should draw a line, not to make C more complex! Changing the name of variables in your example: extern print_int(const int *); int main(int argc, char **argv) { extern int errno; errno = 0; print_int(&i); return errno; } print_int() doesn't know that errno is also the argument. and this compilation unit doesn't know that print_int() will modify errno. Ok, I changed int to extern int, but you see the point? Do you want complex rules about const, depending on context (extern, volatile,...) ? ciao cate -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/