Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2009-03

PWideChar vs. ^

5 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Marc Santhoff

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);

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

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

TIA,

Marc Santhoff
Jonas Maebe
to Marc Santhoff
Marc Santhoff wrote 21 lines
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); ? Maybe this is special behaviour of the routine getting the variable, it is TTreeView.AddChild(ParentNode: TTreeNode; const s: string): TTreeNode

1) you're not saying what the type of "something" is
2) a pwidechar and a pwidestring are completely different things (a pwidestring points to a widestring, which i, turn is sort of like a reference-counted pwidechar; so typecasting a pwidestring to a pwidechar does not make sense)
3) you're not showing how the pwidestring was allocated and initialised
4) the "s" parameter in the procedure definition at the end is a string, while in the other code it's a pwidestring. It is also not clear to me what this routine has to do with any of the other code snippets that you posted

Please always include a compilable sample program when asking questions about code behaviour. Without it, all that can be done is hand waving. It may be more work for you, but it saves a lot of time asking questions after which that work has to be done anyway.

Jonas

Marc Santhoff
to Jonas Maebe
Jonas Maebe wrote
Please always include a compilable sample program when asking questions about code behaviour. Without it, all that can be done is hand waving.

I was more interested in some general guidelines about using (pointers to) widestrings and conversion to ansistring eventually. The details are visible from the code, but bringing the different parts of my data classes and a lcl-treeview in harmony with wide strings handed in (from fpc-xml, but that could be anything else) costs me much time currently.

Reading the installed documentation (pdf) of fpc 2.2.2 doesn't enlight me so far, no mention of unit "cwstring" at all. Only some conversion routines I cannot relate to what I'm doing.

I'll see if the Wiki can help me out,
Thank you.

Marc Santhoff

"Jeder Mensch hat das Recht auf schlechte Laune. Man sollte das in die Verfassung aufnehmen." (Georges Simenon)
Jonas Maebe
to Marc Santhoff
Marc Santhoff wrote 8 lines
Am Freitag, den 13.03.2009, 18:14 +0100 schrieb Jonas Maebe: > > Please always include a compilable sample program when asking > questions about code behaviour. Without it, all that can be done is > hand waving. I was more interested in some general guidelines about using (pointers to) widestrings and conversion to ansistring eventually.

There are none, at least not regarding "don't do this or your program will crash with an access violation" (well, typecasting a pwidestring to a pwidechar can cause crashes since they are unrelated types, but as far as I understood you did not get a crash in that case -- you won't get correct results either in the long run, though).

Of course, assigning a widestring to an ansistring always risks data loss due to the fact that the ansistring may not be able to represent all characters in the widestring, but that's it.

Show quoted text 6 lines
The details are visible from the code, but bringing the different parts of my data classes and a lcl-treeview in harmony with wide strings handed in (from fpc-xml, but that could be anything else) costs me much time currently.

Without a sample that demonstrates the problem you are seeing, I cannot help.

Reading the installed documentation (pdf) of fpc 2.2.2 doesn't enlight me so far, no mention of unit "cwstring" at all. Only some conversion routines I cannot relate to what I'm doing.

It's all internal compiler magic, which is not documented anywhere.

Jonas

Marc Weustink
to Marc Santhoff: maybe this is a dumb question, but: If I have a variable declared as var s: PWideString;…
Marc Santhoff wrote 18 lines
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