C++Primer Fifth Edition: Exercise 3.27 3.28 3.29 3.30 3.31 3.32 3.33

Exercise 2.37

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

Exercise 3.28
Each element of the sa and sa2 arrays is an empty string, the string class has a corresponding default constructor
ia each element value is 0, the built-in type defined outside the function has an initial value
ia2 each element has no value, and the corresponding address is returned

Exercise 3.29 The
allocated space is fixed, it is inconvenient to add elements, and the flexibility is poor
. You need to specify the size of the array when using it.

Exercise 3.30
Index starts from 0, ia[10] is out of range, buffer overflow error

Exercise 3.31

#include<iostream>
using namespace std;

int main()
{
    
    
	int a[10];

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

Exercise 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]);
}

Exercise 3.33
Buffer Overflow

Guess you like

Origin blog.csdn.net/Xgggcalled/article/details/109134316