| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 783 人关注过本帖
标题:一个Stack实现,不知哪有内存泄漏
只看楼主 加入收藏
江楚玥
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2011-1-20
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:5 
一个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 编辑 ]
搜索更多相关主题的帖子: 内存 泄漏 allocated occurred failed 
2011-01-20 11:02
江楚玥
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2011-1-20
收藏
得分:0 
非常奇怪的是我将所有代码放在一个文件里,测试就可以通过了。
程序代码:
testPush: Starting test
testPush: Test completed successfully.
loop 1All 1 tests passed

Process returned 0 (0x0)   execution time : 0.016 s


stack.c
程序代码:
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>

#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <google/cmockery.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=================}


// 函数结果状态代码
#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 */

typedef struct StackNodeStc{
    void *x;
    struct StackNodeStc *next;
}*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);

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;
    if (stk->size>0){
        item->next=stk->top;
    }else{
        item->next=NULL;
    }
    stk->top=item;
    stk->size++;
}

void* stack_pop(Stack stk){
    assert(stk);
    assert(stk->size>0);

    StackNode item=stk->top;
    stk->top=item->next;
    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->next){
        printf("loop %d",i++);
        free(item);
    }
    free(*pstk);
}

//********************************************************
//×××××××××× 测试开始 ×××××××××××××××××××××××××××××××××××*
//********************************************************

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-01-20 13:34
刘定邦
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
帖 子:687
专家分:1570
注 册:2010-9-21
收藏
得分:0 
好难..不懂.
2011-01-20 14:01
逐渐学习
Rank: 6Rank: 6
等 级:侠之大者
帖 子:113
专家分:454
注 册:2010-9-26
收藏
得分:20 
看着是有点头大
既然在一个文件下能顺利执行,那就要考虑你拆分后有些调用没有完全引入造成定义缺失。

帮人《---》帮己
2011-01-21 10:28
江楚玥
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2011-1-20
收藏
得分:0 
以下是引用逐渐学习在2011-1-21 10:28:25的发言:

看着是有点头大
既然在一个文件下能顺利执行,那就要考虑你拆分后有些调用没有完全引入造成定义缺失。

谢谢提醒,我一个一个拆分后,终于得出正确结果。

------------------------------------------------------------------------------
config.h
程序代码:
#ifndef DATASTRUCT_CONFIG_H
#define DATASTRUCT_CONFIG_H

//单元测试开关
#define USE_UNIT_TESTING 1

#if USE_UNIT_TESTING
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <google/cmockery.h>
#endif

// 函数结果状态代码
#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 */

#endif


stack.h
程序代码:
#ifndef STACK_H_INCLUDED
#define STACK_H_INCLUDED

typedef struct StackT *Stack;

/* 创建一个Stack */
Stack stack_create(void);

/* Stack是否为空 */
Boolean  stack_empty(Stack stk);

/* return Stack的大小 */
int   stack_size(Stack stk);

/* return Stack顶部的数据 */
void* stack_top(Stack stk);

/* 压入数据 */
void  stack_push(Stack stk, void *x);

/* 弹出数据 */
void* stack_pop(Stack stk);

/* 销毁Stack */
void  stack_destory(Stack stk);

#endif // STACK_H_INCLUDED


stack.c
程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#include "config.h"
#include "stack.h"

typedef struct StackNodeT{
    void *x;
    struct StackNodeT *next;
}*StackNode;

struct StackT{
    int size;
    struct StackNodeT *top;
};

Stack stack_create(void){
    Stack stk=(Stack)malloc(sizeof(struct StackT));
    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_top(Stack stk){
    assert(stk && stk->size>0);
    return stk->top->x;
}

void  stack_push(Stack stk, void *x){
    assert(stk);

    StackNode t=(StackNode)malloc(sizeof(struct StackNodeT));
    if(!t){
        exit(OVERFLOW);
    }
    t->x=x;
    t->next=stk->size>0?stk->top:NULL;

    stk->top=t;
    stk->size++;
}

void* stack_pop(Stack stk){
    assert(stk);
    assert(stk->size>0);

    StackNode t=stk->top;
    stk->top=t->next;
    stk->size--;
    void *x=t->x;
    free(t);
    return x;
}

void stack_destory(Stack stk){
    assert(stk);

    StackNode t,n;
    for(t=stk->top;t;t=n){
        n=t->next;
        free(t);
    }
    free(stk);
}


stack_tests.c
程序代码:
#include <stdio.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 testStack(void **state) {
    int datas[]={1,2,3,4,5};
    int i,*p;
    Stack const stk = *state;
    assert_true(stack_empty(stk));
    for(i=0;i<5;i++){
        stack_push(stk,datas+i);
    }

    assert_int_equal(5,stack_size(stk));

    p=stack_pop(stk);
    printf("pop %d\n",*p);
    assert_int_equal(4,stack_size(stk));

    p=stack_pop(stk);
    printf("pop %d\n",*p);
    assert_int_equal(3,stack_size(stk));
    //free(datas);
}

int main(void) {
    const UnitTest tests[] = {
        unit_test_setup_teardown(testStack, setup,teardown),
    };
    return run_tests(tests);
}
2011-01-21 12:43
viky2003
Rank: 5Rank: 5
等 级:职业侠客
帖 子:375
专家分:383
注 册:2007-4-11
收藏
得分:0 
有个klocwork的tool可以辅助memory leak分析。不过好像只有linux版本。


要练习算法就来http:///!!有挑战哦!!
2011-01-21 20:50
快速回复:一个Stack实现,不知哪有内存泄漏
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.034635 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved