When the object is deleted? [duplicate] - c++

This question already has answers here:
Can a local variable's memory be accessed outside its scope?
(20 answers)
C++ delete - It deletes my objects but I can still access the data?
(13 answers)
Closed 4 years ago.
I have the following code:
class A
{
public:
int x = 6;
~A() { std::cout << "\ndestr invoked\n"; }
};
int main()
{
int* x;
{
A a;
std::cout << &a.x << "\n";
x = &a.x;
}
std::cout << x << ": " << *x;
}
With output:
0x78cac859fc00
destr invoked
0x78cac859fc00: 6
If I understand correctly it seems that destructor was invoked automatically but in memory variable remained. Does anyone know why?
In the following example (pointer was used so object was deleted manually):
class A
{
public:
int x = 6;
~A() { std::cout << "\ndestr invoked\n"; }
};
int main()
{
int* x;
{
A* a = new A;
std::cout << &a->x << "\n";
x = &a->x;
delete a;
}
std::cout << x << ": " << *x;
}
Variable was cleared:
Output:
0x1381360
destr invoked
0x1381360: 0
Does anyone know what the difference is?

Related

why are more destroyed objects than created [duplicate]

This question already has answers here:
Two calls to destructor
(3 answers)
Closed 4 years ago.
am new to c++ and and trying to learn the cost of copying values or passing references, in my example I found though a segment of code that is for me hard to understand:
consider this example:
struct Foo {
Foo(int x) :
internVal { x } {
std::cout << "Con: " << std::endl;
}
~Foo() {
std::cout << "Des: " << std::endl;
}
int getVal() const {
return internVal;
}
private:
int internVal { 0 };
};
now when I do:
int main(int argc, char **argv) {
{
Foo a { 111 };
Foo b { 6555 };
Foo c { -444 };
std::cout << " Foos created..." << std::endl;
std::vector<Foo> vector_Test { };
vector_Test.push_back(a);
vector_Test.push_back(b);
vector_Test.push_back(c);
std::cout << " Foos added..." << std::endl;
for (const auto& d : vector_Test) {
std::cout << "-----" << d.getVal() << "-----" << std::endl;
}
std::cout << " Foos printed..." << std::endl;
}
std::cout << " Foos out of scope..." << std::endl;
std::cout << "end!" << std::endl;
//
return 0;
}
I see the following output:
Con:
Con:
Con:
Foos created...
Des:
Des:
Des:
Foos added...
-----111-----
-----6555-----
------444-----
Foos printed...
Des:
Des:
Des:
Des:
Des:
Des:
Foos out of scope...
end!
so my question why are more objects destroyed as created?
I would expect an even number... if I create directly/ indirectly 3 objects then destroy 3 objects
Do I habe a memory leak some where?
thanks
Ps: my compiler is MinWG
When you do vector_Test.push_back(a); you are adding a copy of the instance to the vector.
Which means later on those copies and the original elements get destroyed.

How to delete whole object by knowing only its base-pointer [duplicate]

This question already has answers here:
When to use virtual destructors?
(20 answers)
Closed 5 years ago.
I'm trying to implement autorelease pool in c++, and have trouble with deallocating.
So we have root-class object:
class object {
public:
object() {
retainCount_ = 0;
}
~object() {
std::cout << "object " << Description() << " is dealocated" << std::endl;
}
/* code with retain/release here */
int retainCount() {
return retainCount_;
}
std::string Description() {
std::stringstream ss;
ss << this;
return ss.str();
}
private:
int retainCount_;
};
And some realization:
class Integer : public object {
public:
int i;
Integer(int ii) : i(ii) {}
~Integer() {
std::cout << "Integer " << Description() << " is dealocated" << std::endl;
}
};
And of course release pool class, which works with root-class pointers:
class release_pool {
public:
void insert(object* obj) {
pointers_.insert(obj);
}
void flush() {
std::set<object*>::iterator it = pointers_.begin();
std::set<object*>::iterator tmp;
const int N = pointers_.size();
for (int i = 0; i < N; i++) {
tmp = it;
it++;
if ((*tmp)->retainCount() == 0 ) {
object* obj = *tmp;
std::cout << "delete obj: " << obj->Description() << std::endl;
pointers_.erase(tmp);
delete obj;
}
}
}
private:
std::set<object*> pointers_;
};
main.cpp code for test:
int main () {
release_pool pool;
Integer* obj = new Integer(5);
pool.insert(obj);
std::cout << "before flush: " << obj->i << "\n";
pool.flush();
std::cout << "after flush: " << obj->i << "\n";
return 0;
}
After build, I get next:
before flush: 5
delete obj: 0x7f9a84c025d0
object 0x7f9a84c025d0 is dealocated
after flush: 5
At the end: destructor of root-class is invoked, but of Integer not. Hence, we have leaked memory, which is allocated for Integer object. Have you any ideas to fix it? How i can delete whole object, not root-part of it.
You need to make your object destructor virtual.

C++ strange behaviour of cout's flush. [duplicate]

This question already has answers here:
Order of execution in operator <<
(4 answers)
Closed 6 years ago.
Consider the following code. expected output should be
0 1
1 2
2 3
and so on.
#include<iostream>
using namespace std;
int f=0;
int B()
{
return f;
}
int A()
{
return f++;
}
int main()
{
cout<<A()<<" "<<B()<<endl;
cout<<A()<<" "<<B()<<endl;
cout<<A()<<" "<<B()<<endl;
cout<<A()<<" "<<B()<<endl;
return 0;
}
but the actual output is
0 0
1 1
2 2
and so on.. why?
and if i change code like this-
int main()
{
int f=0;
cout<<f++<<" "<<f<<endl;
cout<<f++<<" "<<f<<endl;
cout<<f++<<" "<<f<<endl;
cout<<f++<<" "<<f<<endl;
return 0;
}
then i get correct expected output
why?
The order of evaluation of the operands of << is not specified. So
cout << A() << " " << B() << endl;
can be treated as either:
temp1 = A();
temp2 = B();
cout << temp1 << " " << temp2 << endl;
or as:
temp2 = B();
temp1 = A();
cout << temp1 << " " << temp2 << endl;
Performing side effects on a variable and accessing it without defined sequencing results in undefined behavior.

How does RAII work when mixing default and non-default constructors

I want to write a library wrapper class (LibWrap) around a C library that uses malloc/free. To do this I want to use C++'s RAII to allocate and free memory. I used lib_address as a random example address that I would receive from the library. However when defining my memeber variable the destructor that is called somehow has this lib_address.
I would expect the destructor of the member variable created by the default constructor not to know the new address that I am putting into the constructor of my replacement member variable.
#include <stdlib.h>
#include <iostream>
using namespace std;
class LibWrap
{
int j;
int lib_address;
public:
LibWrap(): //default LibWrap
j(0),
lib_address(0)
{
cout << "default LibWrap "<<j <<"\t\t"<<lib_address << "\t" << this<<endl;
}
LibWrap(int f_j): //special LibWrap
j(0),
lib_address(0)
{
j = f_j;
lib_address = rand();
cout << "special LibWrap " << j<<"\t"<< lib_address<< "\t" << this <<endl;
}
~LibWrap()
{
cout << "killing LibWrap " << j<<"\t" <<lib_address <<"\t" << this<< endl;
}
int g()
{
return j;
}
};
class A
{
int i;
LibWrap b;
public:
A(): //default A
i(0)
{
cout << "default A\t"<<i << endl;
}
A(int f_i)://special A
i(0)
{
i = f_i;
cout << "special A\t"<<i << endl;
b = LibWrap(10);
}
~A()
{
cout << "killing A\t"<<i << endl;
}
void p()
{
cout <<"Test values: "<< i<< "," << b.g() << endl;
}
};
int f()
{
//A a; a.p();
cout << "variable\t\tlib_address\treal_address" << endl;
A a = A(1);
cout << "End" << endl;
//a.p();
}
int main()
{
f();
}
Running this code I would expect to get the following result:
variable lib_address real_address
default LibWrap 0 0 0xbfef2e28
special A 1
special LibWrap 10 1804289383 0xbfef2df8
killing LibWrap 10 1804289383 0xbfef2df8 --would expect kiling LibWrap 0 0 0xbfef2e28
End
killing A 1
killing LibWrap 10 1804289383 0xbfef2e28 --would expect killing LibWrap 10 1804289383 0xbfef2df8
b = LibWrap(10);
This does not initialize b. b has already been initialized as part of constructing A. What you're doing is creating a temporary LibWrap and copying that temporary into b. Then, you're destroying that temporary LibWrap (which is where the extra destructor call with the lib_address comes from).
The temporary LibWrap is where the "0xbfef2df8" address comes from. The b variable is the "0xbfef2e28" address. That's why you're getting them in that order.

C++ - Class members when Destructor is called during overwrite

im trying to understand the order of Constructor and Destructor calls when overwriting an object.
My code is :
class A
{
public:
A(int n): x(n)
{ cout << "A(int " << n << ") called" << endl; }
~A( )
{ cout << "~A( ) with A::x = " << x << endl; }
private:
int x;
};
int main( )
{
cout << "enter main\n";
int x = 14;
A z(11);
z = A(x);
cout << "exit main" << endl;
}
--
The output is :
enter main
A(int 11) called
A(int 14) called
~A( ) with A::xx = 14
exit main
~A( ) with A::xx = 14
--
Why is A::xx = 14 when the destructor is called? Shouldn't it be 11?
Why should it be 11? You reassign z to A(14), so it's 14 at the end.
(After your edit: You also see the destructor of the temporary A(14) object that gets destroyed at the end of the assignment.)