错误从结构内的双指针释放内存

时间:2020-03-04 10:46:40

标签: c gcc

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

struct Exploded {
    char ** tokens;
    int rows;
    int cols;
};

void resetExploded(struct Exploded *exploded)
{
    int i;
    for(i = 0; i < exploded->rows; i++)
    {
        if(exploded->tokens[i] != NULL) {
            free(exploded->tokens[i]);
            exploded->tokens[i] = NULL;
        }
    }

    free(exploded->tokens);

    exploded->tokens = NULL;
    exploded->rows = 0;
    exploded->cols = 0;
}


void explode(char *str, char delim, struct Exploded *exploded)
{
    resetExploded(exploded);
    int i;
    char str_delim[2];
    char *ptr;
    str_delim[0] = delim;
    str_delim[1] = 0; 

    for(i = 0; i < strlen(str); i++)
    {
        if(str[i] == delim)
            exploded->rows++;
    }

    exploded->rows++;

    exploded->tokens = malloc(exploded->rows * sizeof(char *));

    if(exploded->tokens == NULL) {
        perror("more details"); 
        return;
    }

    i = 0;
    ptr = strtok(str, str_delim);
    while(ptr != NULL)
    {
        if(exploded->cols < strlen(ptr) + 1)
        {
            exploded->cols = strlen(ptr) + 1;
        }
        exploded->tokens[i] = malloc(exploded->cols * sizeof(char));
        if(exploded->tokens[i] == NULL) {
            perror("more details");
            return;
        }
        exploded->tokens[i] = ptr;
        i++;
        ptr = strtok(NULL, str_delim); 
    }
}


int main()
{
    struct Exploded exploded = { NULL, 0, 0 };
    char str[] = "I was handling this nicely";
    explode(str, ' ', &exploded);
    int i = 0;
    printf("rows: %d, cols: %d\n", exploded.rows, exploded.cols);
    for(i = 0; i < exploded.rows; i++)
    {
        printf("here %d: %s\n", i, exploded.tokens[i]);
    }

    char str2[] = "I have no ideea what the heck is wrong";
    explode(str2, ' ', &exploded);
    i = 0;
    printf("rows: %d, cols: %d\n", exploded.rows, exploded.cols);
    for(i = 0; i < exploded.rows; i++)
    {
        printf("here %d: %s\n", i, exploded.tokens[i]);
    }

    return 0;
}

我为此苦了2天。内存是从堆分配的,应该释放,但是我的编译器说我做错了。谁能解释一下为什么会崩溃?不确定在Windows 7和MX Linux 32位上崩溃...但是我在更大的程序中的Windows 10上运行了这段代码,但没有崩溃。我认为在Windows 7上有mingw 8.1,而MX linux则有gcc 6.3。我有来自Linux的这份报告:

$ ./explode
rows: 5, cols: 9
here 0: I
here 1: was
here 2: handling
here 3: this
here 4: nicely
*** Error in `./explode': munmap_chunk(): invalid pointer: 0xbfbce665 ***
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x6738a)[0xb7d8338a]
/lib/i386-linux-gnu/libc.so.6(+0x6dfc7)[0xb7d89fc7]
/lib/i386-linux-gnu/libc.so.6(+0x6e6c1)[0xb7d8a6c1]
./explode(+0x6d6)[0x41e6d6]
./explode(+0x753)[0x41e753]
./explode(main+0x12f)[0x41e9f9]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf6)[0xb7d34286]
./explode(+0x551)[0x41e551]
======= Memory map: ========
0041e000-0041f000 r-xp 00000000 07:01 9302       /home/demo/Documents/explode
0041f000-00420000 r--p 00000000 07:01 9302       /home/demo/Documents/explode
00420000-00421000 rw-p 00001000 07:01 9302       /home/demo/Documents/explode
00483000-004a4000 rw-p 00000000 00:00 0          [heap]
b7ce6000-b7d02000 r-xp 00000000 07:00 5649       /lib/i386-linux-gnu/libgcc_s.so.1
b7d02000-b7d03000 r--p 0001b000 07:00 5649       /lib/i386-linux-gnu/libgcc_s.so.1
b7d03000-b7d04000 rw-p 0001c000 07:00 5649       /lib/i386-linux-gnu/libgcc_s.so.1
b7d1c000-b7ecd000 r-xp 00000000 07:00 5616       /lib/i386-linux-gnu/libc-2.24.so
b7ecd000-b7ecf000 r--p 001b0000 07:00 5616       /lib/i386-linux-gnu/libc-2.24.so
b7ecf000-b7ed0000 rw-p 001b2000 07:00 5616       /lib/i386-linux-gnu/libc-2.24.so
b7ed0000-b7ed3000 rw-p 00000000 00:00 0 
b7eea000-b7eee000 rw-p 00000000 00:00 0 
b7eee000-b7ef1000 r--p 00000000 00:00 0          [vvar]
b7ef1000-b7ef3000 r-xp 00000000 00:00 0          [vdso]
b7ef3000-b7f16000 r-xp 00000000 07:00 5592       /lib/i386-linux-gnu/ld-2.24.so
b7f16000-b7f17000 r--p 00022000 07:00 5592       /lib/i386-linux-gnu/ld-2.24.so
b7f17000-b7f18000 rw-p 00023000 07:00 5592       /lib/i386-linux-gnu/ld-2.24.so
bfbb0000-bfbd1000 rw-p 00000000 00:00 0          [stack]
Aborted

0 个答案:

没有答案
相关问题