C++Primer第五版:练习3.27 3.28 3.29 3.30 3.31 3.32 3.33

练习2.37

  (a)非法,buf_size不是常量表达式
  (c)非法,txt_size()函数返回值并不是常量表达式,返回值改为constexpr int
  (d)非法,字符串字面值后自带'\0',超出范围

练习3.28
sa和sa2数组各元素都是空字符串,string类有相应默认构造函数
ia各元素值为0,函数外定义的内置类型有初始值
ia2各元素无值,返回相应地址

练习3.29
分配空间固定,增加元素不便,灵活性差
使用时需要制定数组大小

练习3.30
索引从0开始,ia[10]超出范围,缓冲区溢出错误

练习3.31

#include<iostream>
using namespace std;

int main()
{
    
    
	int a[10];

	for (size_t i = 0; i != 10; ++i)
		a[i] = i;
}

练习3.32

#include<iostream>
#include<vector>
using namespace std;

int main()
{
    
    
	int a[10];

	for (size_t i = 0; i != 10; ++i)
		a[i] = i;

	int b[10];
	auto ret = copy(begin(a), end(a), begin(b));
	
	vector<int> c;
	for (size_t i = 0; i != 10; ++i)
		c.push_back(a[i]);
}

练习3.33
缓冲区溢出

猜你喜欢

转载自blog.csdn.net/Xgggcalled/article/details/109134316