* * *

Author Topic: Wrong String index  (Read 1890 times)

tandatcr2000pro

  • Newbie
  • Posts: 4
Wrong String index
« on: August 29, 2018, 08:00:37 am »
Today, I ran into a problem when running my old code. These code worked correctly before but it doesn't now. I debugged and receive this result (image). It looks like that string index starts at 0. Is it a bug or a new feature or I've done something wrong?

howardpc

  • Hero Member
  • *****
  • Posts: 2780
Re: Wrong String index
« Reply #1 on: August 29, 2018, 10:05:48 am »
The difference in capitalisation may indicate that Str[] and str are different entities.
However, gdb does not always preserve or respect case. I think it depends, among other things, on whether your debug information is stabs or dwarf.
However that may be, why should the Watch window show two different spellings, if it does not detect two different variables?

Zvoni

  • Jr. Member
  • **
  • Posts: 59
Re: Wrong String index
« Reply #2 on: August 29, 2018, 10:19:56 am »
People call me crazy because i'm jumping out of perfectly fine airplanes.
I say you're crazy not to!
--------------------------------------------------------------------------------------------------
For health reasons i try to avoid reading unformatted Code

Handoko

  • Hero Member
  • *****
  • Posts: 2609
  • My goal: build my own game engine using Lazarus
Re: Wrong String index
« Reply #3 on: August 29, 2018, 10:44:24 am »
The images below are my test results on Linux.

Can anyone tell me it shows PChar is not the same as String in the Watch List?

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 6540
Re: Wrong String index
« Reply #4 on: August 29, 2018, 10:49:14 am »
debugger interprets as shortstring?

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 4810
    • wiki
Re: Wrong String index
« Reply #5 on: August 29, 2018, 12:06:51 pm »
http://wiki.lazarus.freepascal.org/GDB_Debugger_Tips#Strings

PChar starts at 0, String starts at 1.

The Debug info does not contain any info what the low index is. Afaik that is a deficiency in the dwarf 2 standard. (not sure).
It may be that this is fixed in the dwarf3 standard (but the IDE is likely to still show both results)

Be aware that dwarf3 may cause gdb to crash (depends on your project).

Handoko

  • Hero Member
  • *****
  • Posts: 2609
  • My goal: build my own game engine using Lazarus
Re: Wrong String index
« Reply #6 on: August 29, 2018, 01:05:49 pm »
http://wiki.lazarus.freepascal.org/GDB_Debugger_Tips#Strings

PChar starts at 0, String starts at 1.

I rarely use PChar, I don't know about it.
Thank you.

lucamar

  • Full Member
  • ***
  • Posts: 240
Re: Wrong String index
« Reply #7 on: August 29, 2018, 03:06:08 pm »
I rarely use PChar, I don't know about it.

It may help to think of sv: PChar as if it were sv: array of char;, the difference being that PChar is explicitely a pointer to char. AFAIK, that is how both types (char* and char[]) are treated in C.
Been there, done that ... barely kept the timelines.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 1.8.4/fpc 3.0.4 - Ubuntu 10, Kubuntu 14, Windows XP SP3 (Home & Prof.) and various DOS incarnations.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 4810
    • wiki
Re: Wrong String index
« Reply #8 on: August 29, 2018, 05:41:27 pm »
Well PChar is ^Char.  A pointer to a single char. And it behaves the same as ^Byte, ^Integer, ^TFoo

As all typed pointers, you can add/subtract. Adding one adds the size of one element. That is PInterger+1 points to the next integer.

And as all pointers, instead of dereferencing p^, you can use an index p[0].

lucamar

  • Full Member
  • ***
  • Posts: 240
Re: Wrong String index
« Reply #9 on: August 29, 2018, 06:03:25 pm »
Well PChar is ^Char.  A pointer to a single char. And it behaves the same as ^Byte, ^Integer, ^TFoo
[...]
And as all pointers, instead of dereferencing p^, you can use an index p[0].

