delete object outside function

Jan 24, 2019 at 9:08am
test() function doesn't return any (no return pointer, it is void function )
How to delete objA outside test() function use reference?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  #include <iostream>
#include <string>
class A{
   public:
   A(){std::cout<<"A\n";}
   ~A(){std::cout<<"~A\n";}
   void hello(){std::cout<<"hello\n";}
};

void test()
{
    A * objA =  new A;   
    objA->hello();
}
int main()
{
  test();
  return 0;
}
Jan 24, 2019 at 9:15am
You can't delete the object unless you have a pointer (or reference) to it.

Normally, when an object is only used within a function, it is the function's responsibility to delete the object. In this case it's not even necessary to dynamically allocate the object. Declaring it as a local variable would mean it will get destroyed automatically when it goes out of scope (at the end of the function).

1
2
3
4
5
void test()
{
    A objA;   
    objA.hello();
} // <-- objA is automatically destroyed 

Last edited on Jan 24, 2019 at 9:15am
Jan 24, 2019 at 9:19am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void test(A* &ptr)
{   
    A * objA =  new A;   
    ptr = objA;
    objA->hello();
}

int main()
{
  A* ptr; 
  test(ptr);
  delete ptr;
  return 0;
}
Jan 24, 2019 at 9:20am
Dear @Peter87
Thanks for your comment.
But I want deep learning about reference and delete operator.
I want management my pointer.
Thanks.
Jan 24, 2019 at 9:26am
Thanks for your idea. @Grime
Do you have any other idea?
I expected using std::map.
Jan 24, 2019 at 9:27am
But I want deep learning about reference and delete operator.
I want management my pointer.

Well, you could make the function return a pointer and then delete it in main.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>

class A
{
public:
	A() { std::cout << "A\n"; }
	~A() { std::cout << "~A\n"; }
	void hello() { std::cout << "hello\n"; }
};

A* test()
{
	A* objA =  new A;   
	objA->hello();
	return objA;
}
int main()
{
	A* a = test();
	delete a;
}

Personally, I would prefer std::unique_ptr instead of raw pointers so that I don't have to call new and delete myself, but I guess that is not what you want to hear...
Last edited on Jan 24, 2019 at 9:30am
Jan 24, 2019 at 9:34am
Thanks @Peter87
at #1, I describe that :
test() function doesn't return any (no return pointer, it is void function )

Thanks for your support.
I expected using reference and std::map to resolve my issue.
Jan 24, 2019 at 9:44am
Is this legitimate?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
class A;
A* ptr;

class A{
   public:
   A(){ptr=this; std::cout<<"A\n";}
   ~A(){std::cout<<"~A\n";}
   void hello(){std::cout<<"hello\n";}
};

void test()
{
    A * objA =  new A;   
    objA->hello();
}
int main()
{
  test();
  delete ptr;
}
Jan 24, 2019 at 9:48am
Thanks @lastchance
This is a good idea
Thanks so much.
But
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <string>
class A;
A* ptr;

class A{
   public:
   A(){ptr=this; std::cout<<"A\n";}
   ~A(){std::cout<<"~A\n";}
   void hello(){std::cout<<"hello\n";}
};

void test()
{
    A * objA =  new A;   
    A * objB =  new A;  
    A * objC =  new A; 
    objA->hello();
    objB->hello();
    objC->hello();
}
int main()
{
  test();
  delete ptr;
  delete ptr;
  delete ptr;
}

It seems impossible delete objC
Last edited on Jan 24, 2019 at 9:53am
Jan 24, 2019 at 9:50am
I'm not sure yet whether it is legitimate, @kakaducsy!

I'm a bit surprised that it seems to work. It is going to be a bit indiscriminate as to which particular object of class A you delete (the last one created at best). Maybe you will need std::map after all.


EDIT: our edits crossed. ptr is a single variable - you can't use it to knock out multiple objects. You will need more than one external pointer.
Last edited on Jan 24, 2019 at 9:57am
Jan 24, 2019 at 9:56am
Dear @lastchance
It look same:
 
std::map<A*,A*> keep_pointer;
Last edited on Jan 24, 2019 at 9:58am
Jan 24, 2019 at 9:59am
What is std::map going to be used for? Am I missing something?

More specifically how will you use map?

Last edited on Jan 24, 2019 at 10:05am
Jan 24, 2019 at 10:18am
@kakaducsy, the fact that you can do something doesn't mean that you should.
The following code is horrible. It's an awful way of trying to circumvent passing by argument or return value. I would go back and read @Peter's comments.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <stack>

class A;
std::stack<A*> ptrs;

class A{
   public:
   static int counter;
   int me;
   A(){ ptrs.push( this );   me = ++counter;   std::cout << "A" << me << '\n'; }
   ~A(){ std::cout << "~A" << me << '\n'; }
   void hello(){ std::cout << "hello " << me << '\n'; }
};

int A::counter = 0;


void test()
{
    A * objA =  new A;   
    A * objB =  new A;  
    A * objC =  new A; 
    objA->hello();
    objB->hello();
    objC->hello();
}


int main()
{
  test();
  while ( !ptrs.empty() ) { delete ptrs.top();   ptrs.pop(); }
}


A1
A2
A3
hello 1
hello 2
hello 3
~A3
~A2
~A1
Last edited on Jan 24, 2019 at 10:29am
Jan 24, 2019 at 10:34am
Constructor can also be called when a static instance of the class is declared so don't rely on that you will get runtime errors.

Hmm is there any way to know whether a pointer points to static memory or dynamically allocated memory?

Could a class know whether it was dynamically allocated with new?
Last edited on Jan 24, 2019 at 10:37am
Jan 24, 2019 at 12:50pm
Thanks @lastchance,@Grime
Thanks for your support.
Have a nice day
Topic archived. No new replies allowed.