尝试释放循环缓冲区时,出现断言错误(表达式: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和frees的代码块:
#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);

最佳答案

free(&cb1);

您试图释放cb1所在的内存,但是
static cbuf cb1 = {INITIAL_SIZE, 0, 0, 0, NULL};

但这并不意味着。
free(cb1.quotes)

是你需要释放的。

08-16 20:19