Marc Santhoff wrote:
Hi,

maybe this is a dumb question, but:

If I have a variable declared as

  var
    s: PWideString;

why is an exception (AV) thrown when using

    something := s^;

but not when using

    something := PWideChar(s);

?

You're mixing things up.

A Widestring is a dynamically allocated type which holds wide chars.

So when declaring

var
  W: Widestring;

you are under the hood in fact declaring a pointer to a dynamically allocated (referencecounted on non windows) struct holding the string.

What you declare is a pointer to this

var
  S: PWideString;

Like a variable of a PChar type this pointer doesn't contain anything but random noise, unless you initialized it.

When you assign
  something := s^;

you are dereferencing random data, which in most cases cause an AV. (even worse in this case since you assume this random data is a pointer to a widestring)


When you cast and assign
  something := PWideChar(s);

you treat this random pointer as a pointer to widechars. If you are lucky you are allowed to read this data and it is somewhere #0#0 termintated, so that is can be intrepreted and converted to a something.



Maybe this is special behaviour of the routine getting the variable, it
is

  TTreeView.AddChild(ParentNode: TTreeNode; const s: string): TTreeNode

I guess this is not the same S

Marc

_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply via email to