2015-01-23 3 views
0

텍스트 파일 (file1.txt)의 가장 큰 단어를 검사하고 그 크기의 모든 단어를 다른 텍스트 파일 (file1a .txt)로하지만 ... 나는 그것이 작동은 File2.txt에 하나의 큰 단어를 쓰기하지만 2 개 이상을 쓸 때 늘 재 할당을 통과하면 내가 작업 ... realloc을 문제가 있다고내 코드가 메모리를 재 할당 할 수 없습니다. (realloc())

#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 [heap] 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-7f 4c04cdc000 RW-P 001b9000 8시 1분 11,013,953 /lib/x86_64-linux-gnu/libc-2.15.so 7f4c04cdc000-7f4c04ce1000 RW-P 00000000 0시 0 7f4c04ce1000-7f4c04d03000 R-XP 8시 1분 00,000,000 11,013,933/LIB/x86_64에-리눅스 GNU/ld-2.15.so 7f4c04ee3000-7f4c04ee6000 RW-P 00000000 0시 0 7f4c04eff000-7f4c04f03000 RW-P 00000000 0시 0 7f4c04f03000-7f4c04f04000 R - P 8시 1분 00,022,000 11,013,933/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] 중단됨 (코어 덤프 됨)

답변

0

realloc을 호출하면 이전에 malloc 또는 realloc을 호출하여 반환 된 정확한 포인터를 전달해야합니다. 너는 그렇게하지 않는다. 동일한 변수를 전달하지만 변수에서 포인터를 변경했습니다.

포인터를 수정하지 말고 색인을 사용하십시오. 그러면 버그가 방지되고 코드가 훨씬 더 읽기 쉽습니다.

+0

또한 NULL을 확인하지 않아도됩니다. realloc에 ​​NULL 포인터를 전달하면, malloc을 수행합니다 –