[fpc-pascal] PWideChar vs. ^
marc at dommelstein.net
Sat Mar 14 00:17:40 CET 2009
Marc Santhoff wrote:
> maybe this is a dumb question, but:
> If I have a variable declared as
> 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
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
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
> TTreeView.AddChild(ParentNode: TTreeNode; const s: string): TTreeNode
I guess this is not the same S
More information about the fpc-pascal