J*****n 发帖数: 4859 | 1 struct D{
vector p;
vector q;
}
D *a, *b;
int main(){
a = new D;
b = new D;
delete b;
b = a;
a = new D;
return;
}
Thank you. |
P********e 发帖数: 2610 | 2 yes.
【在 J*****n 的大作中提到】 : struct D{ : vector p; : vector q; : } : D *a, *b; : int main(){ : a = new D; : b = new D; : delete b; : b = a;
|
J*****n 发帖数: 4859 | 3
This is my real problem. I have need two big struct variables for each loop.
So, my current code goes like following
struct{
//some stl, like vector, map.
}
yesterday = null;
today = get_data(1);
for (i = 2; i != end; ++i){
yesterday = today;
today = get_data(i);
//some function handle yesterday and today.
}
So, what should I do to avoid memory leakage?
Thanks.
【在 P********e 的大作中提到】 : yes.
|
P********e 发帖数: 2610 | 4 你用java就没这问题了
c++的话,不咬用pointer, 用object
struct yourstruct yesterday, today;
loop.
【在 J*****n 的大作中提到】 : : This is my real problem. I have need two big struct variables for each loop. : So, my current code goes like following : struct{ : //some stl, like vector, map. : } : yesterday = null; : today = get_data(1); : for (i = 2; i != end; ++i){ : yesterday = today;
|
t****t 发帖数: 6806 | 5 this is ok as long as you release yesterday and today after the loop.
ignore that Paul guy, he often pretends he know c++, but actually he knows
absolutely nothing about it.
loop.
【在 J*****n 的大作中提到】 : : This is my real problem. I have need two big struct variables for each loop. : So, my current code goes like following : struct{ : //some stl, like vector, map. : } : yesterday = null; : today = get_data(1); : for (i = 2; i != end; ++i){ : yesterday = today;
|
S****z 发帖数: 666 | 6 会,3个new一个delete?这样的好事哪里找?
请在return之前把另外两个delete补上
【在 J*****n 的大作中提到】 : struct D{ : vector p; : vector q; : } : D *a, *b; : int main(){ : a = new D; : b = new D; : delete b; : b = a;
|
M**u 发帖数: 10158 | 7 No
【在 J*****n 的大作中提到】 : struct D{ : vector p; : vector q; : } : D *a, *b; : int main(){ : a = new D; : b = new D; : delete b; : b = a;
|
M**u 发帖数: 10158 | 8 苏坑总好
【在 S****z 的大作中提到】 : 会,3个new一个delete?这样的好事哪里找? : 请在return之前把另外两个delete补上
|
a****l 发帖数: 8211 | 9 就不准人家搞个循环分三次delete掉?
【在 S****z 的大作中提到】 : 会,3个new一个delete?这样的好事哪里找? : 请在return之前把另外两个delete补上
|
J*****n 发帖数: 4859 | 10 what I concern is that if I delete a struct, which contains a stl container.
When the delete is called, the container is always released or just like we
call del on array, which will generate memory leakage (we need to call
delete[])?
Thank you.
【在 t****t 的大作中提到】 : this is ok as long as you release yesterday and today after the loop. : ignore that Paul guy, he often pretends he know c++, but actually he knows : absolutely nothing about it. : : loop.
|
t****t 发帖数: 6806 | 11 objects in struct are properly destroyed when struct is destroyed.
container.
we
【在 J*****n 的大作中提到】 : what I concern is that if I delete a struct, which contains a stl container. : When the delete is called, the container is always released or just like we : call del on array, which will generate memory leakage (we need to call : delete[])? : Thank you.
|
X****r 发帖数: 3557 | 12 and delete yesterday before it is re-assigned.
(assuming get_data returns a new struct every time)
knows
【在 t****t 的大作中提到】 : this is ok as long as you release yesterday and today after the loop. : ignore that Paul guy, he often pretends he know c++, but actually he knows : absolutely nothing about it. : : loop.
|
S*********g 发帖数: 5298 | 13 这个是最基本的C++吧,C++ FAQ, C++ FAQ Lite, thinking in C++都有讲
【在 t****t 的大作中提到】 : objects in struct are properly destroyed when struct is destroyed. : : container. : we
|
J*****n 发帖数: 4859 | 14
sorry,我昏了头了。你说的是对的。我当时定义的时候,曾经考虑过在vector里面放
shared pt。后来脑袋就昏昏沉沉的,也没有多想。
谢谢。
【在 t****t 的大作中提到】 : objects in struct are properly destroyed when struct is destroyed. : : container. : we
|