From owner-freebsd-hackers Wed Aug 2 12:13:36 2000 Delivered-To: freebsd-hackers@freebsd.org Received: from cicuta.babolo.ru (babolo.ru [194.58.226.160]) by hub.freebsd.org (Postfix) with ESMTP id C4E1E37B6C5 for ; Wed, 2 Aug 2000 12:13:25 -0700 (PDT) (envelope-from babolo@cicuta.babolo.ru) Received: (from babolo@localhost) by cicuta.babolo.ru (8.9.3/8.9.3) id XAA00739; Wed, 2 Aug 2000 23:13:06 +0400 (MSD) (envelope-from babolo) Message-Id: <200008021913.XAA00739@cicuta.babolo.ru> Subject: Re: malloc to arrays? In-Reply-To: <39879ECA.61855D9D@newsguy.com> from "Daniel C. Sobral" at "Aug 2, 2000 01:08:42 pm" To: "Daniel C. Sobral" Date: Wed, 2 Aug 2000 23:13:05 +0400 (MSD) Cc: chris@calldei.com, freebsd-hackers@FreeBSD.ORG From: .@babolo.ru MIME-Version: 1.0 Content-Type: text/plain; charset=KOI8-R Content-Transfer-Encoding: 8bit Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG > Chris Costello wrote: > > On Wednesday, August 02, 2000, .@babolo.ru wrote: > > > Sorry I have no other direction to ask this: > > > I have declaration: > > > > > > u_int32_t *(tcpcash_addr[256]); > > > > > > and want malloc some memory for tcpcash_addr: > > > > > > tcpcash_addr = (typeof(tcpcash_addr))malloc(u_int32_t * 256 * n); > > > > > > and have an error: > > > > > > ra-sum.c:386: cast specifies array type > > > *** Error code 1 > > > > Try this: > > > > u_int32_t *tcphash_haddr; > > tcphash_addr = malloc(sizeof(*tcphash_addr) * 256); > > > > You shouldn't be casting malloc and I don't see any reason > > to use *(foo[nmemb]) syntax. > > u_int32_t **tcphash_haddr; > > From the code, it's a variable-sized array of fixed-sized arrays. 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? And more, in the original code not malloc but mmap... tcpcash_addr = (typeof(tcpcash_addr))mmap( NULL, ... so using of pointers is dirty enougth. and more, assign without a cast impossible: ra-sum.c:385: incompatible types in assignment And even u_int32_t *(tcpcash_addr[256]); tcpcash_addr = (void*)mmap( NULL, ... does not translated -- Александр А. Бабайлов mailto://babolo@links.ru/ http://links.ru/ To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message