【问题标题】:My code can't reallocate memory (realloc())我的代码无法重新分配内存(realloc())
【发布时间】:2015-03-22 20:56:38
【问题描述】:

我已经要求编写代码来检查文本文件(file1.txt)中的最大单词并将所有具有该大小的单词写入另一个文本文件(file1a.txt),但它说我有一个 realloc 问题...如果我在 file1.txt 中只写一个大字,它可以工作,但是当我写 2 个或更多时,它不会通过重新分配...

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <malloc.h>

    int check_big_word(FILE *read){
        int count_letter = 0;
        int max_letter_size = 0;
        char letter;

        letter = fgetc(read);

        while(!feof(read) && letter != EOF){
            count_letter = 0;
            while(!(letter >= 'a' && letter <= 'z') || (letter >= 'A' && letter <= 'Z')){
                if (feof(read)){
                    break;
                }
                letter = fgetc(read);
            }
            while((letter >= 'a' && letter <= 'z') || (letter >= 'A' && letter <= 'Z')){
                if (feof(read)){
                    break;
                }
                count_letter++;
                letter = fgetc(read);
            }
            if( count_letter > max_letter_size ){
                max_letter_size = count_letter;
            }
        }
        rewind(read);
        return max_letter_size;
    }

    void print_all_big(FILE *read, FILE *write){
        int big_size = check_big_word(read);
        int count_letter = 0;
        int count_words = 0,i,beggining_of_string = 0;
        char letter;
        char *string_of_biggers;
        string_of_biggers = NULL;

        letter = fgetc(read);

        while(!feof(read)){
            count_letter = 0;
            while(!(letter >= 'a' && letter <= 'z') || (letter >= 'A' && letter <= 'Z')){
                if (feof(read)){
                    break;
                }
                count_words++;
                letter = fgetc(read);
            }
            while((letter >= 'a' && letter <= 'z') || (letter >= 'A' && letter <= 'Z')){
                if (feof(read)){
                    break;
                }
                count_letter++;
                letter = fgetc(read);
            }
            if(count_letter == big_size){
                if(string_of_biggers == NULL){
                    string_of_biggers = (char*)malloc(sizeof(char)*(big_size+1));
                }else{
                    //******* THE PROBLEM IS HERE *****
                    string_of_biggers = (char*)realloc(string_of_biggers, sizeof(char)*(big_size+1));
                }
                rewind(read);
                fseek(read,count_words*sizeof(char),SEEK_SET);
                for(i = 0; i<big_size;i++){
                    *string_of_biggers = fgetc(read);
                    beggining_of_string++;
                    string_of_biggers++;
                }
                *string_of_biggers = ' ';
                beggining_of_string++;
                string_of_biggers++;
            }
            count_words += count_letter;
        }
        string_of_biggers = string_of_biggers - beggining_of_string;
        fputs(string_of_biggers,write);
        fclose(read);
        fclose(write);
    }


void main(){
    FILE *r, *w;
    r = fopen("file1.txt", "rt");
    w = fopen("file1a.txt", "wt");
    print_all_big(r,w);
}

给定的错误是:

* 检测到 glibc * ./lab14_1: realloc(): 无效指针:0x000000000107649c *** ======= 回溯:========= /lib/x86_64-linux-gnu/libc.so.6(+0x7eb96)[0x7f4c0499fb96] /lib/x86_64-linux-gnu/libc.so.6(realloc+0x28e)[0x7f4c049a489e] ./lab14_1[0x400946] ./lab14_1[0x400a54] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xed)[0x7f4c0494276d] ./lab14_1[0x400689] ======= 内存映射:======== 00400000-00401000 r-xp 00000000 00:14 25693062 /home/tailedwiz/Desktop/C_programming/lab14_1 00600000-00601000 r--p 00000000 00:14 25693062 /home/tailedwiz/Desktop/C_programming/lab14_1 00601000-00602000 rw-p 00001000 00:14 25693062 /home/tailedwiz/Desktop/C_programming/lab14_1 01076000-01097000 rw-p 00000000 00:00 0 [堆] 7f4c0470b000-7f4c04720000 r-xp 00000000 08:01 11013974 /lib/x86_64-linux-gnu/libgcc_s.so.1 7f4c04720000-7f4c0491f000 ---p 00015000 08:01 11013974 /lib/x86_64-linux-gnu/libgcc_s.so.1 7f4c0491f000-7f4c04920000 r--p 00014000 08:01 11013974 /lib/x86_64-linux-gnu/libgcc_s.so.1 7f4c04920000-7f4c04921000 rw-p 00015000 08:01 11013974 /lib/x86_64-linux-gnu/libgcc_s.so.1 7f4c04921000-7f4c04ad6000 r-xp 00000000 08:01 11013953 /lib/x86_64-linux-gnu/libc-2.15.so 7f4c04ad6000-7f4c04cd6000 ---p 001b5000 08:01 11013953 /lib/x86_64-linux-gnu/libc-2.15.so 7f4c04cd6000-7f4c04cda000 r--p 001b5000 08:01 11013953 /lib/x86_64-linux-gnu/libc-2.15.so 7f4c04cda000-7f4c04cdc000 rw-p 001b9000 08:01 11013953 /lib/x86_64-linux-gnu/libc-2.15.so 7f4c04cdc000-7f4c04ce1000 rw-p 00000000 00:00 0 7f4c04ce1000-7f4c04d03000 r-xp 00000000 08:01 11013933 /lib/x86_64-linux-gnu/ld-2.15.so 7f4c04ee3000-7f4c04ee6000 rw-p 00000000 00:00 0 7f4c04eff000-7f4c04f03000 rw-p 00000000 00:00 0 7f4c04f03000-7f4c04f04000 r--p 00022000 08:01 11013933 /lib/x86_64-linux-gnu/ld-2.15.so 7f4c04f04000-7f4c04f06000 rw-p 00023000 08:01 11013933 /lib/x86_64-linux-gnu/ld-2.15.so 7fffd5c4d000-7fffd5c6e000 rw-p 00000000 00:00 0 [堆栈] 7fffd5c9b000-7fffd5c9d000 r-xp 00000000 00:00 0 [vdso] ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall] 中止(核心转储)

【问题讨论】:

    标签: file realloc


    【解决方案1】:

    当您调用 realloc 时,您必须将先前调用返回的确切指针传递给 malloc 或 realloc。你没有那样做。您正在传递相同的变量,但是您更改了变量中的指针。

    不要修改指针而是使用索引。这可以防止您的错误并使代码更具可读性。

    【讨论】:

    • 另外,不需要检查 NULL。如果将 NULL 指针传递给 realloc,它将执行 malloc
    猜你喜欢
    • 1970-01-01
    • 2020-05-01
    • 2020-01-31
    • 2018-11-10
    • 1970-01-01
    • 2021-03-16
    • 1970-01-01
    • 2018-03-26
    • 1970-01-01
    相关资源
    最近更新 更多