当我尝试释放我的循环缓冲区时,出现断言错误(表达式:crtisvalidheappointer)。这是为什么发生?不能释放内存(断言错误)
相关结构:
的代码typedef struct quote {
unsigned int seconds;
double rate;
} quote;
typedef struct cbuf {
unsigned int max;
unsigned int start;
unsigned int end;
unsigned int size;
quote *quotes;
} cbuf;
块是mallocs并释放:
#define INITIAL_SIZE 10
static cbuf cb1 = {INITIAL_SIZE, 0, 0, 0, NULL};
cb1.quotes = (quote*)malloc(INITIAL_SIZE * sizeof(quote));
if(cb1.quotes == NULL)
{
printf("Error - memory allocation failed.");
exit(1);
}
free(&cb1);
你释放一个局部堆栈变量。 SideNote:在编程时不要强制使用'malloc()'。这是一种坏习惯。 – WhozCraig 2013-04-03 20:02:08
你想'免费(&(cb1.quotes))'因为'cb1.quotes'就是你分配的内存 – maditya 2013-04-03 20:02:27