Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use string[1] rather than string while using readbuffer

I am having a record like this

  TEmf_SrectchDIBits = packed record
    rEMF_STRETCHDI_BITS: TEMRStretchDIBits;
    rBitmapInfo: TBitmapInfo;
    ImageSource: string;
  end;
  ---
  ---
  RecordData: TEmf_SrectchDIBits;

If i am reading data into it by using TStream like this an exception is occuring

SetLength(RecordData.ImageSource, pRecordSize);

EMFStream.ReadBuffer(RecordData.ImageSource,pRecordSize) 

But if i use below code, it was working normally

SetLength(RecordData.ImageSource, pRecordSize);

EMFStream.ReadBuffer(RecordData.ImageSource[1], pRecordSize);

So what is the difference between using String and String[1]

like image 509
Bharat Avatar asked Dec 03 '22 12:12

Bharat


1 Answers

The difference is a detail related to the signature of the .ReadBuffer method.

The signature is:

procedure ReadBuffer(var Buffer; Count: Longint);

As you can see, the Buffer parameter does not have a type. In this case, you're saying that you want access to the underlying variable.

However, a string is two parts, a pointer (the content of the variable) and the string (the variable points to this).

So, if ReadBuffer were given just the string variable, it would have 4 bytes to store data into, the string variable, and that would not work out too well since the string variable is supposed to hold a pointer, not just any random binary data. If ReadBuffer wrote more than 4 bytes, it would overwrite something else in memory with new data, a potentially disastrous action to do.

By passing the [1] character to a var parameter, you're giving ReadBuffer access to the data that the string variable points to, which is what you want. You want to change the string content after all.

Also, make sure you've set up the length of the string variable to be big enough to hold whatever you're reading into it.

Also, final note, one that I cannot verify. In older Delphi versions, a string variable contained 1-byte characters. In newer, I think they're two, due to unicode, so that code might not work as expected in newer versions of Delphi. You probably would like to use a byte array or heap memory instead.

like image 111
Lasse V. Karlsen Avatar answered Jan 05 '23 21:01

Lasse V. Karlsen