- Hands-On System Programming with C++
- Dr. Rian Quinn
- 415字
- 2021-07-02 14:42:34
Pointers versus references
In the previous section, we discussed pointers in length, including how pointers can take on two values—valid or null (assuming corruption is not part of the equation).
The problem with this is that the user must check whether the pointer is valid or not. This is normally not an issue when using pointers to define the contents of memory (for example, laying out memory using a data structure), but often, pointers in C must be used simply to reduce the overhead of passing a large object to a function, as in the following example:
struct mystruct {
int data1{};
int data2{};
int data3{};
int data4{};
int data5{};
int data6{};
int data7{};
int data8{};
};
void test(mystruct *s)
{
}
int main(void)
{
mystruct s;
test(&s);
}
// > g++ scratchpad.cpp; ./a.out
In the previous example, we create a structure that has eight variables in it. Passing this type of structure with a value would result in the use of the stack (that is, several memory accesses). It is far more efficient to pass this structure with a pointer in C to reduce the cost of passing the structure to a single register, likely removing all memory accesses entirely.
The problem is that, now, the test function must check to make sure the pointer is valid before it can use it. Therefore, the function trades a set of memory accesses for a branch statement and possible pipeline flush in the CPU, when all we are trying to do is reduce the cost of passing a large object to a function.
As stated in the previous section, the solution is to simply not verify the validity of the pointer. In C++, however, we have another option, and that is to pass the structure with a reference, as follows:
struct mystruct {
int data1{};
int data2{};
int data3{};
int data4{};
int data5{};
int data6{};
int data7{};
int data8{};
};
void test(mystruct &s)
{
}
int main(void)
{
mystruct s;
test(s);
}
// > g++ scratchpad.cpp; ./a.out
In the previous example, our test() function takes a reference to mystruct{} instead of a pointer. When we call the test() function, there is no need to get the address of the structure, as we are not using a pointer.
C++ references will be leveraged heavily throughout this book, as they greatly increase both the performance and stability of a program, especially while system programming, where resources, performance, and stability are critical.
- 數據產品經理高效學習手冊:產品設計、技術常識與機器學習
- 數據分析實戰:基于EXCEL和SPSS系列工具的實踐
- Mastering Ninject for Dependency Injection
- Test-Driven Development with Mockito
- 正則表達式必知必會
- Oracle RAC 11g實戰指南
- 分布式數據庫系統:大數據時代新型數據庫技術(第3版)
- R數據科學實戰:工具詳解與案例分析(鮮讀版)
- 深度剖析Hadoop HDFS
- 白話大數據與機器學習
- Lego Mindstorms EV3 Essentials
- Spring Boot 2.0 Cookbook(Second Edition)
- 區塊鏈應用開發指南:業務場景剖析與實戰
- 領域驅動設計精粹
- 數據分析方法及應用:基于SPSS和EXCEL環境