linux - Segmentation fault in C -


when run program system crash "segmentation fault" message.

i want know if there way know instruction (code line) caused system crash segmentation fault message"

code ===>

#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;     (i = 0; < 2; ++i) {         if (0 == i) {             /* read filesystem, check updated file */             gi = geoip_open("/usr/share/geoip/geoip.dat",                             geoip_standard | geoip_check_cache);         } else {             /* read memory, faster takes 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 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 %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 %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 %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 %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; } 

thanks

as many people stated: use debugger or tool valgrind.

however might source of problems (i've stripped non-interesting parts):

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

there several instances this. have fopen calls don't check return value. always that.


Comments