Problems with c++ lifetime extension - c++

I've tried to understand the semantics of c++ temporary objects lifetime extension. I've tried to simulate simple situation and was a bit surprised.
Below I'm providing my code.
#include <iostream>
struct C
{
C(const int new_a) { a = new_a; };
int a = 0;
};
C return_num()
{
C num(20);
std::cout << "From func(): num = " << num.a << ", by adress: " << &num.a << std::endl;
return num;
}
void pass_num(const C& num)
{
std::cout << "From func(): num = " << num.a << ", by adress: " << &num.a << std::endl;
}
int main()
{
std::cout << "\nLifetime extention:" << std::endl;
{
const C& ext_num = return_num();
std::cout << "From main(): num = " << ext_num.a << ", by adress: " << &ext_num.a << std::endl;
}
std::cout << "\nPassing by reference:" << std::endl;
{
C num(20);
std::cout << "From main(): num = " << num.a << ", by adress: " << &num.a << std::endl;
pass_num(num);
}
}
Here is the main question: return_num() works curiously from my point of view, cause I expected that address of the variable, which I'm trying to output in main, would be the same as internally in return_num(). Could you please explain me why it is not?
For example in pass_num() output address matches the external address which I got in main.
Here is example output:
Lifetime extention:
From func(): num = 20, by adress: 0x7fff44fc8b4c
From main(): num = 20, by adress: 0x7fff44fc8b70
Passing by reference:
From main(): num = 20, by adress: 0x7fff44fc8b6c
From func(): num = 20, by adress: 0x7fff44fc8b6c

Move constructors typically "steal" the resources held by the argument (e.g. pointers to dynamically-allocated objects, file descriptors, TCP sockets, I/O streams, running threads, etc.) rather than make copies of them, and leave the argument in some valid but otherwise indeterminate state.
Please see Move Constructor
I changed the below in your code and I hope it is working as expected. I changed int a to int* a
#include <iostream>
class C
{
public:
int *a;
C( int new_a)
{
a = new int();
*a = new_a;
};
C(const C& rhs) { std::cout << "Copy " << std::endl; this->a = rhs.a; }
C(C&& rhs):a(std::move(rhs.a))
{
std::cout << "Move!!" <<"Address resource a " << &(*a) << ", Address of
resource rhs.a" << &(*rhs.a) << std::endl; rhs.a = nullptr;
std::cout << "Value of a:: " << *a << std::endl;
}
};
C return_num()
{
C num(20);
std::cout << "From return_num(): num = " << *num.a << ", Address of resource a :
"<< &(*num.a)<< std::endl;
return (std::move(num));
}
void pass_num(const C& num)
{
std::cout << "From pass_num(): num = " << *num.a << ", by adress: " << &num.a <<
std::endl;
}
int main()
{
std::cout << "\nLifetime extention:" << std::endl;
{
const C& ext_num = return_num();
std::cout << "From main() 1 : num = " << *(ext_num.a) << ", by resource
adress: " << &(*ext_num.a) << std::endl;
}
std::cout << "\nPassing by reference:" << std::endl;
{
C num(20);
std::cout << "From main() 2 : num = " << *num.a << ", by adress: " << &num.a
<< std::endl;
pass_num(num);
}
return 0;
}
The above code produces below output:
Lifetime extention:
From return_num(): num = 20, Address of resource a : 0x7fffeca99280
Move!!Address resource a 0x7fffeca99280, Address of resource rhs.a0x7fffeca99280
Value of a:: 20
From main() 1 : num = 20, by resource adress: 0x7fffeca99280
Passing by reference:
From main() 2 : num = 20, by adress: 0x7ffff466f388
From pass_num(): num = 20, by adress: 0x7ffff466f388
I hope it helps!

Imagine this function:
int getNumber(){
int num = 10;
return num;
}
This function does not return num as an object, it returns a no-named copy of it (r-value, if you will) with the same value. Therefore, it has a different address.
The same thing happens with your return_num function.

I suspect that taking the address of a member is inhibiting the optimization because the compiler doesn't know how to deal with all the possible edge cases. Eliminating taking the address of the member makes the optimization work.
#include <iostream>
struct C
{
C(const int new_a) { a = new_a; };
int a = 0;
struct C* t = this;
};
C return_num()
{
C num(20);
std::cout << "From func(): num = " << num.a << ", by adress: " << num.t << std::endl;
return num;
}
void pass_num(const C& num)
{
std::cout << "From func(): num = " << num.a << ", by adress: " << num.t << std::endl;
}
int main()
{
std::cout << "\nLifetime extention:" << std::endl;
{
const C& ext_num = return_num();
std::cout << "From main(): num = " << ext_num.a << ", by adress: " << ext_num.t << std::endl;
}
std::cout << "\nPassing by reference:" << std::endl;
{
C num(20);
std::cout << "From main(): num = " << num.a << ", by adress: " << num.t << std::endl;
pass_num(num);
}
}
Lifetime extention:
From func(): num = 20, by adress: 0x7ffd61f48a50
From main(): num = 20, by adress: 0x7ffd61f48a50
Passing by reference:
From main(): num = 20, by adress: 0x7ffd61f48a90
From func(): num = 20, by adress: 0x7ffd61f48a90

Related

Questions about right value reference in C++

#include <iostream>
using namespace std;
void swap(int& a, int& b)
{
cout << "address of a: " << &a << " value of a: " << a << endl;
cout << "address of b: " << &b << " value of b: " << b << endl;
int tmp{move(a)};
cout << "address of tmp: " << &tmp << " value of tmp: " << tmp << endl;
a = move(b);
b = move(tmp);
cout << "address of a: " << &a << " value of a: " << a << endl;
cout << "address of b: " << &b << " value of b: " << b << endl;
}
void swap_no_move(int& a, int& b)
{
cout << "address of a: " << &a << " value of a: " << a << endl;
cout << "address of b: " << &b << " value of b: " << b << endl;
int tmp{ a };
cout << "address of tmp: " << &tmp << " value of tmp: " << tmp << endl;
a = b;
b = tmp;
cout << "address of a: " << &a << " value of a: " << a << endl;
cout << "address of b: " << &b << " value of b: " << b << endl;
}
int main() {
int a = 10;
int b = 5;
swap(a, b);
cout << endl;
int c = 10;
int d = 5;
swap_no_move(c, d);
cin.get();
return 0;
}
I have two swap functions: swap and swap_no_move. According to what I read from the book, there should be no "copy" in function swap which means the address of tmp should be the same for tmp and an in function swap. However, the output I got shows there is no difference between these two functions, did I do something wrong?
The definition
int tmp{move(a)};
doesn't move the reference or the variable a itself. It creates a brand new variable tmp which the compiler allocates space for. Then the value of a is moved into tmp.
And since moving int values can't really be done, it's exactly the same as
int tmp = a;

How does shared_ptr increase counter when passed by value?

I have this sample code below. I know little bit about RVO (return value optimization) and how copy constructor and assignment operator are skipped during the optimization and return of the value is placed directly on the memory on the left. So if shared pointer does the RVO how does the shared pointer know when to increase its counter? Because for some reason I thought shared pointer class would know when to increase counter based on the number copies or assignment it made.
#include <iostream>
#include <memory>
using namespace std;
class A{
public:
A(){}
A(const A& other){ std::cout << " Copy Constructor " << std::endl; }
A& operator=(const A&other){
std::cout << "Assingment operator " << std::endl;
return *this;
}
~A(){
std::cout << "~A" << std::endl;
}
};
std::shared_ptr<A> give_me_A(){
std::shared_ptr<A> sp(new A);
return sp;
}
void pass_shared_ptr_by_val(std::shared_ptr<A> sp){
std::cout << __func__ << ": count sp = " << sp.use_count() << std::endl;
std::shared_ptr<A> sp1 = sp;
std::cout << __func__ << ": count sp = " << sp.use_count() << std::endl;
std::cout << __func__ << ": count sp1 = " << sp1.use_count() << std::endl;
}
void pass_shared_ptr_by_ref(std::shared_ptr<A>& sp){
std::cout << __func__ << ": count sp = " << sp.use_count() << std::endl;
std::shared_ptr<A> sp1 = sp;
std::cout << __func__ << ": count sp = " << sp.use_count() << std::endl;
std::cout << __func__ << ": count sp1 = " << sp1.use_count() << std::endl;
}
int main(){
{
shared_ptr<A> sp3 = give_me_A();
std::cout << "sp3 count = " << sp3.use_count() << std::endl;
pass_shared_ptr_by_val(sp3);
pass_shared_ptr_by_ref(sp3);
}
return 0;
}
output:
sp3 count = 1
pass_shared_ptr_by_val: count sp = 2
pass_shared_ptr_by_val: count sp = 3
pass_shared_ptr_by_val: count sp1 = 3
pass_shared_ptr_by_ref: count sp = 1
pass_shared_ptr_by_ref: count sp = 2
pass_shared_ptr_by_ref: count sp1 = 2
~A
If there is no copy, nothing needs to be counted.
If RVO is in play no copy is made, so why would the ref-count need to be increased? There is no extra object to destroy and decrement the ref-count.

Explain C++ pointer initialization

int value = 3;
int *pValue1 = &value;
int *pValue2(pValue1);
cout << (*pValue1) << " " << (*pValue2);
In the above code if you have noticed I have written
int *pValue2(pValue1);
instead of
int *pValue2 = new int;
pValue2 = pValue1;
Still it is working and giving proper result.
Can any one explain to me which of the default function or constructor is getting called in this case?
int *pValue2(pValue1);
is equivalent to
int* pValue2 = pValue1;
Just assign to pValue2 pValue1 (assign to pValue2 address of variable value).
The difference should be apparent if you print the pointers themselves (the addresses) in addition to the values which they reference:
#include <iostream>
using namespace std;
int main() {
int value = 3;
int *pValue1 = &value;
int *pValue2(pValue1);
int *pValue3 = new int;
cout << pValue1 << " " << pValue2 << " " << pValue3 << endl;
cout << *pValue1 << " " << *pValue2 << " " << *pValue3 << endl;
pValue3 = pValue1;
cout << pValue1 << " " << pValue2 << " " << pValue3 << endl;
cout << *pValue1 << " " << *pValue2 << " " << *pValue3 << endl;
return 0;
}
You will also see that after new int, the memory pointed to by the pointer contains uninitialized data.

User Input to a Value Stored in an Integer Array C++

I'm trying to make it so every time the user inputs a value, it's stored in an array in a place that represents a stat. Hard to explain so here's the code:
void MainChar::CharacterCreation()
{
int statPoints = 20;
int health = 0;
int magicDamage = 0;
int magicResist = 0;
int physicalResist = 0;
int physicalDamage = 0;
int magicOffMastery = 0;
int physicalOffMastery = 0;
int magicDefMastery = 0;
int physicalDefMastery = 0;
// SETS STATS AND THIER RESPECTIVE ARRAY PLACEMENT
int statArray[9];
statArray[0] = health;
statArray[1] = magicDamage;
statArray[2] = magicResist;
statArray[3] = physicalResist;
statArray[4] = physicalDamage;
statArray[5] = magicOffMastery;
statArray[6] = physicalOffMastery;
statArray[7] = magicDefMastery;
statArray[8] = physicalDefMastery;
std::string stats[9];
stats[0] = "Health : " ;
stats[1] = "Magic Damage : " ;
stats[2] = "Magic Resist : " ;
stats[3] = "Physical Resist : " ;
stats[4] = "Physical Damage : " ;
stats[5] = "Magic Offensive Mastery : " ;
stats[6] = "Physical Offensive Mastery : ";
stats[7] = "Magic Defensive Mastery : " ;
stats[8] = "Physical Defensive Mastery : ";
int statString = 0;
int statInt = 0;
while (statPoints > 0)
{
std::cout << "*******************************************************************************" << std::endl;
std::cout << "* *" << std::endl;
std::cout << "* CHARACTER CREATION *" << std::endl;
std::cout << "* *" << std::endl;
std::cout << "*******************************************************************************" << std::endl;
std::cout << " Health : " << health * 10 << std::endl;
std::cout << std::endl;
std::cout << " Magic Damage : " << magicDamage << std::endl;
std::cout << std::endl;
std::cout << " Magic Resist : " << magicResist << std::endl;
std::cout << std::endl;
std::cout << " Physical Resist : " << physicalResist << std::endl;
std::cout << std::endl;
std::cout << " Physical Damage : " << physicalDamage << std::endl;
std::cout << std::endl;
std::cout << " Magic Offensive Mastery : " << magicOffMastery << std::endl;
std::cout << std::endl;
std::cout << " Physical Offensive Mastery : " << physicalOffMastery << std::endl;
std::cout << std::endl;
std::cout << " Magic Defensive Mastery : " << magicDefMastery << std::endl;
std::cout << std::endl;
std::cout << " Physical Defensive Mastery : " << physicalDefMastery << std::endl;
std::cout << "*******************************************************************************" << std::endl;
std::cout << "STAT POINTS: " << statPoints << std::endl;
std::cout << stats[statString] ;
std::cin >> statArray[statInt] ;
statPoints -= statArray[statInt];
++statString;
++statInt;
}
}
As you might notice, I'm trying to have the user change the value of health, which is stored in statArray[statInt], which equates to statArray[0], then I ++statInt. The idea was I'd be able to have the user input all his stats one at a time. Instead of my intention, whats happening is it's taking the user input as a reference to the array slot. (statArray[0], statArray[1]) etc. Instead of the stat associated with that array slot.
To achieve what you want to do you could use a union:
union Stat {
struct {
int health;
int magicDamage;
int magicResist;
// ...
};
int array[3];
};
int main() {
Stat stat;
stat.array[1] = 42;
cout<<stat.magicDamage<<endl; // should give 42
};
However a better solution would be to use a map:
map<string,int> stat;
const char *keys[] = {"health","magicDamage","magicResist"};
int main() {
for (int i=0;i<3; ++i) {
cout<<"enter "<<keys[i]<<endl;
cin>>stat[keys[i]];
}
}
Change statArraydefinition to use pointers instead of values
int *statArray[9];
statArray[0] = &health;
statArray[1] = &magicDamage;
statArray[2] = &magicResist;
statArray[3] = &physicalResist;
statArray[4] = &physicalDamage;
statArray[5] = &magicOffMastery;
statArray[6] = &physicalOffMastery;
statArray[7] = &magicDefMastery;
statArray[8] = &physicalDefMastery;
And update next lines:
std::cout << *stats[statString] ;
std::cin >> *statArray[statInt] ;
statPoints -= *statArray[statInt];

c++ Functions (with body) as Argument

I want to pass an function as argument. I know you can pass a function pointer like the first test in my example, but is it possible to pass a hold function (not a pointer) like my second test?
#include <iostream>
using namespace std;
/* variable for function pointer */
void (*func)(int);
/* default output function */
void my_default(int x) {
cout << "x =" << "\t" << x << endl << endl;
}
/* entry */
int main() {
cout << "Test Programm\n\n";
/* 1. Test - default output function */
cout << "my_default\n";
func = &my_default; // WORK! OK!
func(5);
/* 2. Test - special output function 2 */
cout << "my_func2\n";
func = void my_func1(int x) {
cout << "x =" << " " << x << endl << endl;
}; // WON'T WORK! FAILED!
func(5);
return 0;
}
In C++ 11, you can pass a lambda:
func = [](int x) { cout << "x =" << " " << x << endl << endl; };
EDIT: lambdas can return values:
func = [](int x)->int{ cout << "x =" << " " << x << endl << endl; return x; };
With c++11, you can write such code in a lot simpler way:
Instead of writing function signature use auto
auto func = &my_default; // WORK! OK!
func(5);
you can also use std::function objects to pass them around:
template<typename T>
void callme(std::function<T> f) {
f(6);
}
std::function<decltype(my_default)> func1 = &my_default;
func(5);
callme(func1);
and also you can use lambdas:
/* 2. Test - special output function 2 */
cout << "my_func2\n";
auto fun = [](int x) {
cout << "x =" << " " << x << endl << endl;
};
fun(5);
Even with return value it work:
#include <iostream>
using namespace std;
/* variable for function pointer */
int (*func)(int);
/* default output function */
int my_default(int x) {
//cout << "x =" << "\t" << x << endl << endl;
return x;
}
/* entry */
int main() {
cout << "Test Programm\n\n";
/* 1. Test - default output function */
cout << "my_default\n";
func = &my_default; // WORK! OK!
cout << func(5) << endl << endl;
/* 2. Test - special output function 2 */
cout << "my_func2\n";
func = [](int x) {
//cout << "x =" << " " << x << endl << endl;
return x;
};
cout << func(5) << endl << endl;
return 0;
}
You can uses lamdas:
std::function<int(int)> func2 = [](int i) { return i+4; };
std::cout << "func2: " << func2(6) << '\n';
if the body consists of the single return statement, the return type
is the type of the returned expression (after rvalue-to-lvalue,
array-to-pointer, or function-to-pointer implicit conversion)
If you lamda constains not single return statamen you should specify return type
std::function<int(int)> func2 = [=](int i) ->int {
if (globalVar)
return i*4;
else
return 4;
};
std::cout << "func2: " << func2(6) << '\n';