in reply to Re: XS malloc and free (whose)
in thread XS malloc and free

Not easy indeed. None of the following work:
#define d a #include <stdio.h> void a() { printf("a\n"); } void b() { printf("b\n"); } int main() { a(); #define a b a(); (*&a)(); /* \a(); */ #define c a c(); d(); return 0; }

Replies are listed 'Best First'.
Re^3: XS malloc and free (whose)
by tye (Sage) on Feb 07, 2006 at 20:17 UTC

    Of course not. (: The C preprocessor is mostly just text substitution and is defined to not care about the order in which things are defined.

    A more interesting case that also doesn't work is:

    #include <stdio.h> void flee(int i) { printf("flee(%d)\n",i); } void perlFlee(int i) { printf("perlFlee(%d)\n",i); } int main() { flee(1); #define flee perlFlee flee(2); #define _stitch(a,b) a ## b #define Free(x) _stitch(fl,ee)( x ) Free(3); return 0; }

    Which produces "perlFlee(3)" as its last line of output.

    And this tenancity of the C preprocessor is why (at least when the idea of XS came about), there should have been (similar to several other changes that were made for similar reasons) a global s/\bfree\b/Perl_free/, and s/\bmalloc\b/Perl_malloc/g done to the Perl source code so "#define free..." could be eliminated (replaced with "#define Perl_free free" for some systems, of course).

    - tye