Forum > General

Deleting elements from dynamic array causes crash when compiled for win 64bit.

(1/1)

acp693:
Hi all,

I have the following test code which deletes elements from dynamic arrays. It works perfectly when compiled for 32bit win, but crashes when compiled for 64bit win. Is it to do with the fact that integers are different sizes on 64bit machines? I tried replacing the integers with int16, but made no difference. setting a watch on arr, I notice that when compiled for 64bit, the array arr is completely empty after being passed to the delete procedure. Any ideas what might be wrong?

Thanks.



--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs;Type      TintArray = array of integer;type   { TForm1 }   TForm1 = class(TForm)    procedure FormShow(Sender: TObject);  private    { private declarations }  public    { public declarations }    procedure DeleteElementFromArrayInt(var A: Tintarray; const Index: integer);   end; var  Form1: TForm1;  arr: array of array of integer;implementation {$R *.lfm} procedure TForm1.FormShow(Sender: TObject);begin    setlength (arr , 3);     setlength (arr[0], 2);   //Build arr  with different lengths of second dimension    setlength (arr[1], 3);    setlength (arr[2], 2);     arr[0,0]:= 8;       //filling arrays    arr[0,1]:= 9;     arr[1,0]:= 5;    arr[1,1]:= 6;    arr[1,2]:= 7;     arr[2,0]:= 3;    arr[2,1]:= 4;    DeleteElementFromArrayInt(TintArray(arr),1);    // delete all elements of second column of arr     // show that all 3 elements of arr[1] are deleted and replaced by arr[2]    ShowMessage(IntToStr(arr[1,1]) + '  ' + IntToStr(arr[0,1]));end; procedure TForm1.DeleteElementFromArrayInt(var A: Tintarray; const Index: integer); var ALength: integer; TailElements: integer;begin ALength := Length(A); Assert(ALength > 0); Assert(Index < ALength); Finalize(A[Index]); TailElements := ALength - Index; if TailElements > 0 then    Move(A[Index + 1], A[Index], SizeOf(integer) * TailElements); Initialize(A[ALength - 1]); SetLength(A, ALength - 1);end;    end.                        

acp693:
Ok, I saw it almost straight away after I posted; Tintarray is only 1 dimension whereas arr has 2.

Still don't know why that would compile and work for 32bit and not 64bit.

Handoko:

--- Quote from: acp693 on December 01, 2021, 10:26:08 am ---Still don't know why that would compile and work for 32bit and not 64bit.

--- End quote ---

Coincidence, I guess. Try with different data, I don't think that still will work correctly on 32-bit.

Because, you're type casting a wrong data type in line #49, which causes memory problem.

By changing line #9 of your code to:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  TintArray = array of array of integer;
I got this:

acp693:
Yes, thankyou. :)

PascalDragon:

--- Quote from: acp693 on December 01, 2021, 10:26:08 am ---Still don't know why that would compile and work for 32bit and not 64bit.

--- End quote ---

As Handoko wrote: coincidence. If you have wrong code essentially all bets are off: it can work, it might not compile, it might raise an exception, it might cure world hunger or trigger an apocalypse. All possible...

Navigation

[0] Message Index

Go to full version