PDA

View Full Version : Range Check Error...



asdfarkangel
21-10-2010, 08:19 PM
Hi I've got this stupid range check error. I can not define the size of arrays in the input section when declaring and defining functions. altough if I do not define them it will be empty and will create range check error. The solution is just way too simple for me to find it as usually can someone help me?:D

******************************
Details: my function is something like this:
Function myfunction(input1:array of integer; input2:array of integer):real;
{defining as function myfunction(input1:array[1..9] of integer;input2:array[1..9] of integer):real;
will result in error "of expected [ found"}
begin
{calculations...}
myfunction:=calculated_value;
end;
I declare array and an output variable in the main program:
var
array1,array2:array[1..9] of integer;
variable:real;
I then want to call my function

variable:=myfunction(array1,array2);
and then it says Error 201(Which is range check error)

Thanks in forward:D

chronozphere
21-10-2010, 09:56 PM
You cannot do this:



function myfunction(somecrazyinputthingy: array [0..154] of Integer): Integer;


You can only use simple pre-defined types for your arguments. To solve this, just define the type you want:



TMyCrazyArrayType = array [0..154] of Integer;

function myfunction(somecrazyinputthingy: TMyCrazyArrayType): Integer;


Have a nice day. ;)

User137
22-10-2010, 08:47 AM
But you have to remember that

Function myfunction(input1, input2: array of integer):real;
and

type TMyIntArray = array[1..9] of integer;
pMyIntArray = ^TMyIntArray;
Function myfunction(input1, input2: pMyIntArray):real;
are propably faster than

TMyCrazyArrayType = array [0..154] of Integer;
function myfunction(somecrazyinputthingy: TMyCrazyArrayType): Integer;
because the 2 upper versions only send a pointer to the array, don't make actual copies of each of the element in it. Especially if the array is big, copying it all in would be extremely bad coding.

chronozphere
22-10-2010, 09:06 AM
Ah yes. Good point. ;)

asdfarkangel
24-10-2010, 05:18 PM
Thanks this way it works fine:). I've never used pointers because I do not exactly understand them. What is the difference between a pointer and the variable itself except for the pointer uses less memory?

chronozphere
25-10-2010, 08:18 AM
Yes, pointers are one of the harder things about programming. I'll have some links for you:

http://delphi.about.com/od/objectpascalide/a/pointers.htm

http://www.delphibasics.co.uk/Article.asp?Name=Pointers

Ok, I'll breifly explain them:

All your variables are stored in RAM memory. Pointers are also stored there, but they point to another location in RAM. They don't contain value's like 6 or "abc", but memory adresses.
A quick example:



//Make a new pointer type that points to integers (not sure if this allready exists)
type
pinteger = ^integer;

var
v, w: integer;
p: pinteger;
begin
v := 100;
w := 50;
p := @v; //the value of p is now the memory location of v

showmessage( IntToStr( p^ ) ); //Output: 100 (^ means: show us what is stored at this memory location)

v := v + 10;

showmessage( IntToStr( p^ ) ); //Output: 110

p := @w; //Let p point to w now

showmessage( IntToStr( p^ ) ); //output 40

p := nil; //NIL means "points to nothing"

showmessage( IntToStr( p^ ) ); //gives an error
end;


Hope that helps. :)

asdfarkangel
27-10-2010, 10:05 PM
Okay I think I'm getting it. So basicly a pointer is what its name is: a pointer:D. And I could replace it with a variable however in your example an integer "p" variable would use more memory than the pointer "p" which doesn't store the true value of the other variables just their memory address. So maybe in this example it doesn't count much but if I wrote a bigger unit then it would be advisable for the defined types of the unit to make pointers for them and in the main program use those instead right?

code_glitch
28-10-2010, 11:42 AM
My advice to you in terms or trying to access data outside of arrays is too have the array as a record or type, and a constant which tells you how long that array is. Then when you use loops to process data in that array, add a clause that breaks the loop when whatever position in the array you are processing is >= maximum length. That way you never get any crashes from processing data outside an array... It helps in debugging, especially if you have a lot of arrays, you can just rule that out in most cases. (unless you a clutz like me that defines an array 1..100 and then sets the limit to 250.... it took a while to find, trust me.)

hope this was useful,
code_glitch.

asdfarkangel
28-10-2010, 09:00 PM
:D I know mistakes like that very well. The stupidest ones are always the hardest to find. And the advice was very useful thanks for it too.