Sign in Join

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

Mailing lists / fpc-pascal / 2011-09

2 bugs ???

5 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

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

Yann Bat

Hi,

I am trying to learn freepascal generics and I think that I have found 2 bugs.

Tested with FPC 2.4.4 Linux x86

==================
= BUG 1 - with SizeOf =
==================
program SizeOfBug;
{$mode objfpc}{$H+}

type
  generic TGen<_T> = class(TObject)
    private
      FField: _T;
    public
      constructor Create(Val: _T);
      function Bug: Integer;
  end;
{--- TGen.Create ---}
constructor TGen.Create(Val: _T);
begin
  inherited Create;
  FField := Val;
end;
{--- TGen.Bug ---}
function TGen.Bug : Integer;
begin
  Result := 100000 div SizeOf(_T);  // *** DIVISION BY ZERO ***
  // THE FOLLOWING CODE IS OK !
  //
  // var
  //   S: Integer;
  // begin
  //   S := SizeOf(_T);
  //   Result := 100000 div S;
end;
type
  TGenInt = specialize TGen<Integer>;
var
  V: TGenInt;
begin
  V.Create(589);
  WriteLn('V.Bug = ', V.Bug);
  V.Free;
end.

==================
= BUG 2 - with WriteLn =
==================

program WriteLnBug;
{$mode objfpc}{$H+}
type
  generic TGen<_T> = class(TObject)
    private
      FField: _T;
    public
      constructor Create(Val: _T);
      procedure Bug;
  end;
{--- TGen.Create ---}
constructor TGen.Create(Val: _T);
begin
  inherited Create;
  FField := Val;
end;
{--- TGen.Bug ---}
procedure TGen.Bug;
begin
  WriteLn('FField = ', FField);  // *** CAN'T READ OR WRITE VARIABLES
OF THIS TYPE ***
end;
type
  TGenInt = specialize TGen<Integer>;
var
  V: TGenInt;
begin
  V.Create(589);
  V.Bug;
  V.Free;
end.

Reply by email

Marco van de Voort
to Yann Bat

In our previous episode, Yann Bat said:

Show quoted text 6 lines
I am trying to learn freepascal generics and I think that I have found 2 bugs. var V: TGenInt; begin V.Create(589);
This is no pascal way of creating a class. Use v:=tgenint.create;

Reply by email

Yann Bat
to Marco van de Voort

Thanks. You've just found bug number 3 ! :-)

But #1 and #2 are alway here.

2011/9/7 Marco van de Voort :

Show quoted text 14 lines
In our previous episode, Yann Bat said: > I am trying to learn freepascal generics and I think that I have found 2 bugs. > var >   V: TGenInt; > begin >   V.Create(589); This is no pascal way of creating a class. Use v:=tgenint.create; _______________________________________________ fpc-pascal maillist  -   http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

Leonardo M. Ramé
to Yann Bat: I am trying to learn freepascal generics and I think that I have found 2 bugs. Tested…

From: Yann Bat

Show quoted text 26 lines
To: "" Sent: Wednesday, September 7, 2011 11:36 AM Subject: [fpc-pascal] 2 bugs ??? Hi, I am trying to learn freepascal generics and I think that I have found 2 bugs. Tested with FPC 2.4.4 Linux x86 ================== = BUG 1 - with SizeOf = ================== {--- TGen.Bug ---} function TGen.Bug : Integer; begin   Result := 100000 div SizeOf(_T);  // *** DIVISION BY ZERO ***   // THE FOLLOWING CODE IS OK !   //   // var   //   S: Integer;   // begin   //   S := SizeOf(_T);   //   Result := 100000 div S; end;

The error message is correct, because inside the scope of method  Bug, _T points to nil, and the size of nil is 0, so 10000 div 0 raises the error.

What about using Result := 1000000 div SizeOf(FField); ?

Leonardo.

Reply by email

Yann Bat
to Leonardo M. Ramé

2011/9/7 Leonardo M. Ramé :

Show quoted text 5 lines
From: Yann Bat The error message is correct, because inside the scope of method  Bug, _T points to nil, and the size of nil is 0, so 10000 div 0 raises the error.

I think that SizeOf(_T) should be unknown rather than 0. The compiler should not make any assumption about _T before specialization.

What about using Result := 1000000 div SizeOf(FField); ?

Same error with SizeOf(FField).

Show quoted text 6 lines
Leonardo. _______________________________________________ fpc-pascal maillist  -   http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email