我的代码无法重新分配内存(realloc())

时间:2015-01-23 18:26:20

标签: file realloc

我已经要求编写检查文本文件(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 ***       ======= Backtrace:=========       /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 [stack]       7fffd5c9b000-7fffd5c9d000 r-xp 00000000 00:00 0 [vdso]       ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]       中止(核心倾销)

1 个答案:

答案 0 :(得分:0)

调用realloc时,必须传递上一次调用malloc或realloc返回的确切指针。你不是那样做的。您正在传递相同的变量,但您已更改变量中的指针。

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