在C语言双免费或中止腐败(!上一页)(核心转储)错误

问题描述 投票:0回答:1
int get_rates(Date start_at, Date end_at,unsigned n_currencies, char *currencies[], Rate *result){
    char link[1024];
    char * simbolos=envirgular(currencies,n_currencies);
    char * linkp=link;
    snprintf(link, sizeof(link),  "https://api.exchangeratesapi.io/history?start_at=%d-%d-%d&end_at=%d-%d-%d&symbols=%s"
    ,start_at.year,start_at.month,start_at.day,end_at.year,end_at.month,end_at.day,simbolos);
    start_at.day++;
    size_t datasize;
    char *a=http_get_data(linkp,&datasize);
    int numerodedias=getnumberofdays(a);
    result=realloc(result,numerodedias*sizeof(Rate));
    Date datas[numerodedias];
    char * info=strstr(a,"rates");
    char * data=malloc(10*sizeof(char));
    char * year=malloc(4*sizeof(char));
    char * day_month=malloc(2*sizeof(char));
    char * value=malloc(6*sizeof(char));
    for(int j=0;j<numerodedias;j++){
        result[j].values=calloc(n_currencies,sizeof(char*));
        result[j].names=calloc(n_currencies,sizeof(char*));
        info=strstr(info,"-");
        info=info-4;
        strncpy(data,info,10);
        strncpy(year,data,4);
        datas[j].year=atoi(year);
        data=data+5;
        strncpy(day_month,data,2);
        datas[j].month=atoi(day_month);
        data=data+3;
        strncpy(day_month,data,2);
        datas[j].day=atoi(day_month);
        for(int i=0;i<n_currencies;i++){
            info=strstr(info,currencies[i]);
            info=info+5;
            strncpy(value,info,6);
            result[j].values[i]=value;
            result[j].names[i]=currencies[i];





    }
    for(int i=0;i<2;i++){
    printf("\nValor%s\nMoeda%s\n",result[j].names[i],result[j].values[i]);}
    result[j].data.day=datas[j].day;
    result[j].data.month=datas[j].month;
    result[j].data.year=datas[j].year;
    //printf("\nDia:%d-%d-%d   %s=%s    %s=%s\n",result[j].data.day,result[j].data.month,result[j].data.year,result[j].names[0],result[j].values[0],result[j].names[1],result[j].values[1]);
        }

    return 0;
}

typedef struct Data{
    int year;
    int month;
    int day;

}Date;


typedef struct SingleRate{
    Date  data;
    char **names;
    char **values;
}Rate;

您好堆栈溢出社区,我学习C语言在我的课,我被要求做这个小项目,使用的libcurl关于某些货币和价值确定日的一些信息在网站中搜索。我到了一个点,我可以继续,由于这个错误,我一直在互联网上搜索了相同的错误的答案,但我似乎didnt找到它在哪里会很高兴,如果有人能向我解释做什么我错了谢谢。

c compiler-errors segmentation-fault corruption double-free
1个回答
0
投票

警告 :

char * year=malloc(4*sizeof(char));
...
strncpy(year,data,4);
datas[j].year=atoi(year);

今年有结束的空字符没有到位,它是在这种情况下丢失的,具有的atoi一个未定义的行为

同为day_month

char * day_month=malloc(2*sizeof(char));
...
strncpy(day_month,data,2);
datas[j].month=atoi(day_month);

其他可能的问题时:

        result[j].values[i]=value;

你总是相同的char *复制在一个循环中,如果你想你会有不同的值,这是假的。目前它没有结束的空字符,因为通过函数strncpy设定的风险。可能你也将释放它以后(char * value=malloc(6*sizeof(char));)。所以,你可能得的strdup,但与丢失的空字符警告

© www.soinside.com 2019 - 2024. All rights reserved.