What is the difference between
this:
SourceString := 'I am doing just fine!';
MemoryStream.ReadBuffer(Pointer(SourceString)^, xxx);
(full source code available here: http://edn.embarcadero.com/article/26416)
and this code (mine):
SetLength(SourceString, xxx);
MemoryStream.ReadBuffer(SourceString[1], xxx);
- Do I really have to use Pointer(SourceString)^ or SourceString[1] is ok also?
- The code (both of them) will work with Delphi 2010 (unicode)?
1: The
SourceString[1]version is more readable. I prefer not to work with pointers when they aren’t completely necessary.2: This code will not work with Unicode. You’ll have to multiply it:
xxx * sizeof(Char). (This will work with both pre- and post-Unicode versions of Delphi.) But unless you’re making heavy use of non-Ansi chars, this will be a big waste of space. What I prefer to do is:(This is part of a
class helper for TStreamI wrote that makes it a lot easier to read and write various things to and from streams. But if you don’t like class helpers, it shouldn’t be too hard to adapt the basic idea to a different format.)