[fpc-pascal] using macros
Marco van de Voort
marcov at stack.nl
Tue Oct 2 12:47:18 CEST 2012
In our previous episode, dhkblaszyk at zeelandnet.nl said:
A bit late, but:
> I need to translate this: #define mymalloc(x,y) (x*)malloc((y)*
> sizeof(x))
Impossible.
1. FPC macros can't be parameterized
2. less problematic, but macros don't import over unit borders,
you'd have to put the macro in an inc file and include it everyhwere.
The usual trick, converting to procedure is hard because
3. There is no way to pass something typeless to a function and then get
the size of it. This make emulating it with functions hard.
4. the generic part (returning a typed pointer) will be impossible,
requiring a cast.
> Is it possible using macro's in FPC or do I need to convert
> it to a procedure?
Neither is possible I think for the general case. The most sane way is to
make it a three parameter function and pass sizeof(x);
That will require two fixes though, every
z:=malloc(x,y)
will have to be changed to
z:=px(malloc(x,y,sizeof(x)));
> How would I cast the pointer in the latter case (without having to change
> the sourcecode significantly)?
You can't. Macros are illdefined, and thus hard to port or convert. GIGO
principle.
More information about the fpc-pascal
mailing list