[置顶] 动态申请内存,释放出现错误

*** glibc detected *** ./a.out: free(): invalid next size (fast): 0x0000000000ae0010 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x7e846)[0x7fed53fd1846]
./a.out[0x4006db]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xed)[0x7fed53f7476d]
./a.out[0x400559]
======= Memory map: ========
00400000-00401000 r-xp 00000000 08:03 1179757                            /home/hlwang/tian/tmp/c/c_and_pointer_test/a.out
00600000-00601000 r--p 00000000 08:03 1179757                            /home/hlwang/tian/tmp/c/c_and_pointer_test/a.out
00601000-00602000 rw-p 00001000 08:03 1179757                            /home/hlwang/tian/tmp/c/c_and_pointer_test/a.out
00ae0000-00b01000 rw-p 00000000 00:00 0                                  [heap]
7fed53d3d000-7fed53d52000 r-xp 00000000 08:03 72284                      /lib/x86_64-linux-gnu/libgcc_s.so.1
7fed53d52000-7fed53f51000 ---p 00015000 08:03 72284                      /lib/x86_64-linux-gnu/libgcc_s.so.1
7fed53f51000-7fed53f52000 r--p 00014000 08:03 72284                      /lib/x86_64-linux-gnu/libgcc_s.so.1
7fed53f52000-7fed53f53000 rw-p 00015000 08:03 72284                      /lib/x86_64-linux-gnu/libgcc_s.so.1
7fed53f53000-7fed54108000 r-xp 00000000 08:03 28242                      /lib/x86_64-linux-gnu/libc-2.15.so
7fed54108000-7fed54307000 ---p 001b5000 08:03 28242                      /lib/x86_64-linux-gnu/libc-2.15.so
7fed54307000-7fed5430b000 r--p 001b4000 08:03 28242                      /lib/x86_64-linux-gnu/libc-2.15.so
7fed5430b000-7fed5430d000 rw-p 001b8000 08:03 28242                      /lib/x86_64-linux-gnu/libc-2.15.so
7fed5430d000-7fed54312000 rw-p 00000000 00:00 0
7fed54312000-7fed54334000 r-xp 00000000 08:03 28254                      /lib/x86_64-linux-gnu/ld-2.15.so
7fed5450f000-7fed54512000 rw-p 00000000 00:00 0
7fed54530000-7fed54534000 rw-p 00000000 00:00 0
7fed54534000-7fed54535000 r--p 00022000 08:03 28254                      /lib/x86_64-linux-gnu/ld-2.15.so
7fed54535000-7fed54537000 rw-p 00023000 08:03 28254                      /lib/x86_64-linux-gnu/ld-2.15.so
7fff8cc0e000-7fff8cc2f000 rw-p 00000000 00:00 0                          [stack]
7fff8cda0000-7fff8cda1000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
已放弃 (核心已转储)



源代码:

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

#define SIZE  10

int main(char argc, char *argv[] )
{

    int *p = NULL, *tmp;

    int i;
    p=(int*)malloc(sizeof(char)*SIZE);

    if(p == NULL)
    {
        perror("malloc failed!");
        exit(0);
    }

    tmp=p;
    for(i = 0;i<SIZE;i++){
        *p= i;
        printf("the init p==%d\n",*p);
        p++;
    }
    for(i = 0;i<SIZE;i++){                         
        printf("the result p[i]==%d\n",tmp[i]);
    }
    free(tmp);
    return 0;
}

报错:

 p=(int*)malloc(sizeof(char)*SIZE);


修改:

    p=(int*)malloc(sizeof(int)*SIZE);


解决了, 正确使用malloc 函数

你可能感兴趣的:(free,出错)