读取大小为8的valgrind无效

时间:2016-02-19 01:33:33

标签: c valgrind

我遇到了valgrind的问题。我已经在网上寻找解决方案,但没有遇到任何解决我问题的方法。由于某些内存问题,遇到的问题是输出不一致。我仔细检查了我的代码,但似乎无法找到原因。我认为当我为变量分配NULL值时,内存分配会丢失(第288行),但我很确定这不应该发生。

输出:

2015-Jun-27 12:12 PM: B's wedding
notime: (na)

2015-Jun-27 12:12 PM: PP?
notime: (na)

代码

typedef struct event{
char * description; 
char * time_str;
} event;

Status get_info(const CalComp *comp, FILE *const txtfile, event ** event, int * numbEvents){
int nEvents = 0;
Status status;
status.code = OK;


for(int i = 0; i < comp->ncomps; i++){
    if(strcmp(comp->comp[i]->name, "VEVENT") == 0){ //check for top level VEVENT

        *event = realloc(*event, sizeof(event)* (nEvents+1)); //line 286
        (*event)[nEvents].description = NULL;   //line 287
        (*event)[nEvents].time_str = NULL;      //line 288
        CalProp *temp = comp->comp[i]->prop;
        while(temp!=NULL){
            if(strcmp(temp->name, "DTSTART") == 0){ // Get the start time

                struct tm tm;
                char * buff = malloc(sizeof(char)*1000);
                if(strptime(temp->value, "%Y%m%d%H%M%S", &tm)==NULL){
                    perror("ERROR Converting time");
                    status.code = IOERR;
                }
                if(strftime(buff,1000, "%Y-%b-%d %l:%M %p",&tm)>0){
                    printf("time:(%s)\n", buff);
                    (*event)[nEvents].time_str = malloc(sizeof(char) * strlen(buff)+1);
                    strcpy((*event)[nEvents].time_str, buff);
                } else{
                    perror("ERROR Converting time");
                    status.code = IOERR;
                }
                free(buff);

            }
            if(strcmp(temp->name, "SUMMARY") == 0){ //get the summary description

                (*event)[nEvents].description = malloc(sizeof(char) * strlen(temp->value)+1);
                strcpy((*event)[nEvents].description, temp->value);
            }

            temp = temp->next;
        }
        if((*event)[nEvents].description== NULL){

            (*event)[nEvents].description = malloc(sizeof(char) * 5);
            strcpy((*event)[nEvents].description, "(na)\0");
        }
        if((*event)[nEvents].time_str == NULL){

            (*event)[nEvents].time_str= malloc(sizeof(char) * 7);
            strcpy((*event)[nEvents].time_str, "notime\0");
        }

        nEvents++;
    }

}
*numbEvents = nEvents;

for(int i = 0; i < nEvents; i++)
    printf("%s: %s\n", event[i].time_str, event[i].description);

return status;
}

错误:

==19343== Invalid write of size 8
==19343==    at 0x100001C75: get_info (main.c:288)
==19343==    by 0x100002079: calExtract (main.c:347)
==19343==    by 0x100002283: main (main.c:387)
==19343==  Address 0x100a83a78 is 0 bytes after a block of size 8 alloc'd
==19343==    at 0x10000DEA1: malloc (vg_replace_malloc.c:303)
==19343==    by 0x10000ED8C: realloc (vg_replace_malloc.c:789)
==19343==    by 0x100001C48: get_info (main.c:286)
==19343==    by 0x100002079: calExtract (main.c:347)
==19343==    by 0x100002283: main (main.c:387)

0 个答案:

没有答案