求大神来帮我解决这个内存泄露的问题~

Big-Heart 2014-05-18 10:47:57
这段代码的目的是:建立一个集合,往集合中任意添加元素
//Set.h

#pragma once

#include <crtdbg.h>

//#include "stdafx.h"

#include <iostream>

using namespace std;

//template <class int> //声明一个模板,虚拟类型名为int

#ifdef _DEBUG //这个要加上,否则不会输出定义到那个文件中(及不包含存在内存泄露的该cpp文件的相关信息)

#define new new(_NORMAL_BLOCK, __FILE__, __LINE__)

#endif



class Set

{

public:

Set ()

{

i=1;

a=new int [i];

if (a==NULL)

{

cout<<"a分配错误";

}

b=new int [i];

if (b==NULL)

{

cout<<"b分配错误";

}

}

void add(int num);

void cut(int num);

Set operator+(Set &a);

Set operator-(Set &a);

void relese(Set);

void show()

{

for(int n=0;n<i;n++)

cout<<this->a[n]<<endl;

cout<<"i="<<i<<endl;

}

private:

int *a;

int *b;

int i;

};

//Set.cpp

#include "Set.h"

//template <class int>

void Set::add(int num)

{

i++;

memcpy(b,a,(i-1)*sizeof(int));//a数组复制给b

delete[]a;//将a销毁

a=new int [i];//申请一个比b数组大一个单位的数组

if (a==NULL)//判断内存是否分配成功

{

cout<<"a分配错误";

}

else

{

a[i-1]=num;//将写进来的num赋给a[i-1]

memcpy(a,b,(i-1)*sizeof(int));//再将b中的复制给a中前[i-1]项

}

this->b=new int[i];

}

void Set::relese (Set S1)

{

delete []S1.a;

delete []S1.b;

cout<<"delete"<<endl;

}

//mian.cpp

#include "Set.h"

int main()

{

_CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF | _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG));

Set S1;

S1.add(10);

S1.add(1);

S1.add(41);

S1.show();

S1.relese (S1);//销毁a、b申请的空间

_CrtDumpMemoryLeaks();

}
调试窗口中内存泄露提示:
Detected memory leaks!
Dumping objects ->
.\Set.cpp(8) : {135} normal block at 0x00565418, 16 bytes long.
Data: < ) > CD CD CD CD 0A 00 00 00 01 00 00 00 29 00 00 00
d:\编程\vc++2005\代码\experiment\experiment\Set.h(22) : {132} normal block at 0x005653D8, 4 bytes long.
Data: < > CD CD CD CD
各位大神帮忙看看
...全文
156 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Big-Heart 2014-05-19
  • 打赏
  • 举报
回复
你好,谢谢你的回答。随便的你可否帮我修改一下那个内存泄露的代码吗?因为我想知道他哪里泄露了
赵4老师 2014-05-19
  • 打赏
  • 举报
回复
杜绝内存泄漏的最好办法是不使用动态内存。
//使用动态分配
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
int i,L;
char *p;
void main() {
    for (i=0;i<20000;i++) {
        L=rand();
        p=malloc(L);
        if (NULL==p) {
            printf("malloc error!\n");
            continue;
        }
        memset(p,0,L);
        free(p);
    }
}
//不使用动态分配
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#define MAXLEN 30000
int i,L;
char buf[MAXLEN];
char *p;
void main() {
    p=&buf[0];
    for (i=0;i<20000;i++) {
        L=rand();
        if (L>MAXLEN) {
            printf("L>MAXLEN==%d, ignore spilth.\n",MAXLEN);
            L=MAXLEN;
        }
        memset(p,0,L);
    }
}
Big-Heart 2014-05-19
  • 打赏
  • 举报
回复
你好,谢谢你的回答,当我改为: #include "Set.h" void Set::add(int num) { i++; memcpy(b,a,(i-1)*sizeof(int)); delete[]a; a=new int [i]; if (a==NULL) { cout<<"a分配错误"; } else { a[i-1]=num; memcpy(a,b,(i-1)*sizeof(int)); } delete []b; b=new int[i]; if (b==NULL) { cout<<"b分配错误"; } } 输出窗口仍然提示有内存泄露: Detected memory leaks! Dumping objects -> .\Set.cpp(19) : {138} normal block at 0x00855398, 12 bytes long. Data: < > CD CD CD CD CD CD CD CD CD CD CD CD .\Set.cpp(8) : {137} normal block at 0x008553E0, 12 bytes long. Data: < ) > 0A 00 00 00 01 00 00 00 29 00 00 00 Object dump complete. 还需要改什么地方吗?帮忙看一下。谢谢了!
riyueming184 2014-05-18
  • 打赏
  • 举报
回复
void Set::add(int num) { i++; memcpy(b,a,(i-1)*sizeof(int));//a数组复制给b delete[]a;//将a销毁 a=new int [i];//申请一个比b数组大一个单位的数组 if (a==NULL)//判断内存是否分配成功 { cout<<"a分配错误"; } else { a[i-1]=num;//将写进来的num赋给a[i-1] memcpy(a,b,(i-1)*sizeof(int));//再将b中的复制给a中前[i-1]项 } this->b=new int[i];//这里b没有进行销毁又重新分配了 }

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