释放动态内存的问题?

猫仔- 2013-09-01 10:15:31
你们看我的主函数的代码。为什么外部循环那个释放内存的语句我感觉有问题呢?每次进行了几次循环之后
我就感觉有崩溃的感觉!!
#include<cstdio>
#include<stdlib.h>
#include<crtdbg.h>
#include<iostream>
#include"Name.h"
using namespace std;

void init(Name* names,int count)
{
char* fi_rstnames[]={"charles","mary","arthur","emily","john"};
int fi_rstsize=sizeof (fi_rstnames)/sizeof(fi_rstnames[0]);
char* secondnames[]={"dickens","shelley","miller","bronte","steinbeck"};
int secondsize=sizeof(secondnames)/sizeof(secondnames[0]);
char* fi_rst=fi_rstnames[0];
char* second=secondnames[0];
for(int i=0;i<count;i++)
{
if(i%2)
fi_rst=fi_rstnames[i%fi_rstsize];
else
second=secondnames[i%secondsize];
names[i]=Name(fi_rst,second);

}

}


int main(int argc,char* argv[])
{
FILE *pout(nullptr);
errno_t err=freopen_s(&pout,"debug_out_txt","w",stdout);
if(err)
cout<<"error on freopen "<<endl;
_CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF|_CRTDBG_ALLOC_MEM_DF); //程序退出时执行内存泄露检测
_CrtSetReportMode(_CRT_WARN,_CRTDBG_MODE_FILE); //将检测到的内存泄露输出一般目的地
_CrtSetReportFile(_CRT_WARN,_CRTDBG_FILE_STDOUT); //将内存泄漏检测到的输出设定为标准流
Name myname("Ivor","Horton");
char thename[12];
cout<<"\nThe name is "<<myname.getname(thename);
char* pname=new char[myname.getnamelength()+1];
cout<<"\nThe name is "<<myname.getname(pname);
const int arraysize=5;
Name names[arraysize];
init(names,arraysize);
char* phrase=nullptr;
char* iname=nullptr;
char* jname=nullptr;
for(int i=0;i<arraysize;i++)
{
iname=new char[names[i].getnamelength()+1];
for(int j=i+1;j<arraysize;j++)
{
if(names[i]<names[j])
phrase=" less than ";
else if(names[i]<names[j])
phrase=" greater than ";
else if(names[i]==names[j])
phrase=" equal to ";
jname=new char[names[j].getnamelength()+1];
cout<<endl<<names[j].getname(iname)<<" is "<<phrase
<<names[j].getname(jname);
delete[] jname;

}
delete[] iname; //进行多次循环之后。发现在某一次外部循环。外部循环包括的指针数组进行释放内存的时候感觉有问题。~WHY
iname=nullptr;
}

delete[] pname;
cout<<endl;

return 0;
}


你们说:外部循环delete [] iname;是不是有问题。每次进行了几次循环之后到了这里我就感觉有问题了!
...全文
263 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
blackkettle 2013-09-03
  • 打赏
  • 举报
回复
引用 4 楼 zhao4zhong1 的回复:
仅供参考
//使用动态分配
#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);
    }
}
个人倾向不使用动态分配。
+1
猫仔- 2013-09-02
  • 打赏
  • 举报
回复
引用 6 楼 chenyegui 的回复:
[quote=引用 4 楼 zhao4zhong1 的回复:] 仅供参考
//使用动态分配
#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);
    }
}
个人倾向不使用动态分配。
老师的方法是好。可是我是很菜菜的小菜鸟。刚把数据结构和C C++看完的小鸟鸟。。。现在还在靠课本过日子!!还没到脱离课本的时候!![/quote] 还有老师。现在我这个是调试动态内存!!就是检测内存泄漏问题的一个代码段!!!老师的方式。学生记下了!
猫仔- 2013-09-02
  • 打赏
  • 举报
回复
引用 3 楼 sduxiaoxiang 的回复:
没啥问题 new的结果最好判断下 new和delete是对称的
恩恩。我判断了。第一次和第二次的地址时一样的!!!
猫仔- 2013-09-02
  • 打赏
  • 举报
