一个Stack实现,不知哪有内存泄漏
测试失败,纠缠了好久,不知怎么就内存泄漏了?谢谢!
测试结果:
程序代码:
0x003f51a0 : D:\workspace\collection\stack.c:51 ERROR: testPush leaked 1 block(s) testPush: Test failed. loop 1EXCEPTION_ACCESS_VIOLATION occurred at 0x102dbbbf. testPush_teardown: Test failed. 1 out of 1 tests failed! testPush Blocks allocated... 0x003f40f0 : D:\workspace\collection\stack.c:29 ERROR: run_tests leaked 1 block(s) Process returned -1 (0xFFFFFFFF) execution time : 0.016 s Press any key to continue.
stack.h
程序代码:
#ifndef STACK_H_INCLUDED #define STACK_H_INCLUDED typedef struct StackStc *Stack; typedef struct StackNodeStc *StackNode; /* typedef struct StackNodeStc{ void *x; struct StackNodeStc *link; }*StackNode; typedef struct StackStc{ int size; struct StackNodeStc *top; }*Stack; */ Stack stack_create(void); Boolean stack_empty(Stack stk); int stack_size(Stack stk); void stack_push(Stack stk, void *x); void* stack_pop(Stack stk); void stack_destory(Stack *pstk); #endif // STACK_H_INCLUDED
stack.c
程序代码:
#include<stdio.h> #include<stdlib.h> #include<assert.h> #include "config.h" #include "stack.h" //========cmockery test================={ #if UNIT_TESTING extern void* _test_malloc(const size_t size, const char* file, const int line); extern void* _test_calloc(const size_t number_of_elements, const size_t size, const char* file, const int line); extern void _test_free(void* const ptr, const char* file, const int line); #define malloc(size) _test_malloc(size, __FILE__, __LINE__) #define calloc(num, size) _test_calloc(num, size, __FILE__, __LINE__) #define free(ptr) _test_free(ptr, __FILE__, __LINE__) #endif // UNIT_TESTING //========cmockery test=================} struct StackNodeStc{ void *x; struct StackNodeStc *link; }; struct StackStc{ int size; struct StackNodeStc *top; }; Stack stack_create(void){ Stack stk=(Stack)malloc(sizeof(struct StackStc)); if (!stk){ exit(OVERFLOW); } stk->size=0; stk->top=NULL; return stk; } Boolean stack_empty(Stack stk){ assert(stk); return stk->size==0; } int stack_size(Stack stk){ assert(stk); return stk->size; } void stack_push(Stack stk, void *x){ assert(stk); StackNode item=(StackNode)malloc(sizeof(struct StackNodeStc)); if(!item){ exit(OVERFLOW); } item->x=x; item->link=stk->top; stk->top=item; stk->size++; } void* stack_pop(Stack stk){ assert(stk); assert(stk->size>0); StackNode item=stk->top; stk->top=item->link; stk->size--; void* x=item->x; free(item); return x; } void stack_destory(Stack *pstk){ assert(pstk && *pstk); StackNode item; int i=1; for(item=(*pstk)->top;item;item=(*pstk)->top->link){ printf("loop %d",i++); free(item); } free(*pstk); }
config.h
程序代码:
#ifndef CONFIG_H #define CONFIG_H // 函数结果状态代码 #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 #ifndef OVERFLOW #define OVERFLOW -2 #endif typedef int Status; // Status是函数的类型,其值是函数结果状态代码,如OK等 */ typedef int Boolean; // Boolean是布尔类型,其值是TRUE或FALSE */ //单元测试开关 #define UNIT_TESTING 1 #endif
stack_tests.c
程序代码:
#include <google/cmockery.h> #include "config.h" #include "stack.h" void setup(void **state) { Stack stk=stack_create(); *state=(void*)stk; } void teardown(void **state) { Stack stk = *state; stack_destory(&stk); } void testPush(void **state) { int i=1; Stack const stk = *state; assert_true(stack_empty(stk)); stack_push(stk,&i); assert_int_equal(1,stack_size(stk)); } int main(void) { const UnitTest tests[] = { unit_test_setup_teardown(testPush, setup,teardown), }; return run_tests(tests); }
[ 本帖最后由 江楚玥 于 2011-1-20 11:18 编辑 ]