设置一个新的独立变量的变化在C所有其他变量 [英] setting a new independent variable changes all the other variables in c

查看:159
本文介绍了设置一个新的独立变量的变化在C所有其他变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的不知道哪个称谓更适合的来形容我的问题。我通过VirtualBox的使用Linux。我使用的是gcc编译器。我一直在写我的code到现在为止(300线),而类似的问题。突然,如果我把一个新的变量,并设置一个值,在我的$ C $其他变量C更改它们的值。
我想知道如果我要清理内存什么的。

 
{
    rand_arr = rpermute(dim_x * dim_y);
    标志= 0;    对于(t = 0; T<(dim_x * dim_y);吨++)
    {
        X = rand_arr [T] / dim_x;
        Y = rand_arr [T]%dim_x;        如果(ARR [X] [Y] == 255)
        {
            current_distance = SQRT(PO​​W(退出[0] [0] - 的x,2)+ POW(退出[0] [1] - Y,2));            如果(!X = 0&放大器;&安培;!常用3 [X - 1] [Y] = 254)
            {
                distance_up = SQRT(PO​​W(退出[0] [0] - (X - 1),2)+ POW(退出[0] [1] - Y,2));
            }
            其他
            {
                distance_up = MAX_DISTANCE +千万;
            }            //这里有一个code计算类似的数学运算为
            // current_distance和distance_up            min_dist [0] = distance_up;
            min_dist [1] = distance_back;            //这里我继续在min_dist阵列中的所有变量设置
            //这里有一个冒泡code安排min_dist阵列            K = 0;
            做
            {
                如果(distance_up == min_dist [K]放大器;&安培;常用3 [X - 1] [Y] == 0安培;&安培; distance_up< current_distance)
                {
                    ARR [X - 1] [Y] = 255;
                    ARR [X] [Y] = 0;
                    标志= 1;
                    打破;
                }
                否则,如果(distance_back == min_dist [K]放大器;&安培;常用3 [X] [Y - 1] == 0安培;&安培; distance_back< current_distance)
                {
                    ARR [X] [Y - 1] = 255;
                    ARR [X] [Y] = 0;
                    标志= 1;
                    打破;
                }                //这里有一个code如果用类似上述两种说法                ķ++;
            }而(K&4;放大器;&放大器; min_dist [k]的&下; current_distance);
        }
    }
    ...
}而(...)

所以这个循环之前,每一个变量保持不变,但如果我只设置一个新的变量,例如 INT测试= 0; ,在这个循环中每个变量的变化,尽管的事实,新的变量是独立的。
我也使用的malloc 在函数;我不知道如果这是问题,但我没有任何问题,到现在。


解决方案

 的#include<&ASSERT.H GT; / *<<  - 在这里* /做
{
    rand_arr = rpermute(dim_x * dim_y);
    标志= 0;    对于(t = 0; T<(dim_x * dim_y);吨++)
    {
        X = rand_arr [T] / dim_x;
        Y = rand_arr [T]%dim_x;        如果(ARR [X] [Y] == 255)
        {
            current_distance = SQRT(PO​​W(退出[0] [0] - 的x,2)+ POW(退出[0] [1] - Y,2));            如果(!X = 0&放大器;&安培;!常用3 [X - 1] [Y] = 254)
            {
                distance_up = SQRT(PO​​W(退出[0] [0] - (X - 1),2)+ POW(退出[0] [1] - Y,2));
            }
            其他
            {
                distance_up = MAX_DISTANCE +千万;
            }            //这里有一个code计算类似的数学运算为
            // current_distance和distance_up            min_dist [0] = distance_up;
            min_dist [1] = distance_back;            //这里我继续在min_dist阵列中的所有变量设置
            //这里有一个冒泡code安排min_dist阵列            K = 0;
            做
            {
                如果(distance_up == min_dist [K]放大器;&安培;常用3 [X - 1] [Y] == 0安培;&安培; distance_up< current_distance)
                {
                    断言(X 0); //<< - 在这里
                    ARR [X - 1] [Y] = 255;
                    ARR [X] [Y] = 0;
                    标志= 1;
                    打破;
                }
                否则,如果(distance_back == min_dist [K]放大器;&安培;常用3 [X] [Y - 1] == 0安培;&安培; distance_back< current_distance)
                {
                    断言(γ大于0); //<< ---在这里,
                    ARR [X] [Y - 1] = 255;
                    ARR [X] [Y] = 0;
                    标志= 1;
                    打破;
                }                //这里有一个code如果用类似上述两种说法                ķ++;
            }而(K&4;放大器;&放大器; min_dist [k]的&下; current_distance);
        }
    }
    ...
}而(1); //<< - 在这里

I really don't know which title fits better to describe my problem. I'm using Linux through VirtualBox. I'm using the gcc compiler. I have been writing my code up until now (300 lines) without a similar problem. Suddenly, if I put a new variable and set a value, the other variables in my code change their values. I am wondering if I have to clean a memory or something.

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 (...)

So every variable before this loop remains the same, but if I just set a new variable, for example int test=0;, every variable changes in this loop despite the fact that the new variable is independent. I am also using malloc in a function; I wonder if this is the problem but I didn't have any problem till now.

解决方案

#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

这篇关于设置一个新的独立变量的变化在C所有其他变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