5

I am allocating the memory with GetMem (1028 bytes length), so I have an allocated Pointer.

Then I am reading the content and I know that there is e.g. 1028 bytes read. how can I cast pointer, or convert it to a string?

Should I null terminate the content of the memory prior to conversion?

Thanks!

5
  • What version of Delphi (unicode or ansi string)? From where the data comes, do you know beforehand that it is string or might there be other type of data as well? Commented Aug 30, 2011 at 13:39
  • Delphi-7. Data comes from tcp port. It is string. However even if there is some binary bytes it shouldn't be the problem for a string to hold them. Commented Aug 30, 2011 at 13:40
  • With binary data you cannot be sure there' not going to the a 0 value in there. Why not use code like var buffer: string; begin SetLength(Buffer, 1028); and then dump the stuff into that string. It will make it much easier to work with. You can access the individual bytes by using buffer[20]. For newer versions of Delphi you need to use ansistring of course. Commented Aug 30, 2011 at 13:43
  • Ok, let's assume it is a string. How to correctly cast it to a string? Commented Aug 30, 2011 at 13:46
  • In that case you can probably read the data directly into string variable, no need to go through intermediate buffer, ie var buf: AnsiString; SetLength(buf, 1028); read(buf[1]);. Key is to use buf[1] as the address of the buffer. Commented Aug 30, 2011 at 13:47

1 Answer 1

10

Use SetString. Pass it a string variable, your pointer, and the string length (1028). Delphi strings are implicitly null-terminated, so the function will add that automatically (even if your buffer already has null bytes in it).

Better yet, set the length of the string and read your data directly into it instead of using an intermediary buffer. If you must use an intermediary buffer, you may as well use one that's statically sized to 1028 bytes instead of complicating your program with dynamic memory management.

Sign up to request clarification or add additional context in comments.

3 Comments

SetString makes a copy of the content to a new place on the heap, right?
Yes. It certainly doesn't take ownership of the buffer, if that's what you're getting at. It has no way of knowing how an arbitrary buffer was allocated, so there's no way it could incorporate it into the requisite internal string data structures. A separate copy is therefore required.
Note that calling SetLength() over an existing string will re-allocate the string, i.e. move its old content to the new location, even if you'll not use this old content. So in practice, you should better use SetString(), even with nil as the memory buffer, or assign the previous string to '' before calling SetLength().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.