Sign in Join

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

Mailing lists / fpc-devel / 2009-11

Comparing arrays of char

9 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

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

Carsten Bager

Here is output from my program p compiled with Delphi (5) and FPC. I believe that FPC fails because the first byte is chr(0) when comparing 70501033728 with 0.
I did run into this problem when moving code from a Pascal compiler that can compare records of the same type.
In Delphi I can come around this problem creating an array of char in the record of the same size and then comparing those.
But it looks like this doesn´t work in FPC.

Is this an error in FPC, or dos the compiler always look at an array of char as a null terminated string.

Carsten

OK:70501033728/0
OK:70501033729/0
OK:70501033727/0

Error:70501033728/0
OK:70501033729/0
OK:70501033727/0

Free Pascal Compiler version 2.2.4 [2009/09/01] for arm Copyright (c) 1993-2008 by Florian Klaempfl
Target OS: Linux for ARM
Compiling p.pp

Program p;
type
  t_typ=packed array[0..4] of char;
  b_typ=packed array[0..4] of byte;
  t2=record
    case integer of
      0:(t:t_typ);
      1:(b:b_typ);
      2:(i:Int64);
    end;
Var
b1,b2:t2;
Begin
  b2.i:=0;
  b1.i:=70501033728;
  if b1.t=b2.t then
    writeln('Error:',b1.i,'/',b2.i)
  else
    writeln('OK:',b1.i,'/',b2.i);
  b2.i:=0;
  b1.i:=70501033729;
  if b1.t=b2.t then
    writeln('Error:',b1.i,'/',b2.i)
  else
    writeln('OK:',b1.i,'/',b2.i);
  b1.i:=70501033727;
  if b1.t=b2.t then
    writeln('Error:',b1.i,'/',b2.i)
  else
    writeln('OK:',b1.i,'/',b2.i);
End.
------------------------------------

Error:70501033728/0
OK70501033729/0
OK70501033727/0
Med venlig hilsen
Carsten Bager

BEAS A/S
Brørupvænget 10
DK-7650 Bøvlingbjerg
Tlf. : +45 9788 5222 Fax : +45 9788 5434
www.beas.dk

Reply by email

Marco van de Voort
to Carsten Bager

In our previous episode, Carsten Bager said:

Here is output from my program p compiled with Delphi (5) and FPC. I believe that FPC fails because the first byte is chr(0) when comparing 70501033728 with 0.

Did you also try this on a PC? I haven't really tested, but it seems that you are converting between integers and arrays making use of their memory storage.

The memory storage format differs between x86 and (typical) arm. The former is little endian, the latter mostly bigendian.

Google/wikipedia those terms to get an overview, e.g.

http://en.wikipedia.org/wiki/Endianness

Reply by email

Carsten Bager
to Marco van de Voort
Show quoted text 11 lines
In our previous episode, Carsten Bager said: > Here is output from my program p compiled with Delphi (5) and FPC. > I believe that FPC fails because the first byte is chr(0) when comparing 70501033728 with > 0. Did you also try this on a PC? I haven't really tested, but it seems that you are converting between integers and arrays making use of their memory storage. The memory storage format differs between x86 and (typical) arm. The former is little endian, the latter mostly bigendian.

It is the same on a PC

As far as I can see, the problem is the compiler does not differ between an array of char and a null terminated string.
As a result of this, if the first byte/char are 0 in both arrays the compiler stops checking and says that there are equal.

Carsten

Carsten

Reply by email

Jonas Maebe
to Carsten Bager

Carsten Bager wrote on Mon, 02 Nov 2009:

As far as I can see, the problem is the compiler does not differ between an array of char and a null terminated string. As a result of this, if the first byte/char are 0 in both arrays the compiler stops checking and says that there are equal.

When I implemented the current behaviour, I compared it either Turbo Pascal or Kylix 3 (which is Delphi 6.5), I'm not sure which one anymore. There,
a) if the array is zero-based (i.e., lower bound is 0), then the array of char is treated as a null-terminated string
b) if the array is non-zero-based, then the array of char is always checked completely

That should also be the current behaviour for FPC. So if you want to always use the full array, change your lower array bounds.

Jonas

---------------------------------------------------------------- This message was sent using IMP, the Internet Messaging Program.

Reply by email

Michael Schnell
to Jonas Maebe
Jonas Maebe wrote
a) if the array is zero-based (i.e., lower bound is 0), then the array of char is treated as a null-terminated string b) if the array is non-zero-based, then the array of char is always checked completely

The Delphi behavior sounds not very obvious :(.

Is this true as well for arrays of char as for arrays of byte ?

-Michael

Reply by email

Jonas Maebe
to Michael Schnell

Michael Schnell wrote on Mon, 02 Nov 2009:

Show quoted text 7 lines
Jonas Maebe wrote: > a) if the array is zero-based (i.e., lower bound is 0), then the array > of char is treated as a null-terminated string > b) if the array is non-zero-based, then the array of char is always > checked completely The Delphi behavior sounds not very obvious :(.

I guess it's because C-arrays are also always 0-based (and in C a char array is the closest thing there is to a string). But yes, it also cost me quite a bit of testing before I finally figured out the rules.

Is this true as well for arrays of char as for arrays of byte ?

You cannot compare an array of byte with another array using the regular comparison operators, afaik.

Jonas

---------------------------------------------------------------- This message was sent using IMP, the Internet Messaging Program.

Reply by email