我真的不知道哪个标题更适合描述我的问题。我通过VirtualBox使用Linux。我正在使用gcc编译器。到目前为止,我一直在编写代码(300行),没有类似的问题。突然间,如果我放置一个新变量并设置一个值,我代码中的其他变量就会改变它们的值。 我想知道我是否需要清理记忆或其他东西。
do
{
rand_arr = rpermute(dim_x * dim_y);
flag = 0;
for (t = 0; t < (dim_x * dim_y); t++)
{
x = rand_arr[t] / dim_x;
y = rand_arr[t] % dim_x;
if (arr[x][y] == 255)
{
current_distance = sqrt(pow(exit[0][0] - x, 2) + pow(exit[0][1] - y, 2));
if (x != 0 && arr[x - 1][y] != 254)
{
distance_up = sqrt(pow(exit[0][0] - (x - 1), 2) + pow(exit[0][1] - y, 2));
}
else
{
distance_up = max_distance + 10000000;
}
// Here there is a code computing similar math operations as
// current_distance and distance_up
min_dist[0] = distance_up;
min_dist[1] = distance_back;
// Here i continue setting in the min_dist array all the variables
// Here there is a bubblesort code to arrange the min_dist array
k = 0;
do
{
if (distance_up == min_dist[k] && arr[x - 1][y] == 0 && distance_up < current_distance)
{
arr[x - 1][y] = 255;
arr[x][y] = 0;
flag = 1;
break;
}
else if (distance_back == min_dist[k] && arr[x][y - 1] == 0 && distance_back < current_distance)
{
arr[x][y - 1] = 255;
arr[x][y] = 0;
flag = 1;
break;
}
// Here there is a code with if statements similar to the two above
k++;
} while (k < 4 && min_dist[k] < current_distance);
}
}
...
} while (...)
因此,在此循环之前的每个变量都保持不变,但如果我只设置一个新变量,例如int test=0;
,则每个变量在此循环中都会发生变化,尽管新变量是独立的。
我也在函数中使用malloc
;我想知道这是不是问题,但到目前为止我没有任何问题。
答案 0 :(得分:1)
#include <assert.h> /* <<-- Here */
do
{
rand_arr = rpermute(dim_x * dim_y);
flag = 0;
for (t = 0; t < (dim_x * dim_y); t++)
{
x = rand_arr[t] / dim_x;
y = rand_arr[t] % dim_x;
if (arr[x][y] == 255)
{
current_distance = sqrt(pow(exit[0][0] - x, 2) + pow(exit[0][1] - y, 2));
if (x != 0 && arr[x - 1][y] != 254)
{
distance_up = sqrt(pow(exit[0][0] - (x - 1), 2) + pow(exit[0][1] - y, 2));
}
else
{
distance_up = max_distance + 10000000;
}
// Here there is a code computing similar math operations as
// current_distance and distance_up
min_dist[0] = distance_up;
min_dist[1] = distance_back;
// Here i continue setting in the min_dist array all the variables
// Here there is a bubblesort code to arrange the min_dist array
k = 0;
do
{
if (distance_up == min_dist[k] && arr[x - 1][y] == 0 && distance_up < current_distance)
{
assert (x > 0); // <<-- Here
arr[x - 1][y] = 255;
arr[x][y] = 0;
flag = 1;
break;
}
else if (distance_back == min_dist[k] && arr[x][y - 1] == 0 && distance_back < current_distance)
{
assert (y > 0); // <<--- Here
arr[x][y - 1] = 255;
arr[x][y] = 0;
flag = 1;
break;
}
// Here there is a code with if statements similar to the two above
k++;
} while (k < 4 && min_dist[k] < current_distance);
}
}
...
} while (1); // <<-- Here