Gamestudio Links
Zorro Links
Newest Posts
Free Live Data for Zorro with Paper Trading?
by AbrahamR. 05/18/24 13:28
Change chart colours
by 7th_zorro. 05/11/24 09:25
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
4 registered members (degenerate_762, AbrahamR, AndrewAMD, ozgur), 667 guests, and 8 spiders.
Key: Admin, Global Mod, Mod
Newest Members
Hanky27, firatv, wandaluciaia, Mega_Rod, EternallyCurious
19051 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Strange Array behavior #358264
02/10/11 06:35
02/10/11 06:35
Joined: Jan 2011
Posts: 120
United States
Logan Offline OP
Member
Logan  Offline OP
Member

Joined: Jan 2011
Posts: 120
United States
I experienced some weird behavior with exceeding array bounds and so I wrote a tiny program to help isolate some of the cases. I am simply irked by the way the engine reacts (or doesn't) to bad array indices, and I wish there was a consistent way that it did--especially because, as I will show, bad array assignments are sometimes totally tolerated.

Case 1:
Code:
function main()
{
	var array[3];
	
	int i = 0;
	for(; i<=3; i++)
		array[i] = array[i+1];
	wait(-2);
	sys_exit(NULL);
}


This bad for() loop, which accesses "members" of the array 2 indices outside of its bounds, freezes the engine and requires a CTRL-ALT-DELETE.

Case 2:
The following code actually RUNS with no apparent error:
Code:
function main()
{
	var array[3];
	
	array[100] = 50;	

	wait(-2);
	sys_exit(NULL);
}


A crash only occurs if you try to access it later, e.g. array[0]=array[100];.

Amazingly, even absurd array indices such as 67.5 do not cause a crash immediately--only when they are accessed.

Never once does an engine message pop up saying "Script crash--bad array index." The engine simply crashes.

It seems dangerous to not have a specific error message occur for these types of errors, which are very common. Engine freezes and crashes can happen by plenty of other things, in the case of freezing when in fullscreen, this causes a major inconvenience. Even worse is the case of the apparent success in assigning to a bogus array index... no error message implies no error, and bad array calls are hard to find. That's why we need a "BAD ARRAY INDEX IN SYS" message.

Re: Strange Array behavior [Re: Logan] #358274
02/10/11 09:30
02/10/11 09:30
Joined: Apr 2007
Posts: 3,751
Canada
WretchedSid Offline
Expert
WretchedSid  Offline
Expert

Joined: Apr 2007
Posts: 3,751
Canada
How is the engine supposed to know that you access an invalid member? You just do some pointer shifting and then access something in an anonymous mass of memory.
That your script crashes is just the access of unmapped memory (thats the lucky case where you know something bad happens), but you can also overwrite everything else in the RAM that is mapped into your process.




Shitlord by trade and passion. Graphics programmer at Laminar Research.
I write blog posts at feresignum.com
Re: Strange Array behavior [Re: WretchedSid] #358301
02/10/11 16:32
02/10/11 16:32
Joined: Jan 2011
Posts: 120
United States
Logan Offline OP
Member
Logan  Offline OP
Member

Joined: Jan 2011
Posts: 120
United States
Plenty of other languages/environments have ArrayIndexOutOfBoundsException etc. If I define array[3] and then set array[-26.7] to 0, I should get an error message. I don't. At best I get a crash that is unexplained. How is an array error message more impossible than an error message for a bad/empty pointer or an other script crash?

Re: Strange Array behavior [Re: Logan] #358306
02/10/11 16:57
02/10/11 16:57
Joined: Nov 2010
Posts: 96
Vienna
S
Schubido Offline
Junior Member
Schubido  Offline
Junior Member
S

Joined: Nov 2010
Posts: 96
Vienna
This is a concept decision.
Either the complier creates code which checks the array bounderys.
Advantage: saver code
Disadvantage: lower performance, because bounderys need do be checked at each single array access.

Or the compiler creates code which does not check.
Advantage: better performance
Disadvantage: invalid memory access
This can be a memory adress where the program is allowed to access - then something unexpected happens, often unreproducible. Or it happens that the program is not allowed to the memory adress - this causes usually the process to be killed by the operating system if the exception is not handled. Usually the exeption handler tells you "I am crashed".

C usually is optimized for high performance and expects the developer to take care about proper memory access an therefore does no boundery check for arrays.
Lite-C gives sometimes the impression of managed code with some concepts (e.g. STRING), but its also tuned for performance.

Re: Strange Array behavior [Re: Logan] #358332
02/10/11 19:51
02/10/11 19:51
Joined: Apr 2007
Posts: 3,751
Canada
WretchedSid Offline
Expert
WretchedSid  Offline
Expert

Joined: Apr 2007
Posts: 3,751
Canada
Originally Posted By: Logan
Plenty of other languages/environments have ArrayIndexOutOfBoundsException etc. If I define array[3] and then set array[-26.7] to 0, I should get an error message. I don't.

Surprise surprise, C doesn't store the needed information for the array in memory. You can basically shift everything with the [] brackets, even if its not an array.

In C, informations about objects are only available at compile time, at runtime those informations aren't needed anymore. And one can have valid reasons to exceed the boundaries of an array.


Shitlord by trade and passion. Graphics programmer at Laminar Research.
I write blog posts at feresignum.com
Re: Strange Array behavior [Re: WretchedSid] #358336
02/10/11 20:11
02/10/11 20:11
Joined: Jan 2011
Posts: 120
United States
Logan Offline OP
Member
Logan  Offline OP
Member

Joined: Jan 2011
Posts: 120
United States
If I'm the only person whom this bugs, there is no need to discuss it any further. I'll just suck it up.

A runtime error, at LEAST at a high warn_level, seems justified to me. Especially for those who may be inexperienced with arrays, like the beginners this language invites.

But fair enough. I will simply guard my indices carefully. tongue

Re: Strange Array behavior [Re: Logan] #358343
02/10/11 20:31
02/10/11 20:31
Joined: Apr 2007
Posts: 3,751
Canada
WretchedSid Offline
Expert
WretchedSid  Offline
Expert

Joined: Apr 2007
Posts: 3,751
Canada
But the warn_level is a runtime feature, so even if the error message wouldn't be needed, the compiler had to generate the needed informations.


Shitlord by trade and passion. Graphics programmer at Laminar Research.
I write blog posts at feresignum.com

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1