I've tried a for/while loop to empty the contents but have been told using
Mover() is more memory efficient though I cannot get it to work properly.
Thanks.
You have to be careful with Move, since the parameters are typeless.
This means strings and dynamic arrays get interpreted in a different
way than you would expect. Instead of the data, the pointer to it is
passed as the parameter. You have to pass to first element instead to
avoid this.
Assuming S is a string and A is a dynamic array of Byte:
SetLength(S, Length(A));
Move(A[0], S[1], Length(S) * SizeOf(S[1]));
> Assuming S is a string and A is a dynamic array of Byte:
>
> SetLength(S, Length(A));
> Move(A[0], S[1], Length(S) * SizeOf(S[1]));
Some instinct tells me to write
SetString(S, PChar(@A[0]), Length(A));
but of course I haven't tested it.
Danny
---
SetLength(TheString, Length(TheyArray));
Move(TheArray[0], TheString[1], Length(TheArray));
--
Rudy Velthuis [TeamB] http://rvelthuis.de/
"A lady came up to me on the street, pointed at my suede jacket and
said, "Don't you know a cow was murdered for that jacket?" I said
"I didn't know there were any witnesses. Now I'll have to kill you
too"." -- George Carlin
The instinct is wrong. Move requires references, not pointers. The above
would probably not compile (because of the cast, which is not an existing
variable). But if it would, it would only copy junk from the string
variable, and what is beyond that, in memory, to the PChar variable, and
what is beyond that. This would most certainly corrupt memory.
Avatar's syntax was the only correct one.
--
Rudy Velthuis [TeamB] http://rvelthuis.de/
"Anything that is too stupid to be spoken is sung."
- Voltaire (1694-1778)
> The instinct is wrong. Move requires references, not pointers.
Yes, thanks.
Danny
---
> You'd be surprised that it compiles and works as designed.
>
> > Avatar's syntax was the only correct one.
>
> With Move, yes, with SetString, no.
Oops, I missed the SetString part. Yes, SetString works that way.
--
Rudy Velthuis [TeamB] http://rvelthuis.de/
"A people that values its privileges above its principles soon loses
both."
- Dwight D. Eisenhower (1890-1969), Inaugural Address, January 20, 1953
> The instinct is wrong. Move requires references, not pointers.
But SetString doesn't.
> The above
> would probably not compile (because of the cast, which is not an existing
> variable). But if it would, it would only copy junk from the string
> variable, and what is beyond that, in memory, to the PChar variable, and
> what is beyond that. This would most certainly corrupt memory.
You'd be surprised that it compiles and works as designed.
> Avatar's syntax was the only correct one.
With Move, yes, with SetString, no.
Danny
---
>SetString(S, PChar(@A[0]), Length(A));
If A is nil this code will throw a range check error, because the reference
A[0] is evaluated in any case. Simply use:
SetString(S, Pointer(A), Length(A));
Mike
--
www.soft-gems.net
> If A is nil this code will throw a range check error, because the reference
> A[0] is evaluated in any case. Simply use:
>
> SetString(S, Pointer(A), Length(A));
I sometimes do, but often I am too lazy to check out if it is safe to
write the pointer cast and end up writing the less efficient code.
Danny
---