我在维基百科上看到了C99可变长度数组的这个例子:
float read_and_process(int n)
{
float vals[n];
for (int i = 0; i < n; i++)
vals[i] = read_val();
return process(vals, n);
}
Run Code Online (Sandbox Code Playgroud)
这是不正确的?我的印象是变长数组仍然只是指针,这意味着上面的代码将过期的指针val传递给process(...)函数.
我从Accelerated C++中看到了这个例子
vector<string> func(const string&); //function declaration
vector<string> v;
string line = "abc";
v = func(line); //on entry, initialization of func's single parameter from line
//on exit, both initialization of the return value and then assignment to v
Run Code Online (Sandbox Code Playgroud)
我的问题是,因为func将const字符串引用作为参数,为什么在输入func时调用了复制构造函数?由于行是通过引用传递的,因此func只是在其本地堆栈上保留对行的引用?