----- Original Message -----
From: "Ken Williams" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Sunday, September 01, 2002 12:49 AM
Subject: Patch for Inline::C-Cookbook


> Hi,
>
> The C-Cookbook has the following code for doing object-oriented Inline:
>
>      SV* new(char* class, char* name, char* rank, long serial) {
>          Soldier* soldier = malloc(sizeof(Soldier));
>          SV*      obj_ref = newSViv(0);
>          SV*      obj = newSVrv(obj_ref, class);
>
>          soldier->name = strdup(name);
>          soldier->rank = strdup(rank);
>          soldier->serial = serial;
>
>          sv_setiv(obj, (IV)soldier);
>          SvREADONLY_on(obj);
>          return obj_ref;
>      }
>
> I've found that the following simpler code works just as well:
>
>      SV* new(char* class, char* name, char* rank, long serial) {
>        Soldier* soldier = malloc(sizeof(Soldier));
>
>        soldier->name = strdup(name);
>        soldier->rank = strdup(rank);
>        soldier->serial = serial;
>
>        return sv_setref_pv(newSViv(0), class, (void *)soldier);
>      }
>
> Any reason not to use that?
>

Only time I used 'malloc()' and 'free()' in an inline function I struck
problems if the function was called a number of times. I forget just how big
that 'number of times' was - and the amount of memory involved might well
have had a bearing. (It worked fine if the function was called only once -
which led me to believe that 'free()' was not doing the job, and the memory
was being freed only when the app terminated.)

Someone posted the recommendation that 'New()' and 'Safefree()' be used
instead, and I've been using them with no trouble.

Doing a benchmark on a thousand or so iterations might bring any problem to
light.
Or was my problem just a M$ issue .... or are there some other 'ifs and
buts' involved that render this post inapplicable ?

Anyway ... just thought I'd mention it :-)

Cheers,
Rob



Reply via email to