C segmentation fault, can anybody help me identify why? -
i using following code split char array in c:
char line[100]; int number_of_gga_parsed = 0; int gga_blocks_allocated; gga_sentence *ggas_parsed; gsa_sentence gsa; gsv_sentence gsv; ggas_parsed = malloc(10*sizeof(gga_sentence)); gga_blocks_allocated = 10; if(ggas_parsed == null){ printf("error allocating memory, system exiting.\n"); exit(exit_failure); } while(fscanf(stream, "%s", line)!= eof){ strcpy((ggas_parsed + number_of_gga_parsed)->untouched_sentence, line); initiate_gga_values((ggas_parsed + number_of_gga_parsed),(ggas_parsed + number_of_gga_parsed)->untouched_sentence); }
initiate_gga_values function:
void initiate_gga_values(gga_sentence* gga_ptr, const char* sentence){ char *temp_sentence; char *token; int token_no = 0; /*copy gga_sentence temp_sentence char array*/ strcpy(temp_sentence, sentence); token = strsep (temp_sentence,","); while (token != null) { switch(token_no){ case 0: gga_ptr->sentence_id = token; break; case 1: /*atoi converts string int, c string anyways char* */ gga_ptr->time_stamp = atoi(token); break; case 2: /*strtod coverts string double, c string anyways char* */ gga_ptr->latitude = strtod(token, null); break; case 3: gga_ptr->north_south_id = (char)token; break; case 4: gga_ptr->longitude = strtod(token, null); break; case 5: gga_ptr->east_west_id = (char)token; break; case 6: gga_ptr->quality = atoi(token); break; case 7: gga_ptr->no_of_satellites = atoi(token); break; case 8: gga_ptr->horizontal_dillution = strtod(token, null); break; case 9: gga_ptr->altitude = strtod(token, null); break; case 10: gga_ptr->altitude_units = (char)token; break; case 11: gga_ptr->geodial_seperation = strtod(token, null); break; case 12: gga_ptr->geodial_seperation_units = (char)token; break; case 13: gga_ptr->age_of_data_in_seconds = strtod(token, null); break; case 14: gga_ptr->checksum = token; break; } token_no++; token = strsep (temp_sentence, ","); } } the char array consists of information:
$gpgga,151019.000,5225.9627,n,00401.1624,w,1,09,1.0,38.9,m,51.1,m,,0000*72 but when code in run on above, segmentation fault:
segmentation fault (core dumped) gga struct:
typedef struct gga_sentence{ char untouched_sentence[100]; gsa_sentence gsa; char *sentence_id; int time_stamp; double latitude; char north_south_id; double longitude; char east_west_id; int quality; int no_of_satellites; double horizontal_dillution; double altitude; char altitude_units; double geodial_seperation; char geodial_seperation_units; double age_of_data_in_seconds; char *checksum; }gga_sentence; the segmentation fault happens before function initiate_gga_values function completes, or starts really. means assume there problem mallocing of gga_sentences cannot see where!
this first time using strsep, no doubt doing wrong.
gdb error message:
program received signal sigsegv, segmentation fault. 0x00007ffff7de4103 in _dl_lookup_symbol_x (undef_name=0x4004d8 "strlen", undef_map=0x7ffff7ffe268, ref=ref@entry=0x7fffffffdbb8, symbol_scope=0x302e312c362e312c, version=0x7ffff7ff9a08, type_class=type_class@entry=1, flags=1, skip_map=skip_map@entry=0x0) @ dl-lookup.c:733 733 dl-lookup.c: no such file or directory. cheers, chris.
char *temp_sentence; char *token; int token_no = 0; /*copy gga_sentence temp_sentence char array*/ strcpy(temp_sentence, sentence); you aren't allocating memory temp_sentence hold copied string
Comments
Post a Comment