Going beyond the bounds of the array + LOOP

General FreeBASIC programming questions.
VANYA
Posts: 1334
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Going beyond the bounds of the array + LOOP

Postby VANYA » Feb 12, 2020 18:10

Hi all!

Today saw an interesting case , when the array FOR misbehave :)

Code: Select all

dim p(10) as integer

dim ss(20) as Integer

for i as Long = 0 to 20
   
   p(i) = ss(i)
   
   ? I
   
   sleep(100)
   
Next


Result:

1 2 3 4 5 6 7 8 9 10 1 2 3 5 6 7 8 9 10........................... (infinite loop )

--------------------------------

It seems to me that logically it would be 2 result in this situation:

1) without compiling with -exx crash program , but not an infinite loop!
2) if you compile with -exx to crash program with display the error (DONE)

I hope the developers still here?
MrSwiss
Posts: 3348
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Going beyond the bounds of the array + LOOP

Postby MrSwiss » Feb 12, 2020 18:52

Well, you are accessing memory outside of the receiver array: p(0 to 10) ...
indices from: 11 to: 20, which should provoke a program-crash.
If no crash, then we simply have "undefined behaviour", anything is possible!

Fix: use LBound(p) to UBound(p) to size the For-Loop (always the smaller array).
SARG
Posts: 950
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Going beyond the bounds of the array + LOOP

Postby SARG » Feb 12, 2020 19:41

Hi VANYA,

With Gas32 and Gas64 it crashs after 20. I guess you used gcc 32 or 64.
As MrSwiss wrote, it's random so all is possible.

Add

Code: Select all

 print @i
and it crashs after 20...
badidea
Posts: 1779
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Going beyond the bounds of the array + LOOP

Postby badidea » Feb 12, 2020 19:55

Yes, undefined behaviour. Change to order of dim ss(20) and p(10) and behaviour is changed again.

Code: Select all

dim ss(20) as Integer
dim p(10) as integer
for i as Long = 0 to 20
   p(i) = ss(i)
   ? i
   sleep(100)
Next

What probably happens in the original code is that "i" gets overwritten.
VANYA
Posts: 1334
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: Going beyond the bounds of the array + LOOP

Postby VANYA » Feb 13, 2020 9:46

Hi all!

Thank you all for the answers!

SARG wrote:With Gas32 and Gas64 it crashs after 20.


SARG! You have brought it into a state of release? If Yes, then your creation to be included in the official distribution?

MrSwiss wrote:If no crash, then we simply have "undefined behaviour", anything is possible!


Probably BASIC programmers are waiting for more logical behavior from the usual cycles :)
MrSwiss
Posts: 3348
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Going beyond the bounds of the array + LOOP

Postby MrSwiss » Feb 13, 2020 13:46

VANYA wrote:Probably BASIC programmers are waiting for more logical behavior from the usual cycles :)
Even to get that, is no rocket science in FBC.

Just use (for error checking only) the proper compiler switches:
-exx -w pedantic and, it won't run (throws error = program abort).

Checks among other things: array "out of bounds" conditions.
SARG
Posts: 950
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Going beyond the bounds of the array + LOOP

Postby SARG » Feb 17, 2020 15:31

Hi VANYA,

VANYA wrote:SARG! You have brought it into a state of release? If Yes, then your creation to be included in the official distribution?

The version for windows is usable but that for linux is not ready. And there is a bit of work to clean and optimize the code.
It'll be up to coderjeff to decide to include it.

Meanwhile you can use :
http://users.freebasic-portal.de/sarg/f ... 64_107.zip

ONLY WITH FBC 1.08 as it was compiled with the new sources.
http://users.freebasic-portal.de/sarg/fbc64_gas64.zip
VANYA
Posts: 1334
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: Going beyond the bounds of the array + LOOP

Postby VANYA » Feb 18, 2020 14:00

Hi SARG!

SARG wrote:The version for windows is usable but that for linux is not ready.


And when Linux expected? Important to me that the version fbc under Linux (64x).

With the debugger under Linux things are moving?

Return to “General”

Who is online

Users browsing this forum: No registered users and 0 guests