Program Exits With Array Problem

New to FreeBASIC? Post your questions here.
Post Reply
Triopstor
Posts: 118
Joined: Apr 25, 2006 13:11

Program Exits With Array Problem

Post by Triopstor »

Hi! I have a program that Exits at this piece of code:

Code: Select all

SSpin1=1:S5=S5+SSpin1:Y1=SSpin1:S6=SSpin1:S7=S7+SSpin1:beep:print Y1,C1:sleep:FOR X2=1 TO Y1:YAxis2(X2)=C1:NEXT X2:beep:beep:beep
I'm using Sleep and Beep as a way to trace the problem. When I run the code I get 1 beep then a print of "1 1"(which is correct and then I press ENTER) and NO 3 beeps. It then EXITS. It never reaches the 3 beeps! Therefore the program running EXITS at the piece of code "YAxis2(X2)-C1". I don't get an error message. What could be wrong?

I defined YAxis() array as

Code: Select all

DIM SHARED AS LONGINT YAxis2(1 TO E3+LB1)
E3=221 and LB1=0

My program uses lots a variables and some of them are LONGINT defined. I got rid of one of the LONGINT variables to free up memory usage but still no good fortune. Is this the problem I'm running out of memory and the program can't assign YAxis2(X2)=C1? How can I tell and debug this?

EDIT UPDATE: The piece of code works with DIM SHARED AS LONGINT YAxis2(20) but I need the full array size of YAxis2(1 TO E3+LB1)! Edit Update I tried

Code: Select all

DIM SHARED AS LONGINT YAxis2(221)
and it works! No exiting prematurely! Is that the solution? I can't use DIM SHARED AS LONGINT YAxis2(1 TO E3+LB1) I think I solved my own problem use DIM SHARED AS LONGINT YAxis2(E3+LB1)! Still the compiler run didn't give me an error to YAxis2(1 TO E3+LB1).
fxm
Moderator
Posts: 12363
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Program Exits With Array Problem

Post by fxm »

What is the operating system?
Compile the program with the '-exx' option and check if a runtime error message appears on the console output.

[edit]
Unlike 'DIM SHARED AS LONGINT YAxis2(221)', 'DIM SHARED AS LONGINT YAxis2(1 TO E3+LB1)' defines a dynamic array (instead of static) with its data located on the heap (if 'E3' and 'LB1' are not both 2 constants).
Last edited by fxm on May 03, 2024 8:42, edited 2 times in total.
Reason: Updated (see [edit]).
paul doe
Moderator
Posts: 1818
Joined: Jul 25, 2017 17:22
Location: Argentina
Contact:

Re: Program Exits With Array Problem

Post by paul doe »

Most likely an array out of bounds error, yes. If I'm willing to bet, this line:

Code: Select all

DIM SHARED AS LONGINT YAxis2(1 TO E3+LB1)
Probably doesn't work as you'd expect (unless E3 and LB1 are constants)
caseih
Posts: 2166
Joined: Feb 26, 2007 5:32

Re: Program Exits With Array Problem

Post by caseih »

Triopstor wrote: May 02, 2024 23:28 Is that the solution?
Magic fixes like this are a probable sign you are experiencing memory corruption (out-of-bounds array access) and moving the array to the heap just hides the issue. As @fxm says, always use -exx to find out what's happening.
Triopstor
Posts: 118
Joined: Apr 25, 2006 13:11

Re: Program Exits With Array Problem

Post by Triopstor »

fxm wrote: May 03, 2024 5:53 What is the operating system?
Compile the program with the '-exx' option and check if a runtime error message appears on the console output.

[edit]
Unlike 'DIM SHARED AS LONGINT YAxis2(221)', 'DIM SHARED AS LONGINT YAxis2(1 TO E3+LB1)' defines a dynamic array (instead of static) with its data located on the heap (if 'E3' and 'LB1' are not both 2 constants).
Okay. Next time I get the same or different issue can I compile with -exx option and check if a runtime error message appears?

It's solved now. I coded

Code: Select all

DIM SHARED AS LONGINT YAxis2(E3+LB1)
Instead of

Code: Select all

DIM SHARED AS LONGINT YAxis21(1 TO (E3+LB1)
Thanks fxm.
Triopstor
Posts: 118
Joined: Apr 25, 2006 13:11

Re: Program Exits With Array Problem

Post by Triopstor »

paul doe wrote: May 03, 2024 6:53 Most likely an array out of bounds error, yes. If I'm willing to bet, this line:

Code: Select all

DIM SHARED AS LONGINT YAxis2(1 TO E3+LB1)
Probably doesn't work as you'd expect (unless E3 and LB1 are constants)

They are NOT constants so you could be right. I coded

Code: Select all

DIM SHARED AS LONGINT YAxis2(E3+LB1)
instead of

Code: Select all

DIM SHARED AS LONGINT YAxis2(1 TO E3+LB1)
It works now even though they are not constants. Thanks paul doe. I'll remember to check if their constants next time.
Triopstor
Posts: 118
Joined: Apr 25, 2006 13:11

Re: Program Exits With Array Problem

Post by Triopstor »

caseih wrote: May 03, 2024 18:27
Triopstor wrote: May 02, 2024 23:28 Is that the solution?
Magic fixes like this are a probable sign you are experiencing memory corruption (out-of-bounds array access) and moving the array to the heap just hides the issue. As @fxm says, always use -exx to find out what's happening.
Okay. I'll always use -exx to find out what is happening. Thanks for your input caseih
Post Reply