1

当我运行我的程序时,我收到系统崩溃“分段错误”消息。

我想知道是否有办法确切知道导致系统崩溃的指令(代码行)“分段错误消息”

代码 ===>

#include "GeoIP.h"

int main()
{
    FILE *f;
    char ipAddress[30];
    char expectedCountry[3];
    char expectedCountry3[4];
    const char *returnedCountry;
    GeoIP *gi;
    int failed = 0;
    int test_num = 1;

    int i;
    for (i = 0; i < 2; ++i) {
        if (0 == i) {
            /* Read from filesystem, check for updated file */
            gi = GeoIP_open("/usr/share/GeoIP/GeoIP.dat",
                            GEOIP_STANDARD | GEOIP_CHECK_CACHE);
        } else {
            /* Read from memory, faster but takes up more memory */
            gi = GeoIP_open("/usr/share/GeoIP/GeoIP.dat", GEOIP_MEMORY_CACHE);
        }

        if (gi == NULL) {
            fprintf(stderr, "Error opening database\n");
            exit(1);
        }

        /* make sure GeoIP deals with invalid query gracefully */
        returnedCountry = GeoIP_country_code_by_addr(gi, NULL);
        if (returnedCountry != NULL) {
            fprintf(stderr,
                    "Invalid Query test failed, got non NULL, expected NULL\n");
            failed = 1;
        }

        returnedCountry = GeoIP_country_code_by_name(gi, NULL);
        if (returnedCountry != NULL) {
            fprintf(stderr,
                    "Invalid Query test failed, got non NULL, expected NULL\n");
            failed = 1;
        }

        f = fopen("/home/aa/test/country_test.txt", "r");

        while (fscanf(f, "%s%s%s", ipAddress, expectedCountry, expectedCountry3)
               != EOF) {
            returnedCountry = GeoIP_country_code_by_addr(gi, ipAddress);
            if (returnedCountry == NULL
                || strcmp(returnedCountry, expectedCountry) != 0) {
                fprintf(stderr,
                        "Test addr %d for %s failed, got %s, expected %s\n",
                        test_num, ipAddress, returnedCountry, expectedCountry);
                failed = 1;
            }
            returnedCountry = GeoIP_country_code_by_name(gi, ipAddress);
            if (returnedCountry == NULL
                || strcmp(returnedCountry, expectedCountry) != 0) {
                fprintf(stderr,
                        "Test name %d for %s failed, got %s, expected %s\n",
                        test_num, ipAddress, returnedCountry, expectedCountry);
                failed = 1;
            }
            returnedCountry = GeoIP_country_code3_by_addr(gi, ipAddress);
            if (returnedCountry == NULL
                || strcmp(returnedCountry, expectedCountry3) != 0) {
                fprintf(stderr,
                        "Test addr %d for %s failed, got %s, expected %s\n",
                        test_num, ipAddress, returnedCountry, expectedCountry);
                failed = 1;
            }
            returnedCountry = GeoIP_country_code3_by_name(gi, ipAddress);
            if (returnedCountry == NULL
                || strcmp(returnedCountry, expectedCountry3) != 0) {
                fprintf(stderr,
                        "Test name %d for %s failed, got %s, expected %s\n",
                        test_num, ipAddress, returnedCountry, expectedCountry);
                failed = 1;
            }
            test_num++;
        }
        fclose(f);

        f = fopen( "/home/aa/test/country_test2.txt", "r");
        while (fscanf(f, "%s%s", ipAddress, expectedCountry) != EOF) {
            returnedCountry = GeoIP_country_code_by_addr(gi, ipAddress);
            if (returnedCountry == NULL
                || strcmp(returnedCountry, expectedCountry) != 0) {
                fprintf(stderr, "Test addr %d %s failed, got %s, expected %s\n",
                        test_num, ipAddress, returnedCountry, expectedCountry);
                failed = 1;
            }
            test_num++;
        }
        fclose(f);

        f = fopen( "/home/aa/test/country_test_name.txt", "r");
        while (fscanf(f, "%s%s", ipAddress, expectedCountry) != EOF) {
            returnedCountry = GeoIP_country_code_by_name(gi, ipAddress);
            if (returnedCountry == NULL
                || strcmp(returnedCountry, expectedCountry) != 0) {
                fprintf(stderr, "Test addr %d %s failed, got %s, expected %s\n",
                        test_num, ipAddress, returnedCountry, expectedCountry);
                failed = 1;
            }
            test_num++;
        }

        fclose(f);
        GeoIP_delete(gi);
    }
    return failed;
}

谢谢

4

2 回答 2

8

正如许多人所说:使用调试器或类似valgrind.

然而,这绝对可能是问题的根源(我已经删除了不感兴趣的部分):

if (returnedCountry == NULL || ...) {
    fprintf(stderr, ".. %s ..\n", returnedCountry...);
    ...
}

有几个这样的例子。您还有fopen不检查返回值的调用。总是这样做。

于 2013-07-11T13:04:04.883 回答
2

添加-g到您的编译命令行,然后您可以自由使用gdb./a.outvalgrind ./a.out

(这./a.out是您的程序名称)

于 2013-07-11T12:58:29.360 回答