Recent

Author Topic: Setlength problem  (Read 2618 times)

acp693

  • Jr. Member
  • **
  • Posts: 73
Setlength problem
« on: September 09, 2020, 08:32:12 am »
Can anyone help me understand what I'm doing wrong here?  Thank you!


Code: Pascal  [Select][+][-]
  1.  
  2. setlength(somearray, someinteger); // this works
  3. setlength(anotherarray, trunc((someinteger-1)/2) ); // this doesn't work Error Type mismatch.
  4.  

440bx

  • Hero Member
  • *****
  • Posts: 6148
Re: Setlength problem
« Reply #1 on: September 09, 2020, 08:41:32 am »
I haven't tried what I'm going to suggest but, I believe that if you change the numeral 1 to 1.0 or the numeral 2 to 2.0 (in the second call to setlength), you should no longer get a type mismatch.
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

Handoko

  • Hero Member
  • *****
  • Posts: 5524
  • My goal: build my own game engine using Lazarus
Re: Setlength problem
« Reply #2 on: September 09, 2020, 08:48:21 am »
Tested on Lazarus 2.0.10, this works:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. Type
  3.   TMyArray = array of Byte;
  4. var
  5.   SomeArray, AnotherArray: TMyArray;
  6.   SomeInteger: Integer;
  7. begin
  8.   SomeInteger := 10;
  9.   SetLength(SomeArray, SomeInteger);
  10.   SetLength(AnotherArray, Trunc((SomeInteger-1)/2));
  11. end;

No compile time nor runtime error, just some hints.

acp693

  • Jr. Member
  • **
  • Posts: 73
Re: Setlength problem
« Reply #3 on: September 09, 2020, 08:49:48 am »
Thanks, I've just tried that, but still the same error.

acp693

  • Jr. Member
  • **
  • Posts: 73
Re: Setlength problem
« Reply #4 on: September 09, 2020, 08:50:54 am »
Tested on Lazarus 2.0.10, this works:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. Type
  3.   TMyArray = array of Byte;
  4. var
  5.   SomeArray, AnotherArray: TMyArray;
  6.   SomeInteger: Integer;
  7. begin
  8.   SomeInteger := 10;
  9.   SetLength(SomeArray, SomeInteger);
  10.   SetLength(AnotherArray, Trunc((SomeInteger-1)/2));
  11. end;

No compile time nor runtime error, just some hints.

Oh, I don't see any appreciable differences between your code and mine...

Handoko

  • Hero Member
  • *****
  • Posts: 5524
  • My goal: build my own game engine using Lazarus
Re: Setlength problem
« Reply #5 on: September 09, 2020, 08:54:13 am »
That's why it's usually good to show us the whole source code. Only 2 lines did not say anything useful.

Most inexperienced programmers usually think the problem is on a certain spot but actually it is elsewhere.
« Last Edit: September 09, 2020, 09:00:19 am by Handoko »

egsuh

  • Hero Member
  • *****
  • Posts: 1755
Re: Setlength problem
« Reply #6 on: September 09, 2020, 08:55:30 am »
I think it should work as Handoko said, but the definition on the document says they are defined as follows in system unit (and maybe others as well).

Code: Pascal  [Select][+][-]
  1. type Longint = - 2147483648..2147483647;
  2. type SizeInt = LongInt;
  3. type Int64 = - 9223372036854775808..9223372036854775807;
  4.  
  5. function Trunc(d: ValReal):Int64;
  6. procedure SetLength(var A: DynArrayType;  Len: SizeInt);

So the type mismatch may be between LongInt and Int64.

440bx

  • Hero Member
  • *****
  • Posts: 6148
Re: Setlength problem
« Reply #7 on: September 09, 2020, 09:01:37 am »
So the type mismatch may be between LongInt and Int64.
I don't know FPC internals well enough to affirmatively state the reason why FPC issues a type mismatch but, the second expression is using a real operator (the "/") to divide two integers and, that is in strict terms incorrect.  The "/" applies only to real types, it is for that reason that I suggested you change either the 1 to 1.0 or the 2 to 2.0.  That should cause the compiler to promote the integer types to reals causing the "/" operator to then be correct.  IOW, using "/" to divide integers isn't correct, integer division should be carried out with "div" not "/".


FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

