views:

133

answers:

2

Hi,

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]

+11  A: 

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.

Lasse V. Karlsen
@Lasse V. Karlsen: Thank you for your wonderful answer.
Bharat
+5  A: 

String types are implemented actually as pointers to something we could call a "string descriptor block". Basically, you have a level of indirection. That block contains some string control data (reference count, length, and in later versions character set info as well) at negative offsets, and the string characters at positive ones. A string variable is a pointer to the decription block (and if you print SizeOf(stringvar) you get 4), when you work on strings the compiler knows where to find the string data and handle them. But when using an untyped parameter (var Buffer;), the compiler does not know that, it will simply access the memory at "Buffer", but with a string variable that's the pointer to the string block, not the actual string characters. Using string[1] you pass the location of the first character data.

ldsandon