From owner-freebsd-current@FreeBSD.ORG Thu Jul 10 23:23:29 2003 Return-Path: Delivered-To: freebsd-current@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id BE42837B401 for ; Thu, 10 Jul 2003 23:23:29 -0700 (PDT) Received: from mailhub.fokus.fraunhofer.de (mailhub.fokus.fraunhofer.de [193.174.154.14]) by mx1.FreeBSD.org (Postfix) with ESMTP id 6D31E43F93 for ; Thu, 10 Jul 2003 23:23:28 -0700 (PDT) (envelope-from brandt@fokus.fraunhofer.de) Received: from beagle (beagle [193.175.132.100])h6B6NJv16423; Fri, 11 Jul 2003 08:23:20 +0200 (MEST) Date: Fri, 11 Jul 2003 08:23:19 +0200 (CEST) From: Harti Brandt To: David Leimbach In-Reply-To: <6006001.1057858723473.JavaMail.leimy2k@mac.com> Message-ID: <20030711082219.L38638@beagle.fokus.fraunhofer.de> References: <6006001.1057858723473.JavaMail.leimy2k@mac.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII cc: Luigi Rizzo cc: current@freebsd.org Subject: Re: what is the suggested way to do void * arithmetic ? X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 11 Jul 2003 06:23:30 -0000 On Thu, 10 Jul 2003, David Leimbach wrote: DL> I always feel better when I convert void * to char * but that's probably DL>because C++ doesn't allow pointer arithmetic on void *'s. The argument DL>being that you don't know the size of what's being pointed to with a void * DL>and therefore can't know how far to seek the the pointer to get to the next DL>valid address. DL> DL>I think C takes a more low-level approach and says "void * is just an address DL>void * + 1 means the next valid address". void pointer arithmetic is a sun and gcc specific extension and by no means a standardized thing. DL> DL>Anyway... it just seems to help when porting code between C/C++ to use DL>char *... Yes. harti DL>Dave DL>On Thursday, July 10, 2003, at 11:40AM, Luigi Rizzo wrote: DL> DL>>On Thu, Jul 10, 2003 at 03:42:04AM -0700, Terry Lambert wrote: DL>>> Luigi Rizzo wrote: DL>>> > in several places in ipfw2.c i have to move pointers across DL>>> > structures of variable length (lists of ipfw2 instructions DL>>> > returned by the getsockopt()), and i use the following type of code: DL>>> > DL>>> > void *next; DL>>> > foo *p; DL>>> > next = (void *)p + len; DL>>> > foo = (foo *)p + len; DL>> ^^^^^^^^^^^^^^ DL>> DL>>sorry i meant p = (void *)p + len; DL>> DL>>... DL>>> I don't understand the second one. The first one blows up because DL>>> you aren't parenthesizing, e.g.: DL>>> DL>>> next = (void *)(p + len); DL>>> DL>>> The compiler is complaining because it doesn't know sizeof(*((void *)0)) DL>> DL>>ok, it actually evaluates to 1 and i thought it was some standard, probably DL>>it is not so i guess i have to cast to (char *) instead DL>> DL>> thanks DL>> luigi DL>> DL>>> (pointer arithmatic is coerced to the type of the lvalue, in most DL>>> cases of casts). DL>>> DL>>> Unless you are referencing them as array elements (in which case, DL>>> packing becomes a problem for you, when referencing them as arrays DL>>> of foo's, since you don't know how foo's are packed in an array), DL>>> you should probably cast them to char for the arithmatic, and add DL>>> them with byte counts. DL>>> DL>>> -- Terry DL>>_______________________________________________ DL>>freebsd-current@freebsd.org mailing list DL>>http://lists.freebsd.org/mailman/listinfo/freebsd-current DL>>To unsubscribe, send any mail to "freebsd-current-unsubscribe@freebsd.org" DL>> DL>> DL>_______________________________________________ DL>freebsd-current@freebsd.org mailing list DL>http://lists.freebsd.org/mailman/listinfo/freebsd-current DL>To unsubscribe, send any mail to "freebsd-current-unsubscribe@freebsd.org" DL> -- harti brandt, http://www.fokus.fraunhofer.de/research/cc/cats/employees/hartmut.brandt/private brandt@fokus.fraunhofer.de, harti@freebsd.org