passing type names in a C macro.

Oct 5, 2012 at 9:47pm
My C compiler is failing on the following macro (from the perl CGI-SpeedyCGI package) which takes a type name as an argument.

Here is the macro definition:

#define speedy_new(s,n,t) \
do { (s) = (t*)malloc((n)*sizeof(t)); } while (0)

Here is how it is being used:

static SpeedyScript *find_scr(SpeedyDevIno devino, int *is_new) {
SV *sv;
SpeedyScript *retval;

sv = find_devino(devino, scr_hash, 1)[0];

if ((*is_new = !SvOK(sv))) {
speedy_new(retval, 1, SpeedyScript);
retval->handler = NULL;
retval->last_cwd = NULL;
sv_setiv(sv, (IV) retval);
} else {
retval = (SpeedyScript *) SvIV(sv);
}

return retval;
}

The error I am getting is this:

speedy_perl.c: In function 'find_scr':
speedy_perl.c:258: error: expected expression before 'SpeedyScript'

The compiler is gcc 4.4.5.

Is this a legit macro? Any ideas on how I can workaround the compilation failure?

Thanks,

Dallas
Last edited on Oct 5, 2012 at 10:21pm
Oct 5, 2012 at 11:52pm
Not sure what the point of the do {...} while (0) is.
The code will execute once regardless of whether the do while is present or not.
Other than that, the macro looks fine.

It appears the compiler is complaining about the following line:
 
static SpeedyScript *find_scr(SpeedyDevIno devino, int *is_new) {


How is SpeedyScript declared?

Please use code tags (the <> formatting button) when posting code.


Oct 6, 2012 at 1:23am
Sorry dude... :( i have no any Idea right now.....
Topic archived. No new replies allowed.