Strange Array behavior

Posted By: Logan

Strange Array behavior - 02/10/11 06:35

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.
Posted By: WretchedSid

Re: Strange Array behavior - 02/10/11 09:30

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.


Posted By: Logan

Re: Strange Array behavior - 02/10/11 16:32

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?
Posted By: Schubido

Re: Strange Array behavior - 02/10/11 16:57

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.
Posted By: WretchedSid

Re: Strange Array behavior - 02/10/11 19:51

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.
Posted By: Logan

Re: Strange Array behavior - 02/10/11 20:11

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
Posted By: WretchedSid

Re: Strange Array behavior - 02/10/11 20:31

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.
© 2024 lite-C Forums