Dynamic memory used in function

Oct 2, 2012 at 9:58pm
Suppose I have the following code (I saw this kind on the Internet):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class foo{
	public:
	int m , n;
	foo(int x , int y){
		m=x;
		n=y;
	}
};

void f(foo *a){
//...
}

int main(){
f(new foo(2,3));
return 0;
}


Would the variable after "new" be deleted after the function operates? (So it saves space)
Oct 2, 2012 at 10:01pm
Would the variable after "new" be deleted after the function operates?

No. Use an automatic object.
Oct 2, 2012 at 10:30pm
So you mean I have to have another dummy variable?
I have another question. If f(foo *a) has a dynamic variable with "new". Is the variable deleted after the function operates?
Oct 2, 2012 at 10:44pm
All objects created with new has to be deleted with delete.
Oct 3, 2012 at 12:10am
He. Thank you. That helps me a lot!
Oct 3, 2012 at 12:14am
closed account (1yR4jE8b)
You could have checked this yourself by making a destructor that prints something to the screen and see if it's get's called at any point in that program ;) .
Topic archived. No new replies allowed.