Results 1 to 2 of 2

Thread: Access Violation at line 25

  1. #1
    Join Date
    Mar 2008
    Location
    New Jersey
    Posts
    1,673
    Mentioned
    1 Post(s)
    Quoted
    9 Post(s)

    Default [RESOLVED] Access Violation at line 25

    Code:
    program new;
    
    //From SRL include, credit to SRL DEVS & EvilChicken!
    procedure DeleteValueInIntArray(var Arr: TIntegerArray; ValuePosition: Integer);
    var
      ArrLen, I: Integer;
    begin
      ArrLen := High(Arr);
      for I := ValuePosition to ArrLen - 1 do
        Swap(Arr[I], Arr[I + 1]);
      SetArrayLength(Arr, ArrLen);
    end;
    
    procedure AutoThis;
    var
     i, ii, randomNum: Integer;
     arr, OTP: TIntegerArray;
    
    begin
      setLength(arr, 26);
      setLength(OTP, 26);
      for ii:= 0 to 4 do
      begin
        for i:= 0 to 25 do
          arr[i]:= i+1;
        for i:= 0 to 25 do
        begin
          randomNum:= Random(high(arr));
          OTP[i]:= arr[randomNum];
          DeleteValueInIntArray(arr, randomNum);
        end;
        for i:= 0 to 25 do
          writeln(OTP[ii]);
        writeln(chr(13) + chr(13));
      end;
    end;
    
    begin
      AutoThis;
    end.
    Error:

    Error: Access violation at line 25

    I'm pretty rusty, it's been a while since I've programmed like I used to back in the day. So this one's got me. I'm only trying to access index 0-25 and the length is 26, I can't figure this one out. Any help guys? Thanks in advanced.

    Baked



    EDIT:

    [RESOLVED]

    The error was due to line 25 resetting my arr length to be one shorter, I just had to drop line 20 down 4 lines into the first outter loop.

    Benland100 helped me discover my error:

    Code:
    [02:23] <@BenLand100> the make-my-array-shorter one
    [02:23] <@BenLand100> then you try to set all 26 elements again next loop
    [02:23] <@BenLand100> obvs you segfault
    [02:24] <@BenLand100> if you move line 20 inside that for loop it'll work
    ianh and R0b0t1 was also helpful towards me in the making of this script. I am aware there is a better way to do this, as pointed out be Benland in the irc channel, this was just my original thought so I wanted to make it come alive before abandoning it for educational and memory refreshing purposes. Thanks everyone.
    Last edited by Baked0420; 04-26-2018 at 06:40 AM. Reason: Resolved

  2. #2
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    995
    Mentioned
    145 Post(s)
    Quoted
    596 Post(s)

    Default

    fyi, lape has that delete function built in.
    > Delete(Arr: <array or string>; Index, Count: SizeInt);
    Drop in replacement looks like this:

    > Delete(arr, RandomNum, 1);

    And if you used Simba 1.2 your error would be a tad more friendly:
    Code:
    ...
    Error: Index out of range (index:0, low:0, high:-1) at line 25
    Execution failed.
    Last edited by slacky; 04-26-2018 at 11:43 AM.
    !No priv. messages please

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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