C++将指针传递给向量元素而不是数组指针
我认为下面的代码片段是完全合法的(无论如何它都是在 MS Visual Studio 2008,C++ 上构建的)。
我用它来链接到第三方库。但我认为因为我传递的是指向向量元素的指针而不是第 3 方库函数期望的常规指针,所以我收到运行时错误
C 运行时库检测到无效参数
我在这里做错了什么?
std::vector<int> vec_ints(27,0);
std::vector<double> vec_doub(27, 0.);
for(int i = 0; i < n; ++i) {
//-Based on my understanding when i >=27, STL vectors automatically reallocate additional space (twice).
vec_ints[i] = ...;
vec_doub[i] = ...;
}
const int* int_ptr = &vec_ints[0];
const double* doub_ptr = &vec_doub[0];
//-Func is the 3rd party library function that expects a const int* and const double* in the last 2 arguments.
func(...,...,int_ptr,doub_ptr);
但是在 MS Visual Studio 2008 (Windows Vista) 上构建后运行此程序会导致如上所述的运行时错误,即,
C 运行时库检测到无效参数
尚未在 Linux 上对此进行测试,我确实希望避免为此将向量的内容复制到数组中。有什么想法吗?
进一步编辑以确认尼克和克里斯推荐的使用并继续与弗拉德等人讨论;这是一个代码片段:
#include <iostream>
#include <vector>
int main() {
for(int i=2; i<6; ++i) {
int isq = i*i;
std::vector<int> v;
v.reserve(4);
for(int j=0; j<isq; ++j) {
v.push_back(j);
}
std::cout << "Vector v: size = " << v.size() << " capacity = " << v.capacity()
<< "\n";
std::cout << "Elements: \n";
for(int k=0; k<v.size(); ++k) {
std::cout << v.at(k) << " ";
}
std::cout << "\n\n";
}
return 0;
}
给出输出:
Vector v: size = 4 capacity = 4
Elements:
0 1 2 3
Vector v: size = 9 capacity = 16
Elements:
0 1 2 3 4 5 6 7 8
Vector v: size = 16 capacity = 16
Elements:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Vector v: size = 25 capacity = 32
Elements:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
22 23 24
因此,至少对于此上下文中的使用,如果没有使用显式调整大小,它似乎可以按预期/预期工作。
I think the following code snippet is perfectly legal (it builds anyway on MS Visual Studio 2008, C++).
I use it to link to a 3rd party library. But I think because I am passing a pointer to a vector element instead of a regular pointer that the 3rd party library function expects, I get a run-time error
Invalid parameter detected by C-runtime library
What am I doing wrong here?
std::vector<int> vec_ints(27,0);
std::vector<double> vec_doub(27, 0.);
for(int i = 0; i < n; ++i) {
//-Based on my understanding when i >=27, STL vectors automatically reallocate additional space (twice).
vec_ints[i] = ...;
vec_doub[i] = ...;
}
const int* int_ptr = &vec_ints[0];
const double* doub_ptr = &vec_doub[0];
//-Func is the 3rd party library function that expects a const int* and const double* in the last 2 arguments.
func(...,...,int_ptr,doub_ptr);
But running this after building on MS Visual Studio 2008 (Windows Vista), leads to run-time error as mentioned above, viz.,
Invalid parameter detected by C runtime library
Haven't tested this on Linux yet and I sure would like to avoid copying the contents of the vector into an array just for this. Any ideas what is going on?
Further edit to confirm usage of Nick and Chris' recommendation and to continue discussion with Vlad et al; here's a code snippet:
#include <iostream>
#include <vector>
int main() {
for(int i=2; i<6; ++i) {
int isq = i*i;
std::vector<int> v;
v.reserve(4);
for(int j=0; j<isq; ++j) {
v.push_back(j);
}
std::cout << "Vector v: size = " << v.size() << " capacity = " << v.capacity()
<< "\n";
std::cout << "Elements: \n";
for(int k=0; k<v.size(); ++k) {
std::cout << v.at(k) << " ";
}
std::cout << "\n\n";
}
return 0;
}
Gives output:
Vector v: size = 4 capacity = 4
Elements:
0 1 2 3
Vector v: size = 9 capacity = 16
Elements:
0 1 2 3 4 5 6 7 8
Vector v: size = 16 capacity = 16
Elements:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Vector v: size = 25 capacity = 32
Elements:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
22 23 24
So atleast for the usage in this context, where no explicit resize is used, it seems to work as intended/expected.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您使用
std::vector::push_back(T &)
添加元素,std::vector
会展开,std:: vector::insert(iterator, T &)
(感谢 K-ballo)或显式调用std::vector::resize(size_t)
。否则,它不会扩展。你想要那样的东西
std::vector<T>
expands if you are adding elements usingstd::vector<T>::push_back(T &)
,std::vector<T>::insert(iterator, T &)
(thanks K-ballo) or explicitly callingstd::vector<T>::resize(size_t)
. Otherwise, it doesn't expand.You want something like that
不,矢量不会自动扩展。您需要自己扩展:
等等。
No, vector doesn't expand automatically. You need to expand it yourself:
etc.
为什么不一开始就创建具有正确大小的向量呢?就像这样:
Why not just create the vectors with the correct size to begin with? Like so: