Oldham, Adam wrote:
I have another question. I have a veriable type declared as a CString passing into a pascal function with preceding it with VAR. I can modify the variable in the called function correctly, but after the fuction is complete, what should have been a modified CString is acutally not. I've read some FAQs and apparently the only known problems are when passing a CString in a special circumstance into a C Function.... here is some sample code, the output is in the comments:
PROGRAM Main;
PROCEDURE MakeVolName ( VAR VolName : CString ); VAR PasVolName : String[5];
BEGIN PasVolName := '7.12'; write("PasVolName in MakeVoldName ="); writeln(Pasvolname); { Prints 7.12 -- correct }
Volname := String2CString(PasVolName); write("VolName in MakeVolName = "); {$x+} writeln(VolName); { Prints 7.12 -- correct}
END;
VAR VolName2 : CString;
BEGIN VolName2 := 'test'; write("initially = "); writeln(VolName2); { Prints test -- correct}
MakeVolID(VolName2); write("ending = "); {$x+} writeln(VolName2); { Prints garbage -- not correct}
END.
OUTPUT: initially = test PasVolName in MakeVoldName =7.12 VolName in MakeVolName = 7.12 ending = `4øÿ¿ÿÿÿÿ
Any ideas? I am using gpc version 20010502, gcc 2.95.2 on Mandrake Linux 7.2 running kernel 2.2.17.
The problem is in using String2CString -- it allocates storage for the CString on the stack which becomes invalid after the return from the function. (I'd be willing to admit that this probably isn't documented anywhere, though ...)
You might want to use NewCString instead which allocate storage from the heap. Of course, you'll have to Dispose it sometime later to avoid memory leaks.
Frank