r*r
2 楼
I feel that I need to delete everytime after I use "new" to create a pointer.
What if I want to put the new inside a loop?For example
myobj * np;
for (int i =1;i ...
np = new myobj();
}
delete np;
will this work?Or do I have to delete np inside the loop?
What if I want to put the new inside a loop?For example
myobj * np;
for (int i =1;i
np = new myobj();
}
delete np;
will this work?Or do I have to delete np inside the loop?
R*r
3 楼
Your program will have memory leak, delete it inside loop.
.
【在 r*****r 的大作中提到】
: I feel that I need to delete everytime after I use "new" to create a pointer.
: What if I want to put the new inside a loop?For example
: myobj * np;
: for (int i =1;i : ...
: np = new myobj();
: }
: delete np;
: will this work?Or do I have to delete np inside the loop?
.
【在 r*****r 的大作中提到】
: I feel that I need to delete everytime after I use "new" to create a pointer.
: What if I want to put the new inside a loop?For example
: myobj * np;
: for (int i =1;i
: np = new myobj();
: }
: delete np;
: will this work?Or do I have to delete np inside the loop?
m*n
5 楼
It won't work. Even if you define np outside the loop.
You have to do:
myobj * np;
for (int i = 1; i < nloop; ++i)
{
...
np = new myobj();
...
delete np;
}
or you could you smart pointer:
#include
for (int i = 1; i < nloop; ++i)
{
...
std::auto_ptr np(new myboj());
(use np just like plain poiters)
}
.
【在 r*****r 的大作中提到】
: I feel that I need to delete everytime after I use "new" to create a pointer.
: What if I want to put the new inside a loop?For example
: myobj * np;
: for (int i =1;i : ...
: np = new myobj();
: }
: delete np;
: will this work?Or do I have to delete np inside the loop?
You have to do:
myobj * np;
for (int i = 1; i < nloop; ++i)
{
...
np = new myobj();
...
delete np;
}
or you could you smart pointer:
#include
for (int i = 1; i < nloop; ++i)
{
...
std::auto_ptr
(use np just like plain poiters)
}
.
【在 r*****r 的大作中提到】
: I feel that I need to delete everytime after I use "new" to create a pointer.
: What if I want to put the new inside a loop?For example
: myobj * np;
: for (int i =1;i
: np = new myobj();
: }
: delete np;
: will this work?Or do I have to delete np inside the loop?
相关阅读
Spark SQL 1.3 以后的版本 问题比较头疼还是软软好问个弱智问题,js除了前端和node.js这种framework,还能用在哪里?scala.jshow to debug你软是不是又干蠢事了vertica如何,跟impala, hbase,比有什么异同?node.js里调用了callback之后,还会回到函数里。BEA的庄思浩node有啥好的地方啊?神马优势啊?现在怎么越来越多的公司用cookie了?这不是过时的技術吗?今天出了个 bug,给大家提个醒scala基本是死了。clojure还有戏么请推荐嵌入式的显示架构不行了,今年又不涨工资Java8的lambda很难用呀[python] raise another exception in exception constructNFLX大概是世界上最简单的 scale 任务了吧?A join B, spark里面怎么写?问个弱智问题,有网站用nosql做primary db么?