Yeah, I was (as always :) ) simplyfing a lot; but your last point is where I was driving to: that you may think (with caveats) of PChar, in its most common uses, as if it were (kind of) a dynamic array of char. If just to wrap your mind around it ...

And you have to recognize that PChar, in its uses, is a little more ... special? than other common pointers.
« Last Edit: August 29, 2018, 06:08:09 pm by lucamar »
Been there, done that ... barely kept the timelines.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 1.8.4/fpc 3.0.4 - Ubuntu 10, Kubuntu 14, Windows XP SP3 (Home & Prof.) and various DOS incarnations.

Thaddy

  • Hero Member
  • *****
  • Posts: 6904
Re: Wrong String index
« Reply #10 on: August 29, 2018, 06:03:35 pm »
To make matters worse.. Delphi has an option to index strings from zero....(10.2)
Ada's daddy wrote this:"Fools are my theme, let satire be my song."

Thaddy

  • Hero Member
  • *****
  • Posts: 6904
Re: Wrong String index
« Reply #11 on: August 29, 2018, 06:10:27 pm »
Yeah, I was (as always :) ) simplyfing a lot; but your last point is where I was driing too: that you can think (with caveats) of PChar as if it were (kind of) a dynamic array of char. If just to wrap your mind around it ...
And y'all don't consider a PChar a Pascal string type, it isn't.
Code: Pascal  [Select]
  1. {$ifdef fpc}{$mode delphi}{$H+}{$endif}
  2. var
  3.  s:string ='test this is not pascal when cast to pchar '#0'me';
  4. begin
  5.   writeln(length(Pchar(s)),'-',PChar(s)); // you could have used strlen here, but that would reveal you don't know the language.
  6.   writeln(length(s),'-',s);
  7. end.

It is rather hard to stamp out this idiocy. <very grumpy indeed  >:D >:D >:D >:D >

A Pchar in Pascal is merely to interface with dumb languages like C and C++.
Never use it unless you need to interface with other languages. Or feel obliged to use an array of AnsiChar to make your life more difficult. ::)
« Last Edit: August 29, 2018, 06:17:28 pm by Thaddy »
Ada's daddy wrote this:"Fools are my theme, let satire be my song."

Handoko

  • Hero Member
  • *****
  • Posts: 2609
  • My goal: build my own game engine using Lazarus
Re: Wrong String index
« Reply #12 on: August 29, 2018, 06:34:40 pm »
A Pchar in Pascal is merely to interface with dumb languages like C and C++.
Never use it unless you need to interface with other languages.

I feel weird to use null terminated string, I'm convenient with shortstring and ansistring. I use PChar only on something like this:
Code: Pascal  [Select]
  1. Application.MessageBox(PChar(ErrorMessage), 'Error', MB_ICONSTOP);

Lets not talk about PChar. Can anyone answer TS' original question? Is it a debugger error? On my Linux, it the result of Str[i] is correct. But this is what TS got:

Code: Pascal  [Select]
  1. str := 'gia';
  2. i := 2;
  3. Str[i] is 'a';

Thaddy

  • Hero Member
  • *****
  • Posts: 6904
Re: Wrong String index
« Reply #13 on: August 29, 2018, 06:42:18 pm »
Handoko,
that IS focusing: the GNU debugger knows jack sh*t about Pascal strings. It expects an array of ansichar. That is something it can handle.
That has always been the case, so I wonder what happened before... I suspect a little diversion from the truth a.k.a. a catholic lie.
If you believe it worked before it becomes "it worked before". There are many examples on this forum that are proven otherwise.
« Last Edit: August 29, 2018, 06:44:47 pm by Thaddy »
Ada's daddy wrote this:"Fools are my theme, let satire be my song."

Handoko

  • Hero Member
  • *****
  • Posts: 2609
  • My goal: build my own game engine using Lazarus
Re: Wrong String index
« Reply #14 on: August 29, 2018, 06:49:39 pm »
On my Watch List, it showed 2 results. As PChar and String. But why the TS Watch List didn't show as mine? I guess TS was using Lazarus Windows version. Would it better, if on Windows version the Lazarus Watch List also shows the result as PChar and String?

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus