Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: Uncle Steve Newsgroups: comp.lang.c Subject: Re: Malloc Query Date: Sat, 21 May 2011 11:13:12 -0400 Organization: Friends of the Galactic Collective Lines: 70 Message-ID: References: <87aael3xw1.fsf@blp.benpfaff.org> <0.9e954b297cf63735a997.20110518193428BST.87zkmjuabv.fsf@bsb.me.uk> <0.3ac369f64d11bcd06d18.20110519002615BST.87ei3vtwtk.fsf@bsb.me.uk> <98a074c87051012-452fb@gmail.com> <0.bf5e9ff46dfbfc1e78d6.20110519032218BST.87tycrsa3p.fsf@bsb.me.uk> <93omefF1bbU15@mid.individual.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 8bit Injection-Info: mx04.eternal-september.org; posting-host="job9yP/rmK/fxQbJrcqEcA"; logging-data="7332"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/+J2Qc7GaFyS+S8iLVCeTv" User-Agent: nn 6.7.3 Content-Length: 1760 Cancel-Lock: sha1:bITg1Zyi5WPfzSzb/R0fflDlA6o= Xref: x330-a1.tempe.blueboxinc.net comp.lang.c:4494 On Sat, May 21, 2011 at 02:30:39PM +1200, Ian Collins wrote: > On 05/20/11 10:34 AM, Uncle Steve wrote: > > A couple of comments on the code: > > > > >arena_head * arena_creat(size_t s, int n) > >{ > > arena_head * h; > > int i; > > > > h = malloc(sizeof(arena_head)); > > if(h == NULL) { > > perror("malloc()"); > > exit(1); > > } > > > > h->obj_size = s; > > h->arena_size = n; > > h->free = n; > > > > h->arena = malloc(s * n); > > if(h->arena == NULL) { > > perror("malloc()"); > > exit(1); > > } > > > > h->free_list = 0; > > > > for(i = 0; i< n; i++) > > *((int *) arena_obj_addr(h, i)) = i + 1; > > > > *((int *) arena_obj_addr(h, i)) = -1; > > This writes past the end of the allocated block. Ha, you're right. The original code this is based on doesn't have that problem. The for(;;) comparison should be i < (n - 1). Since I wrote it in less than an hour, there ought to be several good bugs in there. > > return(h); > >} > > > > > >typedef unsigned long long int T64; > > > >#define processcputime(p) > >\ > > Why go the the trouble of declaring this as a messy macro? I'm not all that interested in nanosecond resolution for timing things, so I scaled the resolution down to microseconds and turned it into a 64-bit quantity so arithmetic calculations would be more convenient. I suppose it could be a function call, but inline it probably generates ten or so instructions. Insulating the actual system function call also makes porting a little easier. Regards, Uncle Steve -- Should a professional politician be charged with molestation if he kisses babies while attending political rallies?