Forum > Beginners

Counter changed after accessed

(1/1)

sunTan:
Hi,

I wrote earlier (Compilation error??) and I had issue removing the include files. My problem was counter altered from 0 after accessing the array.

            newcat := cbxCategory.Text ;
            catDB[catCnt] := newcat ;
            catCnt := catCnt + 1 ;
            frmMyDB.StatusBar.Panels.Items[0].Text := 'Category ' + newcat + ' saved.' ;

newcat was included for debugging purpose. If you breakpoint in TfrmMyDB.cbxCategoryKeyUp, catCnt = 0, on first encounter. After executing carDB[catCnt], catCnt value is changed from 0 to >17000. As a result, the system will break on error when catCnt is used.

Included is the bare minimum of the code.

Thanking you in advance... sunTan

rsz:
Hi, you are corrupting memory. Take a look, catCnt is set to 0 but the array catDB starts at 1 and you set catDB[0].


--- 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";}};} ---var  newcat  : string[16] ;  // new category string  catCnt  : Integer;  catDB   : array[1..1024] of String[16] ; implementation procedure TfrmMyDB.FormCreate(Sender: TObject);begin  //...  catCnt := 0 ;      // reset total category count  //...end; procedure TfrmMyDB.cbxCategoryKeyUp(Sender: TObject; var Key: Word;  Shift: TShiftState);begin  //...  catDB[catCnt] := newcat ;  //...end;
I recommend using Low and High wherever possible:
https://www.freepascal.org/docs-html/rtl/system/low.html
https://www.freepascal.org/docs-html/rtl/system/high.html

It is also advisable to create the default debug and release build modes and to build and run with the debug mode because checks and assertions (range check) would have caught this. In this case it throws an exception on line 700 where you assign out of range.
https://wiki.freepascal.org/IDE_Window:_Compiler_Options#Adding_a_release_and_debug_build_modes

Navigation

[0] Message Index

Go to full version