Delphi中的访问冲突? [英] Access violation in Delphi?
问题描述
我得到了以下代码,该代码应该正确访问矩阵位置,但是我总是遇到这种访问冲突...
I got the following code which is supposed to correctly acess the matrix positions, however I'm always getting this access violation...
var
tabla : array of array of string;
....
implementation
SetLength(tabla, minterms.Count+1, minterms_essentials.Count+1);
for i := 0 to minterms.Count-1 do
begin
tabla[i+2,1] := minterms[i];
end;
for i := 0 to minterms_essentials.Count-1 do
begin
tabla[1, i+2] := minterms_essentials[i];
end;
end
基本上,我正在生成一个表,并在循环中尝试在第二个循环中填充列标记和行标记.据我所知,数组从1开始.
Basically, I'm generating a table and in the loop I'm trying to fill the column tags and the row tags in second loop. As long as I know, arrays start in 1.
tabla[1][1]
会是一个空置的索引,这就是为什么我没有触摸它的原因.
tabla[1][1]
would an unoccupied index, that's why I'm ain't touching it.
为什么会发生访问冲突?
推荐答案
在Delphi中,动态数组(可在其上调用SetLength的任何数组,而不是像array[1..5] of integer
那样在编译时声明其边界)都从以下索引开始0,而不是1.因此,通过将数组当作使用基于1的索引而不是基于0的索引来对待,就溢出了数组的边界,并尝试写入未分配给您的内存,这可能会导致访问冲突.
In Delp dynamic arrays (any array that you can call SetLength on, as opposed to declaring its bounds at compile time like array[1..5] of integer
) are indexed starting from 0, not from 1. So by treating your array as if it used 1-based indexing instead of 0-based indexing, you're overflowing the bounds of the array and trying to write to memory that's not allocated to you, which can cause an access violation.
这篇关于Delphi中的访问冲突?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!