Subject Re: Re: [firebird-support] UDF:s and strings i Delphi
Author Pavel Menshchikov
Hello Kjell,

KR> So, if I do this:
>> -----
>> procedure myUDF(InStr: PChar; var OutStr: PChar); cdecl; export;
>> begin
>> // check if OutStr<>nil - then FB has allocated the memory
>> // processing
>> ...
>> end;
>> -----
>> DECLARE EXTERNAL FUNCTION myudffunc
>> CSTRING(10), CSTRING(10)
>> RETURNS PARAMETER 2
>> ENTRY_POINT 'myUDF' MODULE_NAME 'myudfmodule';
>> -----

KR> Will myUDF cause buffer overruns if it writes more than 10 characters +
KR> 0 terminator into the OutStr buffer?
I think it produces an error, but I am not sure. Try to test it
yourself...

KR> In that case, it would be tempting to declare with the maximum size
KR> which I think I read is 32767:
KR> -----
KR> DECLARE EXTERNAL FUNCTION myudffunc
KR> CSTRING(10), CSTRING(32767)
KR> RETURNS PARAMETER 2
KR> ENTRY_POINT 'myUDF' MODULE_NAME 'myudfmodule';
KR> -----
KR> But I assume this would impact performance since it has to allocate a
KR> lot more memory on each call. Right? Would this impact be big? I mean,
KR> the buffer would be released as soon as the value is returned to the
KR> client in a dataset or written into a field, woudln't it? So it wouldn't
KR> really waste more than one such buffer at a time. (Per concurrent query
KR> that uses that UDF, that is.)
As I remember in multi-row
SELECT <some UDF>,... FROM...
FB allocates the memory just once and reuses it for every UDF calling
- it may give you some preformance improvements as compared to in-UDF
memory allocation and FREE_IT. But I am not sure, so try to test it
yourself again :)


HTH
--
Best regards,
Pavel Menshchikov
http://www.ls-software.com