我有一个 3 void* 的结构:
typedef struct ck{
void * arg1;
void * arg2;
void * arg3;
} argCookie;
后来我实例化了argCookie:
argCookie myCookie;
然后我为它的论点赋予一些价值:
unsigned long size;
//give a value to size
unsigned short array[size];
//fill the array
myCookie.arg1=malloc(sizeof(size));
myCookie.arg2=malloc(sizeof(array));
memcpy(myCookie.arg1,&size,sizeof(size));
memcpy(myCookie.arg2,array,sizeof(array));
到目前为止一切顺利,我现在可以从我的结构中访问数组和大小,没问题。当我尝试释放已分配的内存时会出现问题:
free(myCookie.arg1);
free(myCookie.arg2);//something goes bad when this line of code is executed don't know why
free(myCookie.arg3);
myCookie.arg1=NULL;
myCookie.arg2=NULL;
myCookie.arg3=NULL;
当行“free(myCookie.arg2);” 执行我收到以下消息:
“恐慌:断言“(char *)NextSlot(prev)<= p”失败,文件“/usr/src/lib/libc/ansi/malloc.c”,第252行
syslib:panic.c: 堆栈跟踪: 0x468b 0x53b4 0x975a 0x2a33 0x2582 0x100a"
有谁知道为什么会这样?
谢谢