here is my code snippet:
#include <iostream>
#include <list>
#include <memory>
class A
{
public:
int a = 100;
A()
{
std::cout << "Create A" << std::endl;
}
~A()
{
std::cout << "Release A" << std::endl;
}
virtual void printer() = 0;
};
std::list<std::shared_ptr<A>> arr;
class B : public A
{
public:
int b = 1000;
~B()
{
std::cout << "Release B" << std::endl;
}
void printer() override
{
std::cout << "B's printer" << std::endl;
}
B()
{
std::shared_ptr<A> tmp(this);
arr.push_back(tmp);
(*arr.begin())->printer();
std::cout << "inside B's c'tor test B counts: " << tmp.use_count()
<< std::endl;
}
};
int main(int argc, char const *argv[])
{
std::shared_ptr<B> B_ptr = std::make_shared<B>();
std::shared_ptr<A> A_ptr(B_ptr);
std::cout << "list address: " << (*arr.begin()).get() << std::endl;
std::cout << "test B address: " << B_ptr.get() << std::endl;
std::cout << "test A address: " << A_ptr.get() << std::endl;
std::cout << "list counts: " << (*arr.begin()).use_count() << std::endl;
std::cout << "test B counts: " << B_ptr.use_count() << std::endl;
std::cout << "test A counts: " << A_ptr.use_count() << std::endl;
return 0;
}
My expectation is: A's reference count should be three, but only got 2. I think when I push_back to the list, there should be a temporarily created share_ptr object, even it is get destroyed, the one in list should also pointing to the same address as A_ptr and B_ptr. It turns out that they (those three), did pointing at the same address, but use_count got different results. (*arr.begin()).use_count() is 1, the others are both 2.
Why? Please help.
Ps: I know turning this pointer to share_ptr is stupid operation, but the result doesn't make sense and even disobey the syntax.
My expectation is: A's reference count should be three, but only got 2.
Your expectation is wrong. You only made one copy of the shared pointer, so the use count is 2.
std::shared_ptr<A> tmp(this);
On this line you transfer the ownership of a bare pointer that you don't own into a new shared pointer. Since this was already owned by another shared pointer, the behaviour of the program will be undefined when the two separate owners attempt to destroy it.
Creating a shared pointer from this is possible using std::enable_shared_from_this, but it's not simple.
I'm using Visual Studio 2017 and came across a bug which I cannot understand. I have this code as an example:
#include <iostream>
struct Foo
{
int a = 0;
int c;
int *b;
Foo(int n)
{
a = n;
b = &c;
}
};
struct Bar
{
Foo nestedFoo = Foo(0);
Bar(int n)
{
std::cout << "bar constructor nestedFoo before:" << std::endl;
std::cout << &nestedFoo.a << std::endl;
std::cout << nestedFoo.b << std::endl;
nestedFoo = Foo(n);
std::cout << "bar constructor nestedFoo after:" << std::endl;
std::cout << &nestedFoo.a << std::endl;
std::cout << nestedFoo.b << std::endl;
}
};
struct App
{
Bar bar = Bar(2);
};
int main()
{
App app;
Bar bar = Bar(10);
Foo foo = Foo(5);
std::cout << "foo before:" << std::endl;
std::cout << &foo.a << std::endl;
std::cout << foo.b << std::endl;
foo = Foo(20);
std::cout << "foo after:" << std::endl;
std::cout << &foo.a << std::endl;
std::cout << foo.b << std::endl;
std::cout << "bar.nestedFoo:" << std::endl;
std::cout << &bar.nestedFoo.a << std::endl;
std::cout << bar.nestedFoo.b << std::endl;
std::cout << "app.bar.nestedFoo:" << std::endl;
std::cout << &app.bar.nestedFoo.a << std::endl;
std::cout << app.bar.nestedFoo.b << std::endl;
return 0;
}
I expected the pointer and the address of the member to be the same after the reassignment but it seems this is not the case, &Foo.a and Foo.b have different values:
bar constructor nestedFoo before:
0058FEE8
0058FEE8
bar constructor nestedFoo after:
0058FEE8
0058FC24
bar constructor nestedFoo before:
0058FED8
0058FED8
bar constructor nestedFoo after:
0058FED8
0058FD04
foo before:
0058FEC8
0058FEC8
foo after:
0058FEC8
0058FDF8
bar.nestedFoo:
0058FED8
0058FD04
app.bar.nestedFoo:
0058FEE8
0058FC24
At first I thought it only happened when I had Foo as a member of another struct, but it seems to happen even at a top-level assignment. So what am I doing wrong?
Any help will be appreciated, thank you!
Edit: showing that assigning b to &c which is a member that doesn't get (explicitly) reassigned also doesn't work.
After compiling the following program i get this error:
char const* func(char const* a, char const* b)
{
return std::strcmp(a,b)<0?b:a;
}
char const*& func(char const*& a, char const*& b,char const*& c)
{
return func(func(a,b),c);
}
int main()
{
const char* a="First";
const char* b="Second";
const char* c="Third";
func(func(a,b),c); //ERROR
return 0;
}
Questions:
1. The error is thrown because of func(a,b) return value but i dont know the reason for it.
2. Why is the temporary created while returning from the func(char const*,char const*)? IS it created because caller side doesnt have a variable to collect the returned value?
EDIT : This Error has been discussed in other threads but the scenario is different(though the reason hopefully has to be the same) in this case hence i have asked this question because the explanation is not evident in this scenario. Please dont mark it as duplicate.
I misjudged your question at first, because I misread your code. Actually, in your code char const*& func(char const*& a, char const*& b,char const*& c) is never called ;)
This part of your code actually works correctly:
#include<cstring>
char const* func(char const* a, char const* b)
{
return std::strcmp(a, b)<0 ? b : a;
}
#include<iostream>
int main()
{
const char* a = "First";
const char* b = "Second";
const char* c = "Third";
std::cout << func(func(a, b), c) << std::endl;
std::cin.ignore();
return 0;
}
which returns "third"
The problem is created when you change func(func(a, b), c) in main into func(a, b, c). This is because you are combining pointers with references to pointers. This is possible to do, but not the way you do it.
Now if you call func(a, b, c), all the arguments are passed to the second function by reference. To demonstrate what that means, consider this code:
#include<iostream>
int main()
{
const char* a = "First";
std::cout << "a: " << (void*)a << ", &a:" << &a << std::endl;
const char* b = "Second";
std::cout << "b: " << (void*)b << ", &b:" << &b << std::endl;
const char* c = "Third";
std::cout << "c: " << (void*)c << ", &c:" << &c << std::endl;
std::cin.ignore();
return 0;
}
For me this returns:
a: 00938B30, &a:00B9FEEC
b: 00938BE4, &b:00B9FEE0
c: 00938BF4, &c:00B9FED4
See? a is a pointer which contains an address, but there is also an address that points to a!. These are passed when you call by reference.
However, within this (second func) function, you call the first func which copies the pointer values, as it is not passing arguments by reference. These local copies are temporary values, that are destroyed once the functions goes out of scope. The problem occurs are the second (func) function tries to write the address of this local copy to the value called by reference.
OK, I don't know if you're following me here. Let's demonstrate what's happening in memory by an example:
#include<cstring>
#include<iostream>
char const* func(char const* const a, char const* const b)
{
std::cout << "Func1:\n";
std::cout << "local: a: " << (void*)a << "<--copy, &a:" << &a << "<--*different from main!*\n";
std::cout << "local: b: " << (void*)b << "<--copy, &b:" << &b << "<--*different from main!*\n";
return std::strcmp(a, b)<0 ? b : a;
}
char const*& func(char const*& a, char const*& b, char const*& c)
{
std::cout << "Func2:\n";
std::cout << "ref: a: " << (void*)a << "<--original, &a:" << &a << "<--original\n";
std::cout << "ref: b: " << (void*)b << "<--original, &b:" << &b << "<--original\n";
std::cout << "ref: c: " << (void*)c << "<--original, &c:" << &c << "<--original\n";
char const* const out = func(func(a, b), c);
std::cout << "Func2:\n";
std::cout << "local: out: " << (void*)out << ", &out:" << &out << " (cannot return this local variable to a reference)\n";
return c; // just to fix it for now
}
int main()
{
std::cout << "Main:\n";
const char* a = "First";
std::cout << "a: " << (void*)a << ", &a:" << &a << "\n";
const char* b = "Second";
std::cout << "b: " << (void*)b << ", &b:" << &b << "\n";
const char* c = "Third";
std::cout << "c: " << (void*)c << ", &c:" << &c << "\n";
func(a, b, c);
std::cin.ignore();
return 0;
}
For me this returns:
Main:
a: 00F38D00, &a:005EF78C
b: 00F38C00, &b:005EF780
c: 00F38D08, &c:005EF774
Func2:
ref: a: 00F38D00<--original, &a:005EF78C<--original
ref: b: 00F38C00<--original, &b:005EF780<--original
ref: c: 00F38D08<--original, &c:005EF774<--original
Func1:
local: a: 00F38D00<--copy, &a:005EF5A4<--*different from main!*
local: b: 00F38C00<--copy, &b:005EF5A8<--*different from main!*
Func1:
local: a: 00F38C00<--copy, &a:005EF5A8<--*different from main!*
local: b: 00F38D08<--copy, &b:005EF5AC<--*different from main!*
Func2:
local: out: 00F38D08, &out:005EF680 (cannot return this local variable to a reference)
live demo
I hope this is clear enough for you
If static variables has only one copy for the program. So why is it not possible to swap 2 numbers using another function?
Code:
#include <iostream>
void swap(int, int);
int main()
{
static int a = 1;
static int b = 2;
swap(a, b);
std::cout << "a = " << a << std::endl << "b = " << b << std::endl;
std::cin.get();
}
void swap(int a,int b)
{
int temp = a;
a = b;
b = temp;
std::cout << "a = " << a << std::endl << "b = " << b << std::endl;
}
As the 'swap' function is taking parameters as pass by value, copies of the variables are passed to the swap function which will only swap its local variables 'a' and 'b' (passed as parameter) not the static ones passed from main.
Swap should be taking parameters as references like below.
#include <iostream>
void swap(int&, int&);
int main()
{
static int a = 1;
static int b = 2;
swap(a, b);
std::cout << "a = " << a << std::endl << "b = " << b << std::endl;
std::cin.get();
}
void swap(int &a,int &b)
{
int temp = a;
a = b;
b = temp;
std::cout << "a = " << a << std::endl << "b = " << b << std::endl;
}
Please note that static variable defined in a function pertains its value in the subsequent calls of the function in which it is declared.
This is because you are passing arguments by value and not by address(reference). Your function is working on a copy of a and a copy of b - not the original values. You could try this(notice the & in the function prototype and function definition arguments)
void swap(int &, int &);
void swap(int& a, int& b)
{
int temp = a;
a = b;
b = temp;
std::cout << "a = " << a << std::endl << "b = " << b << std::endl;
}
I've noticed some behaviour which I can't understand in parameterized constructors. Given the following program:
#include <iostream>
using namespace std;
class A {
public:
int x;
A() {}
A(int i) : x(i){
cout << "A\n";
}
~A(){
cout << "dA\n";
}
};
int main(){
A p;
p = 3;
cout << p.x << endl;
p = 5;
cout << p.x << endl;
return 0;
}
I get as output:
A
dA
3
A
dA
5
dA
This means that using = triggers the parameterized constructor, destroys the object on which it's called and creates a new object.
I cannot understand this behaviour and I can't find the answer in the standard ( I am sure it is there somewhere, but it may be stated in a sophisticated way). Could someone help me with an explanation?
The phrase you're probably looking for is "implicit conversion".
If you add a copy constructor and an assignment operator, and then give each object a unique ID, it's easier to see where things go:
int counter = 0;
class A {
public:
int id;
A(): id(++counter) {cout << "A(): " << id << "\n";}
A(int i) : id(++counter) {cout << "A(" << i << "): " << id << "\n";}
// Don't copy the id.
// (This isn't used anywhere, but you can't see that it's not used unless it exists.)
A(const A& a) : id(++counter) {cout << "A(" << a.id << "): " << id << "\n";}
// Don't copy the id here either.
A& operator=(const A&a) {cout << id << " = " << a.id << "\n"; return *this;}
~A(){cout << "destroy: " << id << "\n";}
};
int main(){
A p;
cout << "p is " << p.id << "\n";
p = 3;
cout << "p is " << p.id << "\n";
p = 5;
cout << p.id << "\n";
}
Output:
A(): 1
p is 1
A(3): 2
1 = 2
destroy: 2
p is 1
A(5): 3
1 = 3
destroy: 3
1
destroy: 1
As you can see, the parameterized constructor is used to create a temporary object whose value can be assigned to p, and that temporary is destroyed immediately after that.
You can also see that p is alive and well until the very end.
With a statement like
p = 3;
what you're actually doing is
p = A(3);
which really translates to
p.operator=(A(3));
The temporary A object created by A(3) of course needs to be destructed, it is temporary after all.
The object p itself will not be destructed by the assignment.