2017-10-09 47 views
-2

我寫了一個程序,最多需要100個字並將它們存儲在一個數組中。然後計算單詞的平均長度並將結果打印到stderr(在其他一些基本計算中)。我試圖在程序結束時釋放內存,但得到這個錯誤,不知道爲什麼這是一個無效的指針?從字符指針數組中釋放內存

碼 -

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

void *emalloc(size_t s) { 
    void *result = malloc(s); 
    if (NULL == result) { 
     fprintf(stderr, "Memory allocation failed!\n"); 
     exit(EXIT_FAILURE); 
    } 
    return result; 
} 

/* n is the size of the array */ 
void print_array(char **a, int n, double average) { 
    if (n == 0) { 
     /* do nothing */ 
    } else { 
     if (strlen(a[0]) > average) { 
      fprintf(stdout, "%s\n", a[0]); 
     } 
     print_array(a + 1, n - 1, average); 
    } 
} 

int main() 
{ 
    #define SIZE 100 
    char *username[SIZE]; 
    char word[80]; 
    int num_words = 0; 
    double average = 0.0; 
    int p; 

    /* Read words into array */ 
    while(num_words < SIZE && 1 == scanf("%s", word)) { 
     size_t len = strlen (word); 
     username[num_words] = emalloc (len + 1); 
     strcpy(username[num_words], word); 
     num_words++; 
     average += len; 
    } 

    average = average/num_words; 

    print_array(username, num_words, average); 

    if (average > 0) { 
     fprintf(stderr, "%.2f\n", average); 
    } 

    /* FREE MEMORY */ 
    for (p = 0; username[p]; p++) { 
     free(username[p]); 
    } 
    free(*username); 

    return EXIT_SUCCESS; 
} 

錯誤 -

*** Error in `./task': free(): invalid pointer: 0x00007f55a2ee6998 *** 
======= Backtrace: ========= 
/lib64/libc.so.6(+0x791fb)[0x7f55a29731fb] 
/lib64/libc.so.6(+0x8288a)[0x7f55a297c88a] 
/lib64/libc.so.6(cfree+0x4c)[0x7f55a29802bc] 
./task[0x4007f9] 
/lib64/libc.so.6(__libc_start_main+0xf1)[0x7f55a291a401] 
./task[0x40085a] 
======= Memory map: ======== 
00400000-00401000 r-xp 00000000 00:31 433193010       /home/cshome/h/hmead/242/lab07/task 
00600000-00601000 r--p 00000000 00:31 433193010       /home/cshome/h/hmead/242/lab07/task 
00601000-00602000 rw-p 00001000 00:31 433193010       /home/cshome/h/hmead/242/lab07/task 
7f559c000000-7f559c021000 rw-p 00000000 00:00 0 
7f559c021000-7f55a0000000 ---p 00000000 00:00 0 
7f55a26e3000-7f55a26f9000 r-xp 00000000 08:06 100663402     /usr/lib64/libgcc_s-6.3.1-20161221.so.1 
7f55a26f9000-7f55a28f8000 ---p 00016000 08:06 100663402     /usr/lib64/libgcc_s-6.3.1-20161221.so.1 
7f55a28f8000-7f55a28f9000 r--p 00015000 08:06 100663402     /usr/lib64/libgcc_s-6.3.1-20161221.so.1 
7f55a28f9000-7f55a28fa000 rw-p 00016000 08:06 100663402     /usr/lib64/libgcc_s-6.3.1-20161221.so.1 
7f55a28fa000-7f55a2ab7000 r-xp 00000000 08:06 100806032     /usr/lib64/libc-2.24.so 
7f55a2ab7000-7f55a2cb6000 ---p 001bd000 08:06 100806032     /usr/lib64/libc-2.24.so 
7f55a2cb6000-7f55a2cba000 r--p 001bc000 08:06 100806032     /usr/lib64/libc-2.24.so 
7f55a2cba000-7f55a2cbc000 rw-p 001c0000 08:06 100806032     /usr/lib64/libc-2.24.so 
7f55a2cbc000-7f55a2cc0000 rw-p 00000000 00:00 0 
7f55a2cc0000-7f55a2ce5000 r-xp 00000000 08:06 100806025     /usr/lib64/ld-2.24.so 
7f55a2dbd000-7f55a2ec0000 rw-p 00000000 00:00 0 
7f55a2ee2000-7f55a2ee5000 rw-p 00000000 00:00 0 
7f55a2ee5000-7f55a2ee6000 r--p 00025000 08:06 100806025     /usr/lib64/ld-2.24.so 
7f55a2ee6000-7f55a2ee7000 rw-p 00026000 08:06 100806025     /usr/lib64/ld-2.24.so 
7f55a2ee7000-7f55a2ee8000 rw-p 00000000 00:00 0 
7ffc89617000-7ffc89638000 rw-p 00000000 00:00 0       [stack] 
7ffc89739000-7ffc8973b000 r--p 00000000 00:00 0       [vvar] 
7ffc8973b000-7ffc8973d000 r-xp 00000000 00:00 0       [vdso] 
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0     [vsyscall] 
/home/cshome/coursework/241/bin/checkprac1: line 44: 4467 Aborted     (core dumped) ./$prog < ${indir}/${file} >| ${tmpfile}.stdout 2>| ${tmpfile}.stderr 
+3

'for(p = 0;用戶名[P]; p ++){ free(username [p]); (* p; 0; p BLUEPIXY

回答

3

您還沒有初始化指針爲NULL

char *username[SIZE] = {0}; 

你是雙釋放第一要素

for (p = 0; username[p]; p++) { 
    free(username[p]); 
} 
// then get rid of this line.... 
// free(*username); 
+0

爲了我自己的理解,你能解釋一下爲什麼有必要初始化爲NULL? –

+0

假設以「NULL」結尾,您正在循環訪問'username'數組,但是,您將垃圾值留在末尾而不是您嘗試「釋放」的位置。 – kamoroso94

+2

這個for循環很奇怪。它不檢查上限 – Amadeus