由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - new and delete in c++
相关主题
请问delete的问题Please Help, dynamic memory after fork()
问个c++在不同函数里分配内存和释放内存的弱问题a question of perl
请问可以这样定义struct吗?请问一个关于 cost of pointer的问题
find start point of loop from linked listpointer to function
【包子求助】20M*20M的loop怎么搞?[合集] C++ programming--break 2-loops outside
一个简单的小问题pointer to base class = new derived, what will happend??
问个C++中重复删除指针的问题const char *p, is it ok to change p[1] ?
one question about overloading operator deleteint (*b)[2]; 是什么意思?
相关话题的讨论汇总
话题: np话题: delete话题: myobj话题: loop话题: new
进入Programming版参与讨论
1 (共1页)
r*****r
发帖数: 397
1
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
发帖数: 104
2
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
发帖数: 397
3
then I guess I also need to declare myobj * np inside the loop?

【在 R*******r 的大作中提到】
: Your program will have memory leak, delete it inside loop.
:
: .

m******n
发帖数: 21
4
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?

r*****r
发帖数: 397
5
then I guess I also need to declare myobj * np inside the loop?

【在 R*******r 的大作中提到】
: Your program will have memory leak, delete it inside loop.
:
: .

c*r
发帖数: 278
6
Even you declare it as a globle or static, it doesn't matter.
new abd delete must match!

outside

【在 r*****r 的大作中提到】
: then I guess I also need to declare myobj * np inside the loop?
N*********y
发帖数: 105
7
you should NOT use np in this way. Since you are using it more like a local
variable, you should allocate the memory block outside the loop.

outside

【在 r*****r 的大作中提到】
: then I guess I also need to declare myobj * np inside the loop?
1 (共1页)
进入Programming版参与讨论
相关主题
int (*b)[2]; 是什么意思?【包子求助】20M*20M的loop怎么搞?
这个function pointer最后的那个int是什么意思?一个简单的小问题
int *a [] 和int (*a)[] 一样吗问个C++中重复删除指针的问题
如何让一个指针指向一个多维数组one question about overloading operator delete
请问delete的问题Please Help, dynamic memory after fork()
问个c++在不同函数里分配内存和释放内存的弱问题a question of perl
请问可以这样定义struct吗?请问一个关于 cost of pointer的问题
find start point of loop from linked listpointer to function
相关话题的讨论汇总
话题: np话题: delete话题: myobj话题: loop话题: new