来自char指针数组的可用内存

时间:2017-10-09 00:45:04

标签: c

我编写了一个程序,最多可以包含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

1 个答案:

答案 0 :(得分:3)

您尚未将指针初始化为NULL

char *username[SIZE] = {0};

你可以双重释放第一个元素

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