----- Original Message ----- From: "Ken Williams" <[EMAIL PROTECTED]> To: "Sisyphus" <[EMAIL PROTECTED]> Cc: "Miah Gregory" <[EMAIL PROTECTED]>; <[EMAIL PROTECTED]> Sent: Wednesday, January 29, 2003 5:01 PM Subject: Re: Passing scalar data around
> > On Tuesday, January 28, 2003, at 09:02 PM, Sisyphus wrote: > > For dynamic memory allocation use New(), Newz(), or Newc() - and > > Safefree() > > instead of free(). > > Btw, there are many perfectly functional modules around with XS code > > containing 'malloc' and 'free'. I'm not sure precisely under which > > circumstances it becomes an issue. > > I think that the reason those modules usually work fine is that under > most circumstances, New() and the like are just #define'd > straightforwardly in terms of malloc() and the like. But if someone > builds with perl's malloc() instead of the system's, there may be > problems. > Thanks Ken. I think that means that the advice to use New() instead of malloc() applies equally to both Inline and XS. Or does it apply to one more than the other ? Incidentally, I've never had any trouble with malloc(), though, having learnt of New(), I've hardly ever used it. I have struck trouble with calloc(), which I similarly no longer use. Here's a little demo script that produces that problem. (I had been wanting to re-create this to be sure that the problem with calloc() really does exist for me.) --------------------------------------------- use Inline C => <<'EOC'; void calloc_fun() { long *buffer; buffer = (long *)calloc( 400, sizeof( long ) ); if( buffer != NULL ) printf( "Allocated 400 long integers\n" ); else printf( "Can't allocate memory\n" ); free( buffer ); } void malloc_fun() { long *buffer; buffer = malloc( 400 ); if( buffer != NULL ) printf( "Allocated 400 long integers\n" ); else printf( "Can't allocate memory\n" ); free( buffer ); } EOC for(1..1000) {malloc_fun()} # No problem print "\n\n"; for(1..100) {calloc_fun()} # Crashes after 22 iterations. --------------------------------------------------- The C code is taken from the MSDN website demos. I tried rewriting the calloc() call as: buffer = calloc( 400, sizeof( long ) ); It made no difference. Anyway, no big deal ..... I just find it curious. Cheers, Rob