[C/C++安全编程]_[中级]_[如何安全使用循环语句]

场景

  1. 在标准C里,使用循环语句也是有可能造成越界或死循环的,那么在现代C++开发里能避免吗?

说明

  1. 在C++11标准里,新增了一个范围for循环语句,可以快速枚举集合,数组,并且不需要索引操作。 这个是正向枚举的推荐操作,可以避免越界。
cpp 复制代码
for (const auto& one : array_1)
		cout << one << endl;
  1. 在C++98时可以使用<algorithm>算法库的std::for_each来执行枚举,效果等同于使用迭代器枚举。不涉及到索引值操作,可以安全的。
cpp 复制代码
for (auto ite = array_1.begin(); ite != array_1.end(); ++ite) {
		cout << *ite << endl;
}
  1. 在倒序枚举时,C++也有倒序迭代器可以用,也是安全的。
cpp 复制代码
for (auto ite = array_1.rbegin(); ite != array_1.rend(); ++ite) {
		cout << *ite << endl;
}
  1. 如果非要在倒序时使用索引值,那么要注意循环变量不要声明为无符号常量。比如size_t就是无符号常量,是集合方法.size()的返回类型。因为无符号常量在递减的时候不会出现小于0的情况。
cpp 复制代码
cout << "=== index 1 type ==" << endl;
for (int i = array_1.size() - 1; i >= 0; --i) {  // 使用后置递减,避免 unsigned 类型的问题
	cout << "[" << i << "] => " << array_1[i] << endl;
}
  1. 注意1: 使用goto跳出循环需要跳出全部循环,避免逻辑混乱和死循环。
cpp 复制代码
for (auto i = 0; i < 100; ++i) {
	for (auto j = 0; j < 100; ++j) {
		if ( i && j && (j % 10 == 0)) {
			cout << "i: " << i << " j: " << j << endl;
			goto next;
		}
			
	}
}
next:
  1. 避免修改循环变量,避免逻辑混乱,死循环和越界访问。
cpp 复制代码
for (auto i = 0; i < 100; ++i) {
	if (i && (i % 5) == 0)
		i--;
}
  1. 不要使用浮点型作为循环变量,因为浮点型有精度问题,可能会导致条件不成立。

    • 浮点型的值i可能永远不会达到10.0f导致死循环。

    • 浮点型运算比整型慢的多。

    cpp 复制代码
    for(auto i = 1.0f; i != 10.0f; i+= 0.1f)

例子

cpp 复制代码
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;


void TestDecreaseLoop()
{
	vector<int> array_1 = {4,5,9,2,3,1,0,3,2};
	// 1. 优先选择: 使用反向迭代器; 不涉及到索引的修改,不会出错。
	cout << "=== iterator ==" << endl;
	for (auto ite = array_1.rbegin(); ite != array_1.rend(); ++ite) {
		cout << *ite << endl;
	}
		
	// 2. 如果需要索引,注意使用`int`声明循环变量,如果使用`auto`,那么会是`size_t`类型;
	// -- 而`size_t`是无符号整型,不会出现小于`0`的情况。
	cout << "=== index 1 type ==" << endl;
	for (int i = array_1.size() - 1; i >= 0; --i) {  // 使用后置递减,避免 unsigned 类型的问题
		cout << "[" << i << "] => " << array_1[i] << endl;
    }

	// 错误:i 一直大于等于`0`,死循环。
	//for (auto i = array_1.size() - 1; i >= 0; --i) {
	//	cout << "[" << i << "] => " << array_1[i] << endl;
	//}


	// -- 另一种获取索引的方式
	cout << "=== index 2 type ==" << endl;
	auto iend = array_1.rend();
	for (auto ite = array_1.rbegin(); ite != iend; ++ite) {
		cout << "[" << iend - ite - 1 << "] => " << *ite << endl;
	}
}

void processOne(int& one) 
{
	cout << one << endl;
}

void TestIncreaseLoop()
{
	vector<int> array_1 = {4,5,9,2,3,1,0,3,2};
	// 1. 优先使用范围`for`循环,避免使用索引
	cout << "=== range for ==" << endl;
	for (const auto& one : array_1)
		cout << one << endl;

	// 2. 如果是`C++98`
	cout << "=== for_each ==" << endl;
	for_each(array_1.begin(), array_1.end(), processOne);

	// -- 效果等同于使用`iterator`
	cout << "=== iterator ==" << endl;
	for (auto ite = array_1.begin(); ite != array_1.end(); ++ite) {
		cout << *ite << endl;
	}

	// 3. 最后的选择: 需要用到索引时;
	cout << "=== index ==" << endl;
	auto size = array_1.size();
	for (auto i = 0; i < size; ++i) {
		cout << "[" << i << "] => " << array_1[i] << endl;
	}
}

void TestOtherLoop()
{
	// 1. 使用`goto`跳出循环需要跳出全部循环,避免逻辑混乱和死循环。
	for (auto i = 0; i < 100; ++i) {
		for (auto j = 0; j < 100; ++j) {
			if ( i && j && (j % 10 == 0)) {
				cout << "i: " << i << " j: " << j << endl;
				goto next;
			}
				
		}
	}

next:
	cout << "loop finish" << endl;

	// 2. 避免修改循环变量,避免逻辑混乱,死循环和越界访问。
	//for (auto i = 0; i < 100; ++i) {
	//	if (i && (i % 5) == 0)
	//		i--;
	//}

	// 3. 不要使用浮点型作为循环变量,因为浮点型有精度问题,可能会导致条件不成立。
	// -- 浮点型的值`i`可能永远不会达到10.0f导致死循环。
	// -- 浮点型运算比整型慢的多。
	// for(auto i = 1.0f; i != 10.0f; i+= 0.1f)
}

int main()
{
    std::cout << "Hello World!\n";
	cout << "========== TestDecreaseLoop =========" << endl;
	TestDecreaseLoop();

	cout << "========== TestIncreaseLoop =========" << endl;
	TestIncreaseLoop();

	cout << "========== TestOtherLoop =========" << endl;
	TestOtherLoop();
}

输出

cpp 复制代码
Hello World!
========== TestDecreaseLoop =========
=== iterator ==
2
3
0
1
3
2
9
5
4
=== index 1 type ==
[8] => 2
[7] => 3
[6] => 0
[5] => 1
[4] => 3
[3] => 2
[2] => 9
[1] => 5
[0] => 4
=== index 2 type ==
[8] => 2
[7] => 3
[6] => 0
[5] => 1
[4] => 3
[3] => 2
[2] => 9
[1] => 5
[0] => 4
========== TestIncreaseLoop =========
=== range for ==
4
5
9
2
3
1
0
3
2
=== for_each ==
4
5
9
2
3
1
0
3
2
=== iterator ==
4
5
9
2
3
1
0
3
2
=== index ==
[0] => 4
[1] => 5
[2] => 9
[3] => 2
[4] => 3
[5] => 1
[6] => 0
[7] => 3
[8] => 2
========== TestOtherLoop =========
i: 1 j: 10
loop finish

参考

  1. C++ Core Guidelines

  2. C++11语言特性和标准库

相关推荐
个 人 练 习 生7 小时前
C++:从零基础到深入了解vector的使用
开发语言·c++·经验分享·学习·程序人生·stl
ShineWinsu8 小时前
对于Qt窗口:QMainWindow、QMenuBar、Qmenu、QAction的解析
c++·qt·菜单·qmainwindow·qaction·qmenu·qmenubar
H.莓飛8 小时前
【C++】【STL】stack_OJ题
服务器·c语言·开发语言·数据结构·c++·算法·visual studio
Rabitebla9 小时前
【Linux操作系统】 文件篇(一):文件描述符与 Linux 文件管理机制
linux·网络·c++·笔记·学习
旖旎夜光11 小时前
【C++ 基础入门】智能指针:RAII、unique_ptr 与 shared_ptr 循环引用
c++·笔记·学习·智能指针
ShineWinsu13 小时前
对于Qt:QSpacerItem常见属性以及方法的解析
开发语言·c++·qt·面试·系统架构·客户端·qspaceritem
ShineWinsu16 小时前
对于Qt:QGripLayout常见属性、方法以及QSizePolicy的解析
开发语言·c++·qt·面试·gui·客户端·网格
艾莉丝努力练剑16 小时前
【AI大模型接入SDK】C++的SessionManager 线程安全
java·开发语言·前端·c++·人工智能·学习·安全
辰小忆17 小时前
11 · RunCurveChart —— 实时运行曲线
c++·ui·duilib
Hhy_110719 小时前
《C++深度解构07》手撕 vector —— 从底层三指针架构到迭代器失效彻底吃透
c语言·开发语言·c++·学习·visual studio