C++将指针传递给向量元素而不是数组指针

发布于 2024-12-11 02:10:52 字数 2021 浏览 0 评论 0原文

我认为下面的代码片段是完全合法的(无论如何它都是在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

凉城 2024-12-18 02:10:52

如果您使用 std::vector::push_back(T &) 添加元素,std::vector 会展开,std:: vector::insert(iterator, T &) (感谢 K-ballo)或显式调用std::vector::resize(size_t)。否则,它不会扩展。

std::vector<int> vec_ints;
vec_ints.reserve(27);
std::vector<double> vec_doub;
vec_doub.reserve(27);
for(int i = 0; i < n; ++i) {
    vec_ints.push_back(...);
    vec_doub.push_back(...);    
}
const int* int_ptr = &vec_ints[0];
const double* doub_ptr = &vec_doub[0];
func(...,...,int_ptr,doub_ptr);

你想要那样的东西

std::vector<T> expands if you are adding elements using std::vector<T>::push_back(T &), std::vector<T>::insert(iterator, T &) (thanks K-ballo) or explicitly calling std::vector<T>::resize(size_t). Otherwise, it doesn't expand.

std::vector<int> vec_ints;
vec_ints.reserve(27);
std::vector<double> vec_doub;
vec_doub.reserve(27);
for(int i = 0; i < n; ++i) {
    vec_ints.push_back(...);
    vec_doub.push_back(...);    
}
const int* int_ptr = &vec_ints[0];
const double* doub_ptr = &vec_doub[0];
func(...,...,int_ptr,doub_ptr);

You want something like that

只怪假的太真实 2024-12-18 02:10:52

不,矢量不会自动扩展。您需要自己扩展

if (n > 27)
    vec_ints.resize(n, 0);

等等。

No, vector doesn't expand automatically. You need to expand it yourself:

if (n > 27)
    vec_ints.resize(n, 0);

etc.

許願樹丅啲祈禱 2024-12-18 02:10:52

为什么不一开始就创建具有正确大小的向量呢?就像这样:

std::vector<int> vec_ints(n,0);
std::vector<double> vec_doub(n, 0.);

Why not just create the vectors with the correct size to begin with? Like so:

std::vector<int> vec_ints(n,0);
std::vector<double> vec_doub(n, 0.);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文