原版
static void ClearAll(CalcContext *calcContext){
ClearBuffer(calcContext);
calcContext->result = 0;
memset(&calcContext->previous_operation,0,sizeof(Operation));
memset(&calcContext->current_operation,0,sizeof(Operation));
}
这个版本运行的时候就会出错:
malloc.c:2617: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)’ failed.
修改后
static void ClearAll(CalcContext *calcContext){
ClearBuffer(calcContext);
calcContext->result = 0;
memset(&calcContext->previous_operation,0,sizeof(Operation)/sizeof(int ));
memset(&calcContext->current_operation,0,sizeof(Operation)/sizeof(int));
}
这个样子就不会了
环境是:ubuntu 64位 22版本 虚拟机环境
编译器是 gcc
内存:4g
尝试用了valgrind 这个工具,有点看不明白,但是原版的版本会显示 memset 的那段代码 Invalid write of size 8,新版就没有。。。