Help for memory management

Hi
In the code below ,my question is when i delete sss[4], what happens to allocated memory for "*x" in str[4]?

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;
struct str{
	int *x;
};
int main(){
	str*  sss[40];
	sss[4] = new str;
	sss[4]->x = new int;
	*(sss[4]->x) = 500;
	delete sss[4];
	return 0;
}


Thanks in advance
Last edited on
delete does not free memory recursively, so if a dynamically allocated structure holds the only pointer to some allocated memory and that structure is deleted, the other allocated memory is neither released nor is it accessible anymore, so it becomes leaked.
In other words, yes. As you suspect, the int pointed to by sss[4]->x is leaked after line 11.
Thanks ..
Is the same result in case of using a Class instead of Structure?
Yes. A class is identical to a struct in every way, save one: the default access specifier. In a struct, the default is public, and in a class, it's private.
Topic archived. No new replies allowed.