C分割故障使用STRSEP/Strdup将C字符串操纵到结构变量中



再次使用另一个分割故障( groan )。

到达我现在的位置,我在这里有其他问题。

此功能背后的想法是:

  • 通过char*
  • 复制通过char*的复制到堆中,并指向此位置
  • 使用strsep根据','定界符
  • 将"字符串"拆分
  • 将这些拆分令牌分配给struct变量
  • 释放指针

这就是我传递给功能的内容:

gga_sentence struct:

typedef struct gga_sentence{
    const char *untouched_sentence;
    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;
    char* age_of_data_in_seconds;
    char *checksum;
}gga_sentence;

gga_parsed是:

    gga_sentence *ggas_parsed;
    ggas_parsed = malloc(10*sizeof(gga_sentence));

当线是char [100]的情况下,使用文件读取的行填充,这很好。

strncpy(&ggas_parsed[number_of_gga_parsed].untouched_sentence, line, strlen(line));
            printf("GGA UNTOUCHED: %s", &ggas_parsed[number_of_gga_parsed].untouched_sentence);

initiate_gga_values(& ggas_parsed [number_of_gga_parsed],& ggas_parsed [number_of_gga_parsed] .untouched_sentence);

上面的printf语句:

printf("GGA UNTOUCHED: %s", &ggas_parsed[number_of_gga_parsed].untouched_sentence);

生产:

GGA UNTOUCHED: $GPGGA,151019.000,5225.9627,N,00401.1624,W,1,09,1.0,38.9,M,51.1,M,,0000*72

因此,当我将其传递给上述函数时:

initiate_gga_values(&ggas_parsed[number_of_gga_parsed], &ggas_parsed[number_of_gga_parsed].untouched_sentence);

函数ITSEF定义为:

void initiate_gga_values(gga_sentence* gga_ptr, const char* sentence){
    char *temp_sentence, *second_temp_ptr;
    char *token;
    int token_no = 0;
    temp_sentence = strdup(sentence);
    second_temp_ptr = temp_sentence;
    printf("TS: %s", temp_sentence);
    printf("2nd: %s", second_temp_ptr);
    token = strsep (&second_temp_ptr,",");
    while (token != NULL) {
        /*if a sentence has missing data then make that clear by settings it's value to
         * <EMPTY>*/
        if(strlen(token)==0){
            token = "<EMPTY>";
        }
        switch(token_no){
        case 0:
            gga_ptr->sentence_id = token;
            //printf("%s,",gga_ptr->sentence_id);
            break;
        case 1:
            /*atoi converts a string to an int, well a c string anyways so a char* */
            gga_ptr->time_stamp = atoi(token);
            //printf("%d,",gga_ptr->time_stamp);
            break;
        case 2:
            /*strtod coverts a string to a double, well a c string anyways so a char* */
            gga_ptr->latitude = strtod(token, NULL);
            //printf("%f,",gga_ptr->latitude);
            break;
        case 3:
            gga_ptr->north_south_id = *token;
            //printf("%c,",gga_ptr->north_south_id);
            break;
        case 4:
            gga_ptr->longitude = strtod(token, NULL);
            //printf("%f,",gga_ptr->longitude);
            break;
        case 5:
            gga_ptr->east_west_id = *token;
            //printf("%c,",gga_ptr->east_west_id);
            break;
        case 6:
            gga_ptr->quality = atoi(token);
            //printf("%d,",gga_ptr->quality);
            break;
        case 7:
            gga_ptr->no_of_satellites = atoi(token);
            //printf("%d,",gga_ptr->no_of_satellites);
            break;
        case 8:
            gga_ptr->horizontal_dillution = strtod(token, NULL);
            //printf("%f,",gga_ptr->horizontal_dillution);
            break;
        case 9:
            gga_ptr->altitude = strtod(token, NULL);
            //printf("%f,",gga_ptr->altitude);
            break;
        case 10:
            gga_ptr->altitude_units = *token;
            //printf("%c,",gga_ptr->altitude_units);
            break;
        case 11:
            gga_ptr->geodial_seperation = strtod(token, NULL);
            //printf("%f,",gga_ptr->geodial_seperation);
            break;
        case 12:
            gga_ptr->geodial_seperation_units = *token;
            //printf("%c,",gga_ptr->geodial_seperation_units);
            break;
        case 13:
            /*This is never used in the sentenced given*/
            gga_ptr->age_of_data_in_seconds = token;
            //printf("%s,",gga_ptr->age_of_data_in_seconds);
            break;
        case 14:
            gga_ptr->checksum = token;
            //printf("%s",gga_ptr->checksum);
            break;
        }
        token_no++;
        token = strsep (&second_temp_ptr, ",");
    }
    printf("untouched: %sn", sentence);
    printf("Second print of TS: %sn", temp_sentence);
    printf("second print of second_temp: %sn", second_temp_ptr);
    free(temp_sentence);
    exit(1); //DBUGGING PURPOSES
}

此功能的输出是:

TS: $GPGGA,151019.000,5225.9627,N,00401.1624,W,1,09,1.0,38.9,M,51.1,M,,0000*72
2nd: $GPGGA,151019.000,5225.9627,N,00401.1624,W,1,09,1.0,38.9,M,51.1,M,,0000*72
untouched: $GPGGA,151019.00�(m
Second print of TS: $GPGGA
second print of second_temp: (null)

因此,如果我添加此printf语句:

printf("testing untouched: %s", gga_ptr->untouched_sentence);

进入initiate_gga_values函数,就在出口(1)之前,我会得到分段故障。用额外的打印行再次运行代码的输出,该打印行打印了gga_ptr-> untouched_sence产生:

GGA UNTOUCHED: $GPGGA,151019.000,5225.9627,N,00401.1624,W,1,09,1.0,38.9,M,51.1,M,,0000*72
TS: $GPGGA,151019.000,5225.9627,N,00401.1624,W,1,09,1.0,38.9,M,51.1,M,,0000*72
2nd: $GPGGA,151019.000,5225.9627,N,00401.1624,W,1,09,1.0,38.9,M,51.1,M,,0000*72
untouched: $GPGGA,151019.00��b
Second print of TS: $GPGGA
second print of second_temp: (null)
Segmentation fault (core dumped)

我不知道发生了什么。

有什么想法?

欢呼,克里斯。

这是错误的:

const char* untouched_sentence;
strncpy(&ggas_parsed[number_of_gga_parsed].untouched_sentence, line, strlen(line));
            printf("GGA UNTOUCHED: %s", &ggas_parsed[number_of_gga_parsed].untouched_sentence);

strncpy的第一个参数应该是将副本放入的缓冲区,您将其指向了您的结构。第三个参数应该是目标缓冲区的大小,而不是输入!

您需要做:

char* untouched_sentence;
ggas_parsed[number_of_gga_parsed].untouched_sentence = malloc(<size>);

如果您担心长行,则需要进行<size>的情况。

然后:

strncpy(ggas_parsed[number_of_gga_parsed].untouched_sentence, line, strlen(line));
printf("GGA UNTOUCHED: %s",ggas_parsed[number_of_gga_parsed].untouched_sentence);

我认为您需要从一个更简单的示例开始,以使您围绕内存管理。

相关内容

  • 没有找到相关文章

最新更新