Handoko

  • Hero Member
  • *****
  • Posts: 5524
  • My goal: build my own game engine using Lazarus
Re: Setlength problem
« Reply #8 on: September 09, 2020, 09:08:10 am »
No need to guess, without seeing more lines of the source we won't know what is the culprit.

But guessing is fun.
My guess: that array is a fix size array.

I don't have a magic ball but I have magic eyes.  ;)

bytebites

  • Hero Member
  • *****
  • Posts: 778
Re: Setlength problem
« Reply #9 on: September 09, 2020, 09:13:22 am »
So the type mismatch may be between LongInt and Int64.
I don't know FPC internals well enough to affirmatively state the reason why FPC issues a type mismatch but, the second expression is using a real operator (the "/") to divide two integers and, that is in strict terms incorrect.  The "/" applies only to real types, it is for that reason that I suggested you change either the 1 to 1.0 or the 2 to 2.0.  That should cause the compiler to promote the integer types to reals causing the "/" operator to then be correct.  IOW, using "/" to divide integers isn't correct, integer division should be carried out with "div" not "/".

Your explanation may apply to C-derived languages but not Pascal. In Pascal operands of /-division can be integers.

Thaddy

  • Hero Member
  • *****
  • Posts: 18783
  • To Europe: simply sell USA bonds: dollar collapses
Re: Setlength problem
« Reply #10 on: September 09, 2020, 09:24:59 am »
Code: Pascal  [Select][+][-]
  1. (SomeInteger-1) div 2;
Does not need trunc.
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

PascalDragon

  • Hero Member
  • *****
  • Posts: 6354
  • Compiler Developer
Re: Setlength problem
« Reply #11 on: September 09, 2020, 09:28:04 am »
Can anyone help me understand what I'm doing wrong here?  Thank you!

As others already wrote, please provide a full example.

So the type mismatch may be between LongInt and Int64.
I don't know FPC internals well enough to affirmatively state the reason why FPC issues a type mismatch but, the second expression is using a real operator (the "/") to divide two integers and, that is in strict terms incorrect.  The "/" applies only to real types, it is for that reason that I suggested you change either the 1 to 1.0 or the 2 to 2.0.  That should cause the compiler to promote the integer types to reals causing the "/" operator to then be correct.  IOW, using "/" to divide integers isn't correct, integer division should be carried out with "div" not "/".

FPC's /-operator takes both floating point and integer types. However it always results in a floating point type.

Code: Pascal  [Select][+][-]
  1. (SomeInteger-1) div 2;
Does not need trunc.

That would indeed be the cleanest solution, though I'd still be interested in the full code that fails just to make sure that this isn't a bug in the compiler...

440bx

  • Hero Member
  • *****
  • Posts: 6148
Re: Setlength problem
« Reply #12 on: September 09, 2020, 09:37:59 am »
Your explanation may apply to C-derived languages but not Pascal.
You may be right about that but, I am not sure.  I'd have to review what the Pascal standard says.

In Pascal operands of /-division can be integers.
_IF_ that is true then it introduces an inconsistency in the language because real_type div real_type is not allowed while, if what you are stating is correct, integer_type / integer_type is allowed.  That would be an undesirable inconsistency.

@OP,

Did you try my suggestion and, if so, did it eliminate the type mismatch message ?

ETA

FPC's /-operator takes both floating point and integer types. However it always results in a floating point type.
Thank you for the clarification.
« Last Edit: September 09, 2020, 09:39:57 am by 440bx »
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

PascalDragon

  • Hero Member
  • *****
  • Posts: 6354
  • Compiler Developer
Re: Setlength problem
« Reply #13 on: September 09, 2020, 09:43:06 am »
In Pascal operands of /-division can be integers.
_IF_ that is true then it introduces an inconsistency in the language because real_type div real_type is not allowed while, if what you are stating is correct, integer_type / integer_type is allowed.  That would be an undesirable inconsistency.

ISO Pascal 6.7.2.1, table 3: the division operator takes both real_type and integer_type as operands.

440bx

  • Hero Member
  • *****
  • Posts: 6148
Re: Setlength problem
« Reply #14 on: September 09, 2020, 10:02:00 am »
ISO Pascal 6.7.2.1, table 3: the division operator takes both real_type and integer_type as operands.
Thank you. 
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

 

TinyPortal © 2005-2018