Re:Long Strings and the Memory (was: @LStrAsg)
On Wed, 19 Feb 1997 17:03:08 GMT, lester.ko...@cognos.com (Lester
Quote
Kovac) wrote:
>p is declared as a pointer.
> {$IFOPT D+} Trace('Test 4'); {$ENDIF}
> GetMem(p, SizeOf(string));
> {$IFOPT D+} Trace('Test 5'); {$ENDIF}
> String(p^) := ComboBox1.Text;
> {$IFOPT D+} Trace('Test 6'); {$ENDIF}
The assignment just after Test5 should GPF on every system.
A string is some very interesting type in Delphi. The main thing you
must know is that the length information is stored just in front of
the string and that a string always points to the beginning of the
string leading to negative offsets. This assignment should GPF on
every system as I think it modifies the contents of unassigned memory.
The Delphi memory system is however allocating memory in larger spaces
and you might come away with it.
The second problem is "SizeOf (String)" I have not tested the return
value, but it seems that it is not what you want. In fact it is a
pointer or it is of 2GB Size. I just don't know.
Better do it this way:
var
p : PChar;
begin
GetMem (p, length (Combo1.Text) + 1);
StrPCopy (p, Combo1.Text);
You can use a PChar just like a String in Delphi 2.
If necessary convert it using the StrPas function.
The better solution would be to declare a real string variable.
Quote
>She's getting Trace of 'Test 4' properly, then the memory is allocated
>(for long string). She's getting Trace of 'Test 5' properly, but She
>is not getting Trace of 'Test 6'. On the line String(p^) :=
>ComboBox1.Text; she gets thown exception EAccessViolation which is
>coming from the routine @LStrAsg . I'm confident, problem is not in
>Combobox1.Text but with the assignment.
>Does anybody experienced such problem ?
Yes, I did.
Quote
>How is it that only one user is getting it while all others
>are happy ?
The NT Random GPF-Generator is not working properly on the other
machines :-)
Hope this helps
Meikel Weber
Make sure you take a look at
http://www.europe.de/cebbs/weber
When replying via E-Mail strip the "-" in my E-Mail address!!