Results 1 to 9 of 9

Thread: Range Check Error...

  1. #1

    Range Check Error...

    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?

    ******************************
    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

  2. #2
    You cannot do this:

    Code:
    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:

    Code:
    TMyCrazyArrayType = array [0..154] of Integer;
    
    function myfunction(somecrazyinputthingy: TMyCrazyArrayType): Integer;
    Have a nice day.
    Coders rule nr 1: Face ur bugz.. dont cage them with code, kill'em with ur cursor.

  3. #3
    But you have to remember that
    Code:
    Function myfunction(input1, input2: array of integer):real;
    and
    Code:
    type TMyIntArray = array[1..9] of integer;
    pMyIntArray = ^TMyIntArray;
    Function myfunction(input1, input2: pMyIntArray):real;
    are propably faster than
    Code:
    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.

  4. #4
    Ah yes. Good point.
    Coders rule nr 1: Face ur bugz.. dont cage them with code, kill'em with ur cursor.

  5. #5
    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?

  6. #6
    Yes, pointers are one of the harder things about programming. I'll have some links for you:

    http://delphi.about.com/od/objectpas...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:

    Code:
    //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.
    Coders rule nr 1: Face ur bugz.. dont cage them with code, kill'em with ur cursor.

  7. #7
    Okay I think I'm getting it. So basicly a pointer is what its name is: a pointer. 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?

  8. #8
    PGD Staff code_glitch's Avatar
    Join Date
    Oct 2009
    Location
    UK (England, the bigger bit)
    Posts
    933
    Blog Entries
    45
    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.
    I once tried to change the world. But they wouldn't give me the source code. Damned evil cunning.

  9. #9
    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •