程序以printf为中间结束,但不在GDB中

时间:2014-12-12 06:15:29

标签: c

这里真的很奇怪,当我通过GDB运行此代码时,它运行正常。但是,如果我正常运行,程序将终止于printf的中间位置。没有seg故障或任何事情,它会使退出成功。

我放入行printf("\nDone\n");以查看它是否只是循环结束,但该行永远不会执行,如果我移动它,那么printf停止的位置会发生变化。

void remove_dup() {
    int i, j;
    Dist origin_dist[sgt_head.len];
    Sgt_Node *s_n = &sgt_head;
    for (i = 0; i < sgt_head.len; i++) {
        s_n = s_n->ptr;
        origin_dist[i].dist = great_circle(s_n->sgt->loc, origin);
        origin_dist[i].sgt = s_n->sgt;
    }
    for (i = 0; i < sgt_head.len; i++) {
        printf("  %f %s\n", origin_dist[i].dist, origin_dist[i].sgt->obs.id);
    }
    qsort(origin_dist, sgt_head.len, sizeof(Dist), comp_dist);
    printf("Sorted Array:\n");
    for (i = 0; i < sgt_head.len; i++) {
        printf("  %f %s\n", origin_dist[i].dist, origin_dist[i].sgt->obs.id);
    }

    int duplicate = 0;
    for (i = 0; i < sgt_head.len;) {
        double sep = origin_dist[i + 1].dist - origin_dist[i].dist;
        if (sep <= 0.02 && origin_dist[i + 1].sgt->type == origin_dist[i].sgt->type) {
            double gcd = great_circle(origin_dist[i].sgt->loc, origin_dist[i + 1].sgt->loc);
            if (gcd <= 0.02) {
                char type[10];
                int count = 1;
                location avg;
                avg.lat = 0.0;
                avg.lng = 0.0;
                if (origin_dist[i].sgt->type == 'P') {
                    strcpy(type, "Porpoise");
                } else {
                    strcpy(type, "Dolphins");
                }
                printf("\nMultiple %s:\n    Obs. lat.      lng.\n", type);
                printf("    %s %f %f\n", origin_dist[i].sgt->obs.id, origin_dist[i].sgt->loc.lat, origin_dist[i].sgt->loc.lng);
                avg.lat += origin_dist[i].sgt->loc.lat;
                avg.lng += origin_dist[i].sgt->loc.lng;
                do {
                    if (i != sgt_head.len - 1) {
                        i++;
                        count++;
                        sep = origin_dist[i + 1].dist - origin_dist[i].dist;
                        printf("    %s %f %f\n", origin_dist[i].sgt->obs.id, origin_dist[i].sgt->loc.lat, origin_dist[i].sgt->loc.lng);
                        avg.lat += origin_dist[i].sgt->loc.lat;
                        avg.lng += origin_dist[i].sgt->loc.lng;
                    }
                } while(sep <= 0.02 && i < sgt_head.len - 1);
                avg.lat /= count;
                avg.lng /= count;
                printf("Count:  %d, Average Location: %f, %f\n", count, avg.lat, avg.lng);
                i++;
            } else {
                char type[10];
                if (origin_dist[i].sgt->type == 'P') {
                    strcpy(type, "Porpoise");
                } else {
                    strcpy(type, "Dolphin");
                }
                printf("\nSingle %s:\n    Obs. lat. lng.\n", type);
                printf("    %s %f %f\n", origin_dist[i].sgt->obs.id, origin_dist[i].sgt->loc.lat, origin_dist[i].sgt->loc.lng);
                i++;
            }
        } else {
            char type[10];
            if (origin_dist[i].sgt->type == 'P') {
                strcpy(type, "Porpoise");
            } else {
                strcpy(type, "Dolphin");
            }
            printf("\nSingle %s:\n    Obs. lat. lng.\n", type);
            printf("    %s %f %f\n", origin_dist[i].sgt->obs.id, origin_dist[i].sgt->loc.lat, origin_dist[i].sgt->loc.lng);
            i++;
        } 
    printf("\nDone\n");
    }
}

我不知道该怎么做,我没有做任何关于指针的事情(我想)。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

这两行:

double gcd = great_circle(origin_dist[i].sgt->loc, origin_dist[i + 1].sgt->loc);
if (gcd <= 0.02) {

应该是:

double gcd = fabs(great_circle(origin_dist[i].sgt->loc, origin_dist[i + 1].sgt->loc));
if (gcd <= 0.02) {

以下内容包含对已发布代码的注释/注释和适度更改。     请阅读代码中嵌入的注释。因为基本     算法有问题。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

struct location
{
    float lat;
    float lng;
};

void remove_dup()
{
    int i;
    //int j; // raises compiler warning about unused variable
    Dist origin_dist[sgt_head.len];
    Sgt_Node *s_n = &sgt_head; // get address of head of linked list

    for (i = 0; i < sgt_head.len; i++)
    {
        // step to next entry in linked list
        s_n = s_n->ptr;
        // calculate distance
        // this has problem because great_circle() distance
        // does not take into account the radial of the compass
        // only the arc distance over the earths' surface
        // suggest loc.lat -origin.lat --> leg-a and loc.lng - origin.lng --> leg-b
        // and save results in origin_dist[i].dist
        origin_dist[i].dist = great_circle(s_n->sgt->loc, origin);
        // update struct  Dist array entry
        origin_dist[i].sgt = s_n->sgt;
    } // end for

    for (i = 0; i < sgt_head.len; i++)
    {
        printf("  %f %s\n", origin_dist[i].dist, origin_dist[i].sgt->obs.id);
    } // end for

    qsort(origin_dist, sgt_head.len, sizeof(Dist), comp_dist);

    printf("Sorted Array:\n");
    for (i = 0; i < sgt_head.len; i++)
    {
        printf("  %f %s\n", origin_dist[i].dist, origin_dist[i].sgt->obs.id);
    } // end for

    //int duplicate = 0; // raises warning about unused variable
    // step through local array, skipping duplicates
    for (i = 0; i < sgt_head.len;)
    {
        // suggest this line be changed to use saved leg-a and leg-b (from above)
        double sep = fabs(origin_dist[i + 1].dist - origin_dist[i].dist);

        if ((sep <= 0.02)
            &&
            (origin_dist[i + 1].sgt->type == origin_dist[i].sgt->type))
        {
            double gcd = fabs(great_circle(origin_dist[i].sgt->loc, origin_dist[i + 1].sgt->loc));
            if (gcd <= 0.02)
            {
                char type[10];
                int count = 1;
                struct location avg;
                avg.lat = 0.0;
                avg.lng = 0.0;

                if (origin_dist[i].sgt->type == 'P')
                {
                    strcpy(type, "Porpoise");
                }
                else
                {
                    strcpy(type, "Dolphins");
                } // end if

                printf("\nMultiple %s:\n    Obs. lat.      lng.\n", type);

                for(; i < (sgt_head.len -1); i++ )
                {
                    printf("    %s %f %f\n", origin_dist[i].sgt->obs.id, origin_dist[i].sgt->loc.lat, origin_dist[i].sgt->loc.lng);
                    avg.lat += origin_dist[i].sgt->loc.lat;
                    avg.lng += origin_dist[i].sgt->loc.lng;
                    count++;
                    sep = fabs(origin_dist[i + 1].dist - origin_dist[i].dist);
                    if( sep > 0.02 ) break;
                }  // end for

                avg.lat /= count;
                avg.lng /= count;
                printf("Count:  %d, Average Location: %f, %f\n", count, avg.lat, avg.lng);
                i++;
            }
            else
            {
                char type[10];
                if (origin_dist[i].sgt->type == 'P')
                {
                    strcpy(type, "Porpoise");
                }
                else
                {
                    strcpy(type, "Dolphin");
                }

                printf("\nSingle %s:\n    Obs. lat. lng.\n", type);
                printf("    %s %f %f\n", origin_dist[i].sgt->obs.id, origin_dist[i].sgt->loc.lat, origin_dist[i].sgt->loc.lng);
                i++;
            } // end if
        }
        else
        {
            char type[10];
            if (origin_dist[i].sgt->type == 'P')
            {
                strcpy(type, "Porpoise");
            }
            else
            {
                strcpy(type, "Dolphin");
            } // end if

            printf("\nSingle %s:\n    Obs. lat. lng.\n", type);
            printf("    %s %f %f\n", origin_dist[i].sgt->obs.id, origin_dist[i].sgt->loc.lat, origin_dist[i].sgt->loc.lng);
            i++;
        } // end if
    } // end for
    printf("\nDone\n");
} // end function: remove_dup