Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2012-10

using macros

7 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Unknown author

I need to translate this: #define mymalloc(x,y) (x*)malloc((y)* sizeof(x))

Is it possible using macro's in FPC or do I need to convert it to a procedure? How would I cast the pointer in the latter case (without having to change the sourcecode significantly)?

Regards,
Darius

Lukasz Sokol
to Unknown author
Show quoted text 8 lines
I need to translate this: #define mymalloc(x,y) (x*)malloc((y)* sizeof(x)) Is it possible using macro's in FPC or do I need to convert it to a procedure? How would I cast the pointer in the latter case (without having to change the sourcecode significantly)?

Not that I claim to /know/, but,
Is the purpose of this macro, to substitute

(something) = mymalloc(UInt, 5)
with
(something) = (Uint*)malloc(5*sizeof(UInt))

in source code (before compiling?) This means (something) is an array?

I'd definitively make it a function.

(haven't we got dynamic arrays for something like that ?)

Regards, Darius

L.

leledumbo
to Lukasz Sokol

If you want to port the code to clean Pascal, I suggest using related language feature, e.g. dynamic array. You can't really port that macro (even to a procedure / function), as the first parameter expected is a type name. If malloc returns Pointer, it doesn't need to be casted, the value is compatible with any pointer type.

This means (something) is an array?

pointer, despite it can be treated as array.

View this message in context: http://free-pascal-general.1045716.n5.nabble.com/using-macros-tp5711420p5711422.html Sent from the Free Pascal - General mailing list .
Unknown author
to Unknown author: I need to translate this: #define mymalloc(x,y) (x*)malloc((y)* sizeof(x)) Is it possible…
I need to

translate this: #define mymalloc(x,y) (x*)malloc((y)* sizeof(x))

Is it possible using macro's in FPC or do I need to convert it to a procedure? How would I cast the pointer in the latter case (without having to change the sourcecode significantly)?

Regards,

Darius

Thanks all, I decided to use an inline function and not to cast the return pointer. However, I've got another interesting one. Does someone have an idea how I could implement this?

#define MYPRINT(var1)
printf(# var1 ":%dn", var1)

Calling the macro as such :
MYPRINT(sizeof(int));

will yield: sizeof(int):4, which is useful for
debugging.

Will I need RTTI for this? I never used it before so I might be wrong.

Darius

Marco van de Voort
to Unknown author: I need to translate this: #define mymalloc(x,y) (x*)malloc((y)* sizeof(x)) Is it possible…

In our previous episode, 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.

leledumbo
to Unknown author: translate this: #define mymalloc(x,y) (x*)malloc((y)* sizeof(x)) Is it possible using…

Those macros are simply to reduce typing, better use true debugger. Even if RTTI can (which I believe not), it would be an abuse of the feature.

Macros in FPC aren't as flexible as in C for a purpose IMHO: it's often abused as the way you show.

View this message in context: http://free-pascal-general.1045716.n5.nabble.com/using-macros-tp5711420p5711433.html Sent from the Free Pascal - General mailing list .
Marco van de Voort
to Marco van de Voort: In our previous episode, said: A bit late, but: Impossible. 1. FPC macros can't be…

In our previous episode, Marco van de Voort said:

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.

Btw, Modula2 allows to pass any type of defined size to an ARRAY OF BYTE open array (and thus such functions also get the size). Its one of those things I missed going back to Pascal.