程序终止/内存泄漏 [英] program termination/memory leaks

查看:76
本文介绍了程序终止/内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当程序在释放

内存之前终止时,分配的内存会发生什么。例如:


int main(){

int * array;

int a_size = 1000;

array = new int [a_size];

for(int i = 0; i< a_size; ++ i){

if(i>( a_size / 2))

返回0; //删节前删除...这是一个问题吗?

}

删除[]数组;

返回0;

}


如果这是泄漏,当你ctrl-break时会发生同样的事情。一个

运行程序?

当一个数组被一个类放在堆上时,同样的问题

构造函数,删除在析构函数中被调用。当程序在对象超出范围之前停止时会发生什么?

what happens to allocated memory when a program terminates before the
memory is released. For example:

int main(){
int* array;
int a_size = 1000;
array = new int[a_size];
for(int i = 0; i < a_size; ++i){
if(i > (a_size/2))
return 0; //oops...ended before delete...is this a problem?
}
delete[] array;
return 0;
}

If this is a leak, does the same thing happen when you "ctrl-break" a
running program?

And the same question when an array is placed on the heap by a class
constructor, with delete being called in the destructor. What happens
when the program stops before the object goes out of scope?

推荐答案

J。 Campbell写道:
J. Campbell wrote:
当程序在释放内存之前终止时,分配的内存会发生什么。例如:

int main(){
int * array;
int a_size = 1000;
array = new int [a_size];
for (int i = 0; i< a_size; ++ i){
if(i>(a_size / 2))
返回0; //删除之前删除...这是一个问题吗?
}删除[]数组;
返回0;
}
如果这是泄漏,当你ctrl-break时会发生同样的事情。
运行程序?




进程退出时,操作系统回收进程使用的所有资源。

同样的问题,当一个数组由类
构造函数放在堆上,在析构函数中调用delete。


您必须明确删除使用new创建的所有对象。只有

自动,静态或全局变量会被

编译器自动删除。

当程序在对象退出之前停止时会发生什么范围?
what happens to allocated memory when a program terminates before the
memory is released. For example:

int main(){
int* array;
int a_size = 1000;
array = new int[a_size];
for(int i = 0; i < a_size; ++i){
if(i > (a_size/2))
return 0; //oops...ended before delete...is this a problem?
}
delete[] array;
return 0;
}

If this is a leak, does the same thing happen when you "ctrl-break" a
running program?
The operating system reclaims all resources used by the process when the
process exits.

And the same question when an array is placed on the heap by a class
constructor, with delete being called in the destructor.
You must explicity delete all object you create with new. Only
automatic, static or global variables are deleted automatically by the
compiler.
What happens when the program stops before the object goes out of scope?




程序停止 ?


程序有多种方式可以停止。您指的是哪一个

到了?


a)电源故障


b)低级别退出


c)停在调试器中


e)异步终止


g)暂停


....可能更多



"program stops" ?

There are various ways a program may stop. Which ones are you referring
to ?

a) Power failure

b) low level exit

c) stopped in the debugger

e) terminated asynchronously

g) suspended

.... probably more


来回答你的问题。是的,这是一个内存泄漏。不要这样做。



J。坎贝尔" <毫安********** @ yahoo.com>在消息中写道

news:b9 ************************** @ posting.google.c om ...
to answer your question. YES that is a memory leak. dont do that.


"J. Campbell" <ma**********@yahoo.com> wrote in message
news:b9**************************@posting.google.c om...
当程序在释放内存之前终止时,分配的内存会发生什么。例如:

int main(){
int * array;
int a_size = 1000;
array = new int [a_size];
for (int i = 0; i< a_size; ++ i){
if(i>(a_size / 2))
返回0; //删除之前删除...这是一个问题吗?
}删除[]数组;
返回0;
}
如果这是泄漏,当你ctrl-break时会发生同样的事情。一个运行程序?

当一个数组被一个类
构造函数放在堆上时,同样的问题是在析构函数中调用delete。当程序在对象超出范围之前停止时会发生什么?
what happens to allocated memory when a program terminates before the
memory is released. For example:

int main(){
int* array;
int a_size = 1000;
array = new int[a_size];
for(int i = 0; i < a_size; ++i){
if(i > (a_size/2))
return 0; //oops...ended before delete...is this a problem?
}
delete[] array;
return 0;
}

If this is a leak, does the same thing happen when you "ctrl-break" a
running program?

And the same question when an array is placed on the heap by a class
constructor, with delete being called in the destructor. What happens
when the program stops before the object goes out of scope?



Gianni Mariani写道:
Gianni Mariani wrote:

进程退出时,操作系统会回收进程使用的所有资源。
The operating system reclaims all resources used by the process when the
process exits.




哦?据我所知,标准并不要求这样做。我见过一个

系统丢失了没有明确释放的内存。即使系统已经关闭并重新打开,它也很可能永远消失了。

回收它的唯一方法是擦除整个内存(相当于

格式化你的硬盘)。


-Kevin

-

我的电子邮件地址有效,但会定期更改。

要联系我,请使用最近发布的地址。



Oh? As far as I know, this is not required by the standard. I''ve seen a
system that lost memory that was not explicitly freed. It was pretty
much gone forever, even if the system was turned off and back on. The
only way to reclaim it was to wipe the entire memory (the equivalent of
formating your hard disk).

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.


这篇关于程序终止/内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