Re: malloc during selectAll -> crash
Re: malloc during selectAll -> crash
- Subject: Re: malloc during selectAll -> crash
- From: John Stiles <email@hidden>
- Date: Fri, 12 May 2006 07:05:28 -0700
Jim Correia wrote:
On May 11, 2006, at 10:31 AM, Ryan Britton wrote:
On May 11, 2006, at 7:16 AM, Lorenzo wrote:
//////////////////////
NSMutableArray *items = [NSMutableArray array];
NSIndexSet *selectedRows = [self selectedRowIndexes];
unsigned int i, totItems = [selectedRows count];
if(totItems == 0) return items ;
unsigned int *buf = malloc(totItems);
You're allocating a buffer here of totItems bytes. An int is not a
single byte so you're bound to get a memory access error when you
attempt to access something outside your malloc'd memory. Try
replacing this with something like unsigned int *buf = (unsigned int
*) malloc(totlItems * sizeof(unsigned int));
And in fact, when allocating space for an array, calloc may be a
natural choice.
#include <stdlib.h>
void *
malloc(size_t size);
void *
calloc(size_t count, size_t size);
If you don't mind a smattering of ObjC++ then this is easily the
cleanest approach--no casts or sizeof needed:
unsigned int * buf = new unsigned int[totItems];
/// ... use it ...
delete [] buf; // don't forget the [] here!
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden