Skip to content

Commit ea03fa2

Browse files
committed
add cxx tips
1 parent 15b2fd9 commit ea03fa2

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

cxx/tips.md

+48
Original file line numberDiff line numberDiff line change
@@ -1276,6 +1276,54 @@ public:
12761276
string3 = string1; // 这样也是不行的, 因为取消了隐式转换, 除非类实现操作符"="的重载
12771277
```
12781278
1279+
### __restrict__
1280+
1281+
restrict 用来定义指针变量,表明该变量没有别名,意思就是:除了该变量以外,没有别的方法可以访问其指向的地址空间。
1282+
1283+
```c++
1284+
/*
1285+
mov eax val;
1286+
add ptrA eax;
1287+
mov eax val;
1288+
add ptrB eax;
1289+
*/
1290+
void update(int *ptrA, int * ptrB, int * val)
1291+
{
1292+
*ptrA +=*val;
1293+
*ptrB +=*val;
1294+
}
1295+
1296+
/*
1297+
其中,变量val加载了两次。因为它不知道ptrA,ptrB是否与val有内存上的交叠,所以为了保险起见,只能每次用到val时都从内存里读。如果加上__restrict__:
1298+
mov eax val;
1299+
add ptrA eax;
1300+
add ptrB eax;
1301+
1302+
*/
1303+
void update_restrict(int * __restrict ptrA, int * __restrictptrB, int * __restrict val);
1304+
1305+
```
1306+
1307+
### register
1308+
1309+
为寄存器变量分配寄存器是动态完成的,因此,只有局部变量和形式参数才能定义为寄存器变量。寄存器的长度一般和机器的字长一致,所以,只有较短的类型如int、char、short等才适合定义为寄存器变量,诸如double等较大的类型,不推荐将其定义为寄存器类型。
1310+
1311+
注意一般在多层循环用
1312+
1313+
```c++
1314+
void floyd(){
1315+
for(register int a=1;a<=v;a++){
1316+
for(register int b=1;b<=v;b++){
1317+
for(register int c=1;c<=v;c++){
1318+
if(G[b][a]+G[a][c]<G[b][c])G[b][c]=G[b][a]+G[a][c];
1319+
}
1320+
}
1321+
}
1322+
return;
1323+
}
1324+
```
1325+
1326+
12791327

12801328
## 构造函数
12811329

0 commit comments

Comments
 (0)