>
> not to stick my nose in, but what convention makes this a "usually" ?
It's okay to stick your nose in, I do it all the time.
>
> i'm not a 'C' programmer by any stretch, but i don't _think_ functions
> that have a return type of "char*" are that unusual are they ?
Are we talking about DLL's or regular functions in your every day code? I'm
talking
about DLL's here. It is rare to see a pchar returned as a function result in
DLL's,
yes.
Well first thing that comes to mind here, is that we are discussing Pascal
here, not
C.. and Pascal programmers make extensive use of strings but C programmers
don't (C++
maybe).
If you do this...
function DoSomething: PChar;
var
mystr: String;
begin
mystr := 'Give me a string, now';
Result := PChar(s1);
end;
...you're in trouble.
MyStr is allocated by the function, and freed at the end of the function. Pchar
is
invalid, randomly, depending on what day it is.
If you return a pchar(pointer) to a piece of stack, like a local variables,
memory
can be freed as soon as the function returns the result - this leads to random,
unpredictable behavior.
Other cautions:
* Windows API functions in windows.pas don't return LPSTR (PChar) values,
unless they
return an address to constant or static text
* Examples of pchar function result are hard to find. Studying how parameters
are
passed, on the other hand, between DLL's, are easy to find.
* Future life of your source code: if you upgrade your source code 6 months
later,
you will not necessarily remember the problems of returning a pchar. Add a
string to
your function while upgrading your source code, and you may end up passing to
invalid
memory because you forgot about the function result drawbacks. Everyone
upgrades or
updates their code at some point, but we all forget what traps we had waiting to
happen - so prepare for that.
* how can you allocate memory for a function result, on the calling side, and
then
free it on the calling side too? The function result is not a parameter that
can be
controlled by you, on the calling side. A parameter can be allocated precisely,
controlled precisely, freed precisely.
Now I don't have the original email - is he returning static or constant text?
It may
be safe. I forget now, what he was trying to do. Where is the memory being
allocated
for the Pchar, again?
--
L505
_________________________________________________________________
To unsubscribe: mail [EMAIL PROTECTED] with
"unsubscribe" as the Subject
archives at http://www.lazarus.freepascal.org/mailarchives