简体   繁体   English

While循环后C中的空白值(LinkedList)

[英]Blank Values In C After While Loop (LinkedList)

I am working on a problem, that has to do with the Barnes-Hut algorithm for solving the N-Body problem. 我正在研究一个问题,该问题与解决N体问题的Barnes-Hut算法有关。

I believe this is a C issue. 我相信这是一个C问题。 But to give you some context, this method is used to get the new X and Y positions for each body. 但是,为了给您一些上下文,此方法用于获取每个实体的新X和Y位置。

Here's the problem, it seems to be doing the calculations correctly, and when I print out the values of which I want to see from within the loop they are properly populated. 这就是问题所在,它似乎在正确地进行计算,当我打印出我想从循环中看到的值时,它们已正确填充。

However, when I go to print the values of the node outside the loop, they are all zeros. 但是,当我在循环外打印节点的值时,它们全为零。

I need to C programming, by can someone explain to me why this is happening, or how to go about fixing this? 我需要进行C编程,有人可以向我解释为什么会发生这种情况,或者如何解决这个问题?

I have attached the code below, and below that, I have attached the print out I am seeing from the console. 我在下面附加了代码,在下面附加了从控制台看到的打印输出。 Any and all help is greatly appreciated! 任何帮助都将不胜感激!

void reposition (struct Node *node)
{
struct Node *referenceNode = node;
int numberOfBodies = getAmountOfBodies(node->body);
float xValues [numberOfBodies];
float yValues [numberOfBodies];
int index = 0;

while (node->body->next != NULL)
{
    struct Forces * forces = getForce(node->body, node);

    //NOT SURE IF DIVIDING BY BODY MASS, OR NODE MASS
    float xVelocity = node->body->pos_x + (kDeltaTime * forces->x)/node->body->mass;
    float yVelocity = node->body->pos_y + (kDeltaTime * forces->y)/node->body->mass;
    xValues[index] = node->body->pos_x + xVelocity * kDeltaTime;
    yValues[index] = node->body->pos_y + yVelocity * kDeltaTime;
    node->body->v_x = xVelocity;
    node->body->v_y = yVelocity;
    index++;

    printf("RED: %d | GREEN: %d | BLUE: %d | xVelocity: %f | yVelocity: %f \n", node->body->red, node->body->green, node->body->blue, xVelocity, yVelocity);
    node->body = node->body->next;
}
node = referenceNode;

printf("\n");
printf("RED: %d | GREEN: %d | BLUE: %d | xVelocity: %f | yVelocity: %f \n", node->body->red, node->body->green, node->body->blue, xValues[0], yValues[0]);
}

BEFORE LOOP 循环之前

RED: 255 | GREEN: 255 | BLUE: 0 | xVelocity: 0.000000 | yVelocity: 0.000000 
 RED: 0 | GREEN: 255 | BLUE: 0 | xVelocity: 57899999232.000000 | yVelocity: 0.000000 
 RED: 255 | GREEN: 0 | BLUE: 255 | xVelocity: 108200001536.000000 | yVelocity: 0.000000 
 RED: 0 | GREEN: 50 | BLUE: 255 | xVelocity: 149599993856.000000 | yVelocity: 0.000000 
 RED: 255 | GREEN: 0 | BLUE: 0 | xVelocity: 227899998208.000000 | yVelocity: 0.000000 

AFTER LOOP 循环后

RED: 0 | GREEN: 0 | BLUE: 0 | xVelocity: 0.000000 | yVelocity: 0.000000 

循环发布后的输出似乎表明LinkedList的最后一个非空节点中的值(因为循环末尾的位置)为零,这就是为什么要获得该输出的原因。

The last statement in your loop permanently changes the node's body pointer. 循环中的最后一条语句永久更改节点的body指针。 So, after the loop referenceNode has been trashed. 因此,在循环referenceNode被废弃之后。

Here's your code with annotations to show the problem [please pardon the gratuitous style cleanup]: 这是带有注释的代码,以显示问题[请原谅免费的样式清理]:

void
reposition(struct Node *node)
{
    struct Node *referenceNode = node;
    int numberOfBodies = getAmountOfBodies(node->body);
    float xValues[numberOfBodies];
    float yValues[numberOfBodies];
    int index = 0;

    while (node->body->next != NULL) {
        struct Forces *forces = getForce(node->body, node);

        // NOT SURE IF DIVIDING BY BODY MASS, OR NODE MASS
        float xVelocity = node->body->pos_x + (kDeltaTime * forces->x) / node->body->mass;
        float yVelocity = node->body->pos_y + (kDeltaTime * forces->y) / node->body->mass;

        xValues[index] = node->body->pos_x + xVelocity * kDeltaTime;
        yValues[index] = node->body->pos_y + yVelocity * kDeltaTime;
        node->body->v_x = xVelocity;
        node->body->v_y = yVelocity;
        index++;

        printf("RED: %d | GREEN: %d | BLUE: %d | xVelocity: %f | yVelocity: %f \n",
            node->body->red, node->body->green, node->body->blue,
            xVelocity, yVelocity);

        // NOTE/BUG: on the first iteration this is trashing referenceNode and
        // each subsequent node on each subsequent iteration
        node->body = node->body->next;
    }

    node = referenceNode;

    printf("\n");
    printf("RED: %d | GREEN: %d | BLUE: %d | xVelocity: %f | yVelocity: %f \n",
        node->body->red, node->body->green, node->body->blue,
        xValues[0], yValues[0]);
}

Here's the corrected code. 这是更正的代码。 Note I had to guess at the struct definition for what body points to [I called it Body ], but otherwise it should be fine: 注意,我不得不在结构定义中猜测body指向[我称之为Body ]的含义,但是否则应该没问题:

void
reposition(struct Node *node)
{
    struct Node *referenceNode = node;
    struct Body *body;
    int numberOfBodies = getAmountOfBodies(node->body);
    float xValues[numberOfBodies];
    float yValues[numberOfBodies];
    int index = 0;

    body = node->body;
    while (body->next != NULL) {
        struct Forces *forces = getForce(body, node);

        // NOT SURE IF DIVIDING BY BODY MASS, OR NODE MASS
        float xVelocity = body->pos_x + (kDeltaTime * forces->x) / body->mass;
        float yVelocity = body->pos_y + (kDeltaTime * forces->y) / body->mass;

        xValues[index] = body->pos_x + xVelocity * kDeltaTime;
        yValues[index] = body->pos_y + yVelocity * kDeltaTime;
        body->v_x = xVelocity;
        body->v_y = yVelocity;
        index++;

        printf("RED: %d | GREEN: %d | BLUE: %d | xVelocity: %f | yVelocity: %f \n",
            body->red, body->green, body->blue,
            xVelocity, yVelocity);

        // NOTE/BUGFIX: here we're only advancing the pointer but _not_
        // permanently changing the node's body pointer
        body = body->next;
    }

    node = referenceNode;
    body = node->body;

    printf("\n");
    printf("RED: %d | GREEN: %d | BLUE: %d | xVelocity: %f | yVelocity: %f \n",
        body->red, body->green, body->blue,
        xValues[0], yValues[0]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM