当前位置:网站首页>Temporary objects and compilation optimization
Temporary objects and compilation optimization
2022-06-13 01:22:00 【Zhao_| adult】
1. Temporary objects and compilation optimization
C++ in Temporary objects , Also known as nameless objects , Temporary objects mainly appear in the following scenarios :
- When a constructor is converted as an implicit type , Will create temporary objects , Pass as an argument to a function ;
#include <iostream>
using namespace std;
class Integer
{
public:
Integer(int i) : m_ival(i) // No, explicit Display conversion limits Indicates that it can be implicitly converted int ==> Integer
{
cout << "Interger (int i) construct:" << ++m_constructs << endl;
}
explicit Integer(double d) : m_dval(d)
{
cout << "Integer(double d) construct:" << ++m_constructs << endl;
}
Integer(const Integer &integer)
{
cout << "Interger copy construct:" << ++m_copy_constructs << endl;
}
~Integer()
{
cout << "Interger desconstruct:" << ++m_desconstructs << endl;
}
private:
int m_ival;
double m_dval;
static int m_constructs;
static int m_desconstructs;
static int m_copy_constructs;
};
int Integer::m_constructs = 0;
int Integer::m_desconstructs = 0;
int Integer::m_copy_constructs = 0;
void Func(Integer integer)
{
cout << "--Func--" << endl;
}
int main()
{
int i = 10;
Func(i);
return 0;
}
/*g++ -g main.cpp -fno-elide-constructors Ignore compiler optimizations Output information : Interger (int i) construct:1 Interger copy construct:1 --Func-- Interger desconstruct:1 Interger desconstruct:2 First Func(i); The parameter type passed is int != Integer; By constructor Integer(int i) Construct a temporary object template_object; ==> Interger (int i) construct:1 Calling Func(Integer integer) when , The temporary object template_object Assign a value to integer ; ==> Interger copy construct:1 then Func(i) Execution output ; ==> --Func-- Func(i) At the end of execution Temporary object deconstruction ; ==> Interger desconstruct:1 Func(i) At the end of execution integer destructor : ==> Interger desconstruct:2 */
- Create an unnamed non heap object , That is, when an unknown object , A temporary object will be generated ;
Such as :
Class name (< parameter list >); // Created a temporary object
- Function returns an object , Temporary objects will be generated , The return value in the function will be copied to a temporary object in the stack of the called function in the form of value copy ;
Integer Func()
{
Integer inter(5);
return inter;
}
int main()
{
Integer re=Func();
return 0;
}
/* Interger (int i) construct:1 Interger copy construct:1 Interger desconstruct:1 Interger copy construct:2 Interger desconstruct:2 Interger desconstruct:3 1. main Call in Func(); 1. Integer inter(5); Call constructor Integer(int i) ==> Interger (int i) construct:1; 2. return inter; Will Func Function stack inter Save to a temporary object template_object in :==> Interger copy construct:1 3. Func() After the function is executed ,inter destructor : ==> Interger desconstruct:1 4. Temporary objects template_object Copied to the re in : ==> Interger copy construct:2 5. Temporary objects template_object destructor ; ==> Interger desconstruct:2 6. main End of the function re destructor ; ==> Interger desconstruct:3 */

2. Compile optimization -fno-elide-constructors
Because the generation of temporary objects will not only occupy computer resources ( Copy many times ), And sometimes it will lead to program errors , So the compiler optimizes the program , Minimize the generation of temporary objects .
-fno-elide-constructors:
The C++ standard allows an implementation to omit creating a temporary that is only used to initialize another object of the same type. Specifying this option disables that optimization, and forces G++ to call the copy constructor in all cases.
stay C++11 Before the R-value reference appears ,C++ The temporary object problem of brings a very large performance overhead , This optimization of the compiler , A lot of unnecessary copy;
3. Example
#include <iostream>
using namespace std;
class HasPtrMem
{
public:
HasPtrMem() : d(new int(0))
{
cout << "Construct: " << ++n_cstr << endl;
}
HasPtrMem(const HasPtrMem &h) : d(new int(*h.d))
{
cout << "Copy construct: " << ++n_cptr << endl;
}
~HasPtrMem()
{
cout << "Destruct: " << ++n_dstr << endl;
}
int *d;
static int n_cstr;
static int n_dstr;
static int n_cptr;
};
int HasPtrMem::n_cstr = 0;
int HasPtrMem::n_dstr = 0;
int HasPtrMem::n_cptr = 0;
HasPtrMem GetTemp()
{
return HasPtrMem();
}
int main()
{
HasPtrMem object = GetTemp();
return 0;
}
/* g++ -g main.cpp -fno-elide-constructors Ignore compiler optimizations * Construct: 1 Copy construct: 1 Destruct: 1 Copy construct: 2 Destruct: 2 Destruct: 3 1. call GetTemp() Temporary objects will be created and generated twice during the process : 1. HasPtrMem(); ==> Will create temporary objects template_object1 ==> Construct: 1 2. return , Return temporary object template_object1 To tempate_object2 , Then destruct template_object1; 3. And then template_object2 assignment to object after ,template_object2 destructor ; 4. main End of the function , object destructor ; */
/* g++ -g main.cpp Compiler optimization is not ignored * Construct: 1 * Destruct: 1 */
边栏推荐
- Leetcode 01 array
- Ecological convergence NFT attacks, metaverse ape leads the new paradigm revolution of Web 3.0 meta universe
- Simple operation of MySQL database
- [latex] insérer une image
- Facial expression recognition dataset
- Web Application & applet application deployment
- 五篇经典好文,值得一看(2)
- MySQL related summary
- Sonarqube local installation
- Leetcode-17- letter combination of phone number (medium)
猜你喜欢

在国企做软件测试工程师是一种什么样的体验:每天过的像打仗一样

Five classic articles worth reading (2)

【斯坦福计网CS144项目】Lab1: StreamReassembler

Deadlock problem summary

Simple operation of MySQL database
![[Latex] 插入图片](/img/0b/3304aaa03d3fea3ebb93b0348c3131.png)
[Latex] 插入图片
![[Stanford Jiwang cs144 project] lab1: streamreassembler](/img/7b/fad18b68a6ee30d1dec4dad6273b98.png)
[Stanford Jiwang cs144 project] lab1: streamreassembler
![[latex] insérer une image](/img/0b/3304aaa03d3fea3ebb93b0348c3131.png)
[latex] insérer une image

Page optimization - Notes

Several categories of software testing are clear at a glance
随机推荐
DFS and BFS notes (II): depth first search (implemented in C language)
Idea installation tutorial
redis
Five classic articles worth reading
[从零开始学习FPGA编程-22]:进阶篇 - 架构 - FPGA内部硬件电路的设计与建模
ES6解构赋值
Wikipedia User Guide
数学知识整理:极值&最值,驻点,拉格朗日乘子
关于#数据库#的问题,如何解决?
[Latex] 插入圖片
Page optimization - Notes
Sonarqube local installation
MySQL transaction
Tangent and tangent plane
Stack and queue practice (C language): Demon King's language
np.concatenate中axis的理解
生态聚合NFT来袭,Metaverse Ape引领Web 3.0元宇宙新范式革命
使用Pygame创建一个简单游戏界面
Wal mechanism of MySQL
How to solve the problem of database?