回复
引用 9 楼 aizibion 的回复:
怀疑跟“ _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF|_CRTDBG_ALLOC_MEM_DF); //程序退出时执行内存泄露检测 _CrtSetReportMode(_CRT_WARN,_CRTDBG_MODE_FILE); //将检测到的内存泄露输出一般目的地 _CrtSetReportFile(_CRT_WARN,_CRTDBG_FILE_STDOUT); //将内存泄漏检测到的输出设定为标准流” 有关系,你把这段屏蔽了再试试呢
没关系啦。我试了。还是在执行第二次外部循环的释放内存段有问题。第二次执行delete [] iname;时都会自动结束主函数。。。。蛋疼!循环一样还没结束!
aizibion 2013-09-02
  • 打赏
  • 举报
回复
怀疑跟“ _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF|_CRTDBG_ALLOC_MEM_DF); //程序退出时执行内存泄露检测 _CrtSetReportMode(_CRT_WARN,_CRTDBG_MODE_FILE); //将检测到的内存泄露输出一般目的地 _CrtSetReportFile(_CRT_WARN,_CRTDBG_FILE_STDOUT); //将内存泄漏检测到的输出设定为标准流” 有关系,你把这段屏蔽了再试试呢
BuleRiver 2013-09-02
  • 打赏
  • 举报
回复
使用智能指针吧。
猫仔- 2013-09-02
  • 打赏
  • 举报
回复
引用 2 楼 max_min_ 的回复:
等崩溃了 你再感觉吧! 一个new对应一次delete就好了!担心啥!
呵呵。是我错了。应该不是感觉。只是不知道为啥。我单步调试了。在某一次循环到了那么。就直接在调用释放内存的函数结束后就把主函数给结束了。直接弹出了dos窗口。程序就不在执行下面的程序段了。还有。循环还没结束呢!!!
猫仔- 2013-09-02
  • 打赏
  • 举报
回复
引用 4 楼 zhao4zhong1 的回复:
仅供参考
//使用动态分配
#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);
    }
}
个人倾向不使用动态分配。
老师的方法是好。可是我是很菜菜的小菜鸟。刚把数据结构和C C++看完的小鸟鸟。。。现在还在靠课本过日子!!还没到脱离课本的时候!!
猫仔- 2013-09-02
  • 打赏
  • 举报
回复
引用 1 楼 aizibion 的回复:
兄弟,编程是一个很严肃的问题,怎么能凭感觉形式的,最不济也得凭现象啊。。。
嗯。我单步调试了。就是在某一次循环内。释放内存。调用本地库释放内存那个函数的结束的时候。居然直接把 主函数给结束了!!把DOS窗口弹出来~~下了的程序段执行不了~
赵4老师 2013-09-02
  • 打赏
  • 举报
回复
仅供参考
//使用动态分配
#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);
    }
}
个人倾向不使用动态分配。
sduxiaoxiang 2013-09-02
  • 打赏
  • 举报
回复
没啥问题 new的结果最好判断下 new和delete是对称的
max_min_ 2013-09-02
  • 打赏
  • 举报
回复
等崩溃了 你再感觉吧! 一个new对应一次delete就好了!担心啥!
猫仔- 2013-09-02
  • 打赏
  • 举报
回复
引用 15 楼 zhao4zhong1 的回复:
c++通常的做法是捕获异常而不判断new是否成功。
非常感谢老师的提醒!经过你的提醒。我就认真的一一把其中的可能给排除了。终于经过几次调试发现其中的蹊跷!呵呵。原来是我不小心把指针数组的长度给弄错了。names[j].getname(iname)与 iname=new char[names[i].getnamelength()+1];这里给弄错了。造成第二次循环数组指针的长度不能全把复制过来的字段给取址完全!!非常感谢!
猫仔- 2013-09-02
  • 打赏
  • 举报
回复
引用 15 楼 zhao4zhong1 的回复:
c++通常的做法是捕获异常而不判断new是否成功。
这么说是那里有异常咯?我仔细的看了一下new分配的地址,第二次还是和第一次分配的一样!!
赵4老师 2013-09-02
  • 打赏
  • 举报
回复
c++通常的做法是捕获异常而不判断new是否成功。
猫仔- 2013-09-02
  • 打赏
  • 举报
回复
引用 13 楼 ao834391367 的回复:
new delete是成对出现 malloc free成对出现
嗯。这个知道。就是外部循环那个释放内存delete 执行第二次的时候居然起到return 的作用了。。。
  • 打赏
  • 举报
回复
new delete是成对出现 malloc free成对出现
aizibion 2013-09-01
  • 打赏
  • 举报
回复
兄弟,编程是一个很严肃的问题,怎么能凭感觉形式的,最不济也得凭现象啊。。。

64,653

社区成员

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

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