两次调用新指针? [英] Calling new pointer twice?

查看:76
本文介绍了两次调用新指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,此代码是否有效?这段代码可以导致内存吗?
泄漏?


#include< cstdlib>

#include< iostream>


使用命名空间std;


int main(int argc,char * argv [])

{


int * num = new int(12);

num = new int(18);


cout<< ; * num;


删除数量;


返回0;

}

解决方案

morz< bl ********* @ gmail.com>写道:

我的问题是,这段代码是否有效?


这是有效的。

这段代码会导致内存泄漏吗?


是的,它肯定是内存泄漏。

#include< cstdlib>
#include< iostream>

using namespace std;


int * num = new int(12);
num = new int(18);


此时,num的旧值将丢失,因此您永远无法将
释放。这是有效的,你有内存泄漏,这就是为什么你应该

总是避免这种代码。

cout<< * num;

删除数量;

返回0;
}




问候

-

jb


(rot13中的回复地址,先解读)


< blockquote>好的,我改变了我的代码。如何下面的代码?还是会导致内存泄漏

与否?顺便说一下,谢谢Jakob Bieling。 :)


#include< cstdlib>

#include< iostream>

#include< memory>


使用命名空间std;


int main(int argc,char * argv [])

{

auto_ptr< int> num(new int(12));

num.reset(new int(18));


cout<< * num;


返回0;

}


morz< bl *********@gmail.com>写道:

好吧,我改变了我的代码。如何下面的代码?还是会导致内存泄漏吗?顺便说一下,谢谢Jakob Bieling。 :)

#include< cstdlib>
#include< iostream>
#include< memory>

使用命名空间std;

int main(int argc,char * argv [])

auto_ptr< int> num(new int(12));
num.reset(new int(18));

cout<< * num;

返回0;
}




不,现在你没有内存泄漏,因为调用reset会

如有必要,请保管内存。


hth

-

jb


(rot13中的回复地址,先解读)


My question is,this code is valid or not? can this code cause memory
leaks?

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{

int * num = new int(12);
num = new int(18);

cout << *num;

delete num;

return 0;
}

解决方案

morz <bl*********@gmail.com> wrote:

My question is,this code is valid or not?
It is valid.
can this code cause memory
leaks?
Yes, it definitely is a memory leak.
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{

int * num = new int(12);
num = new int(18);
At this point, the old value of ''num'' is lost, so you are never able
to free it. Tho valid, you have a memory leak, which is why you should
always avoid this kind of code.

cout << *num;

delete num;

return 0;
}



regards
--
jb

(reply address in rot13, unscramble first)


ok i change my code.How about below code? still can cause memory leak
or not? btw,thank you Jakob Bieling. :)

#include <cstdlib>
#include <iostream>
#include <memory>

using namespace std;

int main(int argc, char *argv[])
{
auto_ptr<int> num(new int(12));
num.reset(new int(18));

cout << *num;

return 0;
}


morz <bl*********@gmail.com> wrote:

ok i change my code.How about below code? still can cause memory leak
or not? btw,thank you Jakob Bieling. :)

#include <cstdlib>
#include <iostream>
#include <memory>

using namespace std;

int main(int argc, char *argv[])
{
auto_ptr<int> num(new int(12));
num.reset(new int(18));

cout << *num;

return 0;
}



No, now you do not have a memory leak, because calling reset will
take care of freeing the memory, if necessary.

hth
--
jb

(reply address in rot13, unscramble first)


这篇关于两次调用新指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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