标题Error in `./a.out’: munmap_chunk(): invalid pointer: 0x00007ffe98820bad
invalid pointer是无效指针,在CSDN博客里面找答案,看到new的问题中说到“指针中间经过了改动”。我返回看到了下代码:
#include “stdio.h”
#include “malloc.h”
int main(void)
{
char str[] = “hello word”;
char *p1 = (char *)malloc(32);
if(p1==NULL)
{
printf(“error”);
return 0;
}
p1 = str;
printf(“len=%ld\nstr=%s\n”,sizeof(p1),p1);
free(p1);
return 0;
}
发现**p1=str;**这里申请的指针地址被改掉了
结论:用malloc申请的是一个动态内存指针,而p1是指向该内存的地址,所以在程序运行中不能修改地址例:p1++,p1=str等都是错误的。