变量的值根据向量值而变化
Values of the variables change according to vector values
我通过函数 MontaVetorVerticalOtimizado(x, y, Vetor)
的参数发送数组 int Vetor[33];
,在这个数组中填充了数组,问题是在填充数组后函数 OtimizaVerticalDentina()
的所有变量用数组的值签名,看起来很混乱,所以我在调试时添加了图像以使其更容易理解:
第一个函数:
void OtimizaVerticalDentina() {
int Vetor[33];
int x, y;
for (x = 1; x < NewImage.SizeX() - 1; x++)
{
for (y = 10; y < NewImage.SizeY() - 10; y++)
{
MontaVetorVerticalOtimizado(x, y, Vetor);
VerificaIntensidadeVetorVerticalOtimizado(Vetor);
if (bPreenche) {
NewImage.DrawPixel(x, y, 255, 255, 255);
} else {
NewImage.DrawPixel(x, y, 0, 0, 0);
bPreenche = true;
}
}
}
}
第二个函数:
void MontaVetorVerticalOtimizado(int Px, int Py, int Vetor[33])
{
int x, y;
int i = 0;
unsigned char r, g, b;
for(x = Px - 1; x <= Px + 1; x++)
{
for(y = Py - 10; y <= Py + 10; y++)
{
NewImage.ReadPixel(x, y, r, g, b);
Vetor[i] = r;
i++;
}
}
}
注:
ImageClass NewImage; // global
之前 填充数组变量是他们的正常值
在 填充数组后,变量使用另一个值(添加到向量中的值)
*我在第一个测试方法中创建了其他变量,它们也发生了变化,有人知道会发生什么吗?
我能找到的唯一解释是您遇到了缓冲区溢出。那就是你正在写入这个数组(Vetor
),它不够大并且恰好覆盖了进程中不相关的内存。在这种情况下,具体来说,您将覆盖调用函数的变量 x
和 y
的值。
我有演示 here:
#include <iostream>
void bar(int* arr)
{
for (int i = 0; i <= 35; i++) arr[i] = 255;
}
void foo()
{
int arr[33];
int x;
for (x = 0; x < 5; x++)
{
std::cout << x << '\n';
bar(arr);
std::cout << x << '\n';
}
}
int main()
{
foo();
return 0;
}
这会产生:0 255 并立即终止,因为循环变量被覆盖并且随后的 x < 5
检查失败。您要么必须增加数组的大小(如果事实证明它太小),要么确保在其范围内进行索引。
我通过函数 MontaVetorVerticalOtimizado(x, y, Vetor)
的参数发送数组 int Vetor[33];
,在这个数组中填充了数组,问题是在填充数组后函数 OtimizaVerticalDentina()
的所有变量用数组的值签名,看起来很混乱,所以我在调试时添加了图像以使其更容易理解:
第一个函数:
void OtimizaVerticalDentina() {
int Vetor[33];
int x, y;
for (x = 1; x < NewImage.SizeX() - 1; x++)
{
for (y = 10; y < NewImage.SizeY() - 10; y++)
{
MontaVetorVerticalOtimizado(x, y, Vetor);
VerificaIntensidadeVetorVerticalOtimizado(Vetor);
if (bPreenche) {
NewImage.DrawPixel(x, y, 255, 255, 255);
} else {
NewImage.DrawPixel(x, y, 0, 0, 0);
bPreenche = true;
}
}
}
}
第二个函数:
void MontaVetorVerticalOtimizado(int Px, int Py, int Vetor[33])
{
int x, y;
int i = 0;
unsigned char r, g, b;
for(x = Px - 1; x <= Px + 1; x++)
{
for(y = Py - 10; y <= Py + 10; y++)
{
NewImage.ReadPixel(x, y, r, g, b);
Vetor[i] = r;
i++;
}
}
}
注:
ImageClass NewImage; // global
之前 填充数组变量是他们的正常值
在 填充数组后,变量使用另一个值(添加到向量中的值)
*我在第一个测试方法中创建了其他变量,它们也发生了变化,有人知道会发生什么吗?
我能找到的唯一解释是您遇到了缓冲区溢出。那就是你正在写入这个数组(Vetor
),它不够大并且恰好覆盖了进程中不相关的内存。在这种情况下,具体来说,您将覆盖调用函数的变量 x
和 y
的值。
我有演示 here:
#include <iostream>
void bar(int* arr)
{
for (int i = 0; i <= 35; i++) arr[i] = 255;
}
void foo()
{
int arr[33];
int x;
for (x = 0; x < 5; x++)
{
std::cout << x << '\n';
bar(arr);
std::cout << x << '\n';
}
}
int main()
{
foo();
return 0;
}
这会产生:0 255 并立即终止,因为循环变量被覆盖并且随后的 x < 5
检查失败。您要么必须增加数组的大小(如果事实证明它太小),要么确保在其范围内进行索引。