From owner-freebsd-hackers Wed Aug 2 12:23:22 2000 Delivered-To: freebsd-hackers@freebsd.org Received: from mass.osd.bsdi.com (adsl-63-202-177-42.dsl.snfc21.pacbell.net [63.202.177.42]) by hub.freebsd.org (Postfix) with ESMTP id 4397437B926 for ; Wed, 2 Aug 2000 12:23:14 -0700 (PDT) (envelope-from msmith@mass.osd.bsdi.com) Received: from mass.osd.bsdi.com (localhost [127.0.0.1]) by mass.osd.bsdi.com (8.9.3/8.9.3) with ESMTP id MAA01407; Wed, 2 Aug 2000 12:34:09 -0700 (PDT) (envelope-from msmith@mass.osd.bsdi.com) Message-Id: <200008021934.MAA01407@mass.osd.bsdi.com> X-Mailer: exmh version 2.1.1 10/15/1999 Cc: "Daniel C. Sobral" , chris@calldei.com, freebsd-hackers@FreeBSD.ORG Subject: Re: malloc to arrays? In-reply-to: Your message of "Wed, 02 Aug 2000 23:13:05 +0400." <200008021913.XAA00739@cicuta.babolo.ru> Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Date: Wed, 02 Aug 2000 12:34:09 -0700 From: Mike Smith Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG > Yes, and whan I use u_int32_t *(tcpcash_addr[256]) declaration, > I can use tcpcash_addr[x][y], and I must use > tcpcash_addr[x * 256 + y] if declaration is u_int32_t *tcphash_haddr, > I cant use if u_int32_t **tcphash_haddr, > because of need of array of pointers in addition to array of arrays, > and the only clean but not nice way I know is > struct tcphash_haddr {u_int32_t tcpcash_addr[256];}; > struct tcphash_haddr *tcphash_haddr; > but tcphash_haddr[x].tcpcash_addr[y] is not so clear > as tcpcash_addr[x][y]... > = > Is the reason to use *(foo[nmemb]) syntax clear enough? It's only "clear" insofar as it's "clear" that the code is terrible. #define TCPCASH_ROWSIZE 256 #define TCPCASH_COLSIZE 256 #define TCPCASH_ADDR(x) ((x) * TCPCASH_ROWSIZE) u_int32_t *tcpcash_addr tcpcash_addr =3D (typeof(tcpcash_addr)) malloc(sizeof(*tcpcash_addr) * = TCPCASH_ROWSIZE * = TCPCASH_COOLSIZE); =2E.. foo =3D tcpcash[TCPCASH_ADDR(x)]; This is fast and unambiguous, as well as keeping all the magic numbers = away from the actual code. Oh, and you spell "cache" like that. "cash" is money. -- = =2E.. every activity meets with opposition, everyone who acts has his rivals and unfortunately opponents also. But not because people want to be opponents, rather because the tasks and relationships force people to take different points of view. [Dr. Fritz Todt] To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message