当前位置:网站首页>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 */
边栏推荐
- Status of the thread
- Wal mechanism of MySQL
- Pipeline流水线项目构建
- Sliding window summary of TCP connections
- Redis usage optimization summary learning
- Leetcode-13- Roman numeral to integer (simple)
- Leetcode question brushing 06 bit operation
- ArrayList underlying source code
- MySQL related summary
- Downloading wiki corpus and aligning with multilingual wikis
猜你喜欢

Realization of flip animation

Alexnet implements image classification of caltech101 dataset (pytorch Implementation)

Status of the thread

My crawler learning notes

MySQL index

Leetcode find duplicates

【斯坦福計網CS144項目】Lab1: StreamReassembler

Sonarqube local installation

MySQL exception: com mysql. jdbc. PacketTooBigException: Packet for query is too large(4223215 > 4194304)

Liu Hui and introduction to nine chapter arithmetic and island arithmetic
随机推荐
Leetcode-15- sum of three numbers (medium)
spiral matrix visit Search a 2D Matrix
How to choose stocks? Which indicator strategy is reliable? Quantitative analysis and comparison of strategic returns of BBI, MTM, obv, CCI and priceosc indicators
工作与生活
Leetcode-11- container with the most water (medium)
[leetcode] valid phone number Bash
Golang context (context summary)
Mathematical knowledge arrangement: extremum & maximum, stagnation point, Lagrange multiplier
Higherhrnet pre training model -- download from network disk
Redis usage optimization summary learning
leetcode. 541. reverse string II
【斯坦福計網CS144項目】Lab1: StreamReassembler
Minimum spanning tree problem
Downloading wiki corpus and aligning with multilingual wikis
redis
RSA encryption colloquial explanation
How to turn on the hotspot for the mobile phone after the computer is connected to the network cable
Common skills of quantitative investment - index part 2: detailed explanation of BOL (Bollinger line) index, its code implementation and drawing
5G工业网关在煤矿行业的应用优势
Leetcode find duplicates