'for' loop/'if' statement bugging?
#1

Hey!

I was scripting and when I did the next;
pawn Код:
for(new i = 0; i < 1; i++)
    {
    printf("i = %d", i);
    }
The code would only print;
Код:
i = 0
Then I tried these two (Basically the same);
pawn Код:
for(new i = 0; i < 2; i++)
    {
    printf("i = %d", i);
    }
// and
for(new i = 0; i <= 1; i++)
    {
    printf("i = %d", i);
    }
The code (And crashdetect straight after it) printed;
Код:
i = 0
i = 1
Run time error 4: "Array index out of bounds"
Accessing element at index 2 past array upper bound 1
Then I got suspicious something had to be wrong as the first code would just loop with 0 and the second codes go to 2.
changed it like this;
pawn Код:
for(new i = -1; i < 2; i++)
    {
    printf("i = %d", i);
    if(i == 0 || i == 1)
        {
        }
    }
And it is still printing;
Код:
i = 0
i = 1
Run time error 4: "Array index out of bounds"
Accessing element at index 2 past array upper bound 1
The if statement is not bugging when it's on -1, as crashdetect isn't printing for accessing at negative index but it is bugging when 'i' gets to 2, as crashdetect is printing. How is this possible?!

Best regards,
Jesse

EDIT:
Even after changing the code to this;
pawn Код:
for(new i = 0; i < 2; i++)
    {
    if(i == 0 || i == 1)
        {
        if(i == 2) break;
        }
    }
Crashdetect is printing;
Код:
Run time error 4: "Array index out of bounds"
Accessing element at index 2 past array upper bound 1
Reply
#2

The error means that the array has size of 2 so the valid indexes are 0 and 1. Any other value will cause run time error 4.

It'd be helpful if you could post the whole code (that you use "i" as index in the array) and show the results of crashdetect with -d3 flag: https://github.com/Zeex/samp-plugin-...ith-debug-info
it will print the line caused it.
Reply
#3

somehow I think the problem is related outside the loop... This because inside the loop the i will never be 2, just outside of it, so are you really sure the problem is inside the loop?
Reply
#4

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
The error means that the array has size of 2 so the valid indexes are 0 and 1. Any other value will cause run time error 4.

It'd be helpful if you could post the whole code (that you use "i" as index in the array) and show the results of crashdetect with -d3 flag: https://github.com/Zeex/samp-plugin-...ith-debug-info
it will print the line caused it.
Yea well, I've been a fool, I did this;
pawn Код:
if(!sscanf(params, "ds[10]dd", vehicleid, setedit, type, var))
The params would be [VehicleID] [Copy] [FromVehicleID] [ID:1/2/3] (1 to copy 0, 2 to copy 1 and 3 to copy both, that's what's the loop is for.)

In the switch if you would've filled in 1 or 2 it would have this at the error'ing array;
Код:
(var-1)
If you would've filled in 3 '(var-1)' should've been replaced by 'i' from the loop but I forgot to replace it at 1 place, what resulted in doing "(3-1)" which is obviously 2.

So this topic can be deleted, was my own fault!

Best regards,
Jesse
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)