From owner-cvs-all@FreeBSD.ORG Tue Dec 4 20:52:45 2007 Return-Path: Delivered-To: cvs-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 2C2CA16A468 for ; Tue, 4 Dec 2007 20:52:45 +0000 (UTC) (envelope-from christoph.mallon@gmx.de) Received: from mail.gmx.net (mail.gmx.net [213.165.64.20]) by mx1.freebsd.org (Postfix) with SMTP id 8190E13C45B for ; Tue, 4 Dec 2007 20:52:44 +0000 (UTC) (envelope-from christoph.mallon@gmx.de) Received: (qmail invoked by alias); 04 Dec 2007 20:52:43 -0000 Received: from p54A3C6E4.dip.t-dialin.net (EHLO tron.homeunix.org) [84.163.198.228] by mail.gmx.net (mp028) with SMTP; 04 Dec 2007 21:52:43 +0100 X-Authenticated: #1673122 X-Provags-ID: V01U2FsdGVkX19u5QU19mRuYX1FoTJ0YPX1hJLNjP7dZ7A/3yECWW iFgYJjDspLyq6w Message-ID: <4755BE19.4000709@gmx.de> Date: Tue, 04 Dec 2007 21:52:41 +0100 From: Christoph Mallon User-Agent: Thunderbird 2.0.0.6 (X11/20070806) MIME-Version: 1.0 To: Eygene Ryabinkin References: <200711232356.lANNu3mp040885@repoman.freebsd.org> <864pezer7f.fsf@ds4.des.no> <200712031657.34074.jhb@freebsd.org> <20071204172535.GB82261@FreeBSD.org> In-Reply-To: Content-Type: text/plain; charset=KOI8-R; format=flowed Content-Transfer-Encoding: 8bit X-Y-GMX-Trusted: 0 Cc: Alexey Dokuchaev , src-committers@freebsd.org, John Baldwin , cvs-src@freebsd.org, cvs-all@freebsd.org, John Birrell , Dag-Erling Sm??rgrav Subject: Re: cvs commit: src/sys/netinet/libalias alias_util.c X-BeenThere: cvs-all@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: CVS commit messages for the entire tree List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 04 Dec 2007 20:52:45 -0000 Eygene Ryabinkin wrote: > Tue, Dec 04, 2007 at 05:25:35PM +0000, Alexey Dokuchaev wrote: >>> *ptr++ would choke since pointer arith on (void *) is undefined AFAIK. >> I've been under impression that ++ on void * whould simply increase it >> by one. > > This behaviour is documented for GCC: > http://www.mcs.vuw.ac.nz/cgi-bin/info2www?(gcc)Pointer+Arith > > Just for the record (gcc 4.2.1): > ----- > $ gcc -o test -Wall -ansi -pedantic test.c > test.c: In function 'main': > test.c:9: warning: wrong type argument to increment > > $ ./test > '2' > > $ g++ -o test -Wall -ansi test.c > test.c: In function 'int main()': > test.c:9: error: ISO C++ forbids incrementing a pointer of type 'void*' > > $ cat test.c > #include > > int > main(void) > { > char c[] = "123456789abcdef"; > void *p = c; > > p++; > printf("'%c'\n", *((char *)p)); > return 0; > } > ----- > > It seems to me that ++ adds one to the void pointer because it is > demanded by C99 (ISO/IEC 9899:TC2, 6.2.5, requirement 26, page 36) > that 'char *' and 'void *' have the same representation and > alignment requirements. So, it seems to me that (p++) has implicit > conversion from 'void *' to 'char *' for 'void *p', at least it > can be interpreted in this way. > > But some people say that void* arithmetics is GCC'ism. It worth to > note that the warning about void* arithmetics lived in GCC at least > since 1992: see > http://gcc.gnu.org/viewcvs/trunk/gcc/c-typeck.c?revision=364&view=markup > function 'pointer_int_sum'. > > And the problem of 'void *' arithmetics had been touched in the > -current a while ago: > http://lists.freebsd.org/pipermail/freebsd-current/2003-July/006439.html > > I am failing to find a place in the C standard where void arithmetics > is prohibited, but I can be blind. Anyone? Arithmethic on void pointers is forbidden. The relevant parts of the (C99) standard are: ?6.2.5 clause 19: "The void type comprises an empty set of values; it is an incomplete type that cannot be completed." ?6.2.5 clause 1: "[...] Types are partitioned into object types [...], function types [...], and incomplete types [...]. ?6.5.6 clause 2: "For addition [...] one operand shall be a pointer to an object type and the other shall have integer type. [...]" (subtraction has an analogous statement, increment and decrement are just addition/substraction by one) So the conclusion is: - void* is a pointer to an incomplete type. - Incomplete types are not object types. - Addition is only allowed on pointers to an object type. Therefore arithmetic on void pointers is not allowed. Arithmetic on void pointers is indeed a GCCism. Christoph