malloc和realloc的问题

时间:2018-01-27 22:11:57

标签: c malloc valgrind free calloc

当我仍然为'\0'留出空间时,为什么在大小为7的块之后我得到0字节的分配错误?

我尝试分配并重新分配7个字节并保持大小变量上升5,这样当我添加空终止符时,最后总会留下至少2个字节,但我仍然得到valgrind错误:< / p>

  

写入大小为1的无效:

     

大小为7的块之后的0字节,分配&#39; d

每当我读取或写入令牌时,例如我都会在此行中获取它:

token[i] = read;
void parse_file(char file[]) {

    char read = 0;
    int size = 5;
    char *token = NULL;
    int i = 0;
    FILE *fp = NULL;

    token = malloc(7 * sizeof(char));
    fp = fopen(file, "r");
    if(fp == NULL) {
        fprintf(stderr, "%s: No such file or directory\n", file);
        free(token);
        fclose(fp);
        return;
    }
    read = fgetc(fp);
    while(read != EOF) {
        if(i == size) {
            token = realloc(token, 7 * sizeof(char));
            size += 5;
        }
        if(isalpha(read)) {
            read = (char) tolower(read);
            token[i] = read;
        }
        else {
            if(isalpha(token[0])) {
                token[i] = '\0';
                put(token);
            }
            else {
                free(token);
            }
            token = calloc(7,sizeof(char));
            size = 5;
            i = 0;
            read = fgetc(fp);
            continue;
        }
        read = fgetc(fp);
        i++;
    }
    free(token);
    fclose(fp);

}

1 个答案:

答案 0 :(得分:0)

以下提议的代码:

  1. 干净地编译
  2. 消除了不必要的代码/逻辑
  3. 执行所需的功能
  4. 正确检查错误
  5. 将评论纳入OP问题
  6. 将评论纳入此答案
  7. 现在提议的代码:(已编辑)

    #include <stdlib.h>   // exit(), EXIT_FAILURE, realloc(), free()
    #include <stdio.h>    // FILE, fprintf(),  fopen(), fgetc(), perror()
    #include <ctype.h>    // isalpha(), tolower()
    #include <errno.h>    // errno
    #include <string.h>   // strerror()
    
    
    // prototypes
    void parse_file(char fileName[]);
    
    
    void parse_file(char fileName[])
    {
    
        int byteRead = 0;
        size_t size = 0;
        char *token = NULL;
        size_t i = 0;
        FILE *fp = NULL;
    
    
        fp = fopen(fileName, "r");
        if( !fp )
        {
            fprintf(stderr, "Can't open %s: %s\n", fileName, strerror(errno));
            exit( EXIT_FAILURE );
        }
    
    
        while( (byteRead = fgetc(fp) ) != EOF )
        {
            char *temp = NULL;
    
            if(i >= size)
            {
                temp = realloc(token, 7 + size );
                if( !temp )
                {
                    perror( "realloc failed" );
                    free( token );
                    fclose( fp );
                    exit( EXIT_FAILURE );
                }
    
                // implied else, realloc successful
    
                size += 7;
                token = temp;
            }
    
            if( isalpha(byteRead) )
            {
                byteRead = tolower(byteRead);
                token[i] = (char)byteRead;
                i++;
            }
    
            else if( i )
            {
                token[i] = '\0';
    
                puts(token);
                free( token );
    
                i = 0;
                size = 0;
            }
        }
    
        free(token);
        fclose(fp);
    }