Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::vector and c-style arrays

Tags:

c++

stl

opencl

I am experimenting with OpenCL to increase the speed of our software. We work with maps a lot and, to simplify, represent a map as a std::vector< std::vector >. The OpenCL API takes raw c-style pointers as arguments, for example int* in the case above.

My questions:

  • Are there implementation guarantees in the stl that vector is, internally, consecutive in memory?
  • Can I safely cast a std::vector to int* and expect that to work?
  • In the case of a vector of vectors, can I still assume this holds true? I would expect the vector to hold other state data, or alignment issues, or maybe something else...
  • What is the best way to approach this? Write a custom 2d data structure that holds an internal, contiguous-in-memory buffer and work with that? I'd have to copy to/from vectors a lot...

Thanks.

like image 933
Roel Avatar asked Sep 07 '09 10:09

Roel


People also ask

Is vector and array the same in C++?

Vectors in C++ are the dynamic arrays that are used to store data. Unlike arrays, which are used to store sequential data and are static in nature, Vectors provide more flexibility to the program.

What is the difference between std :: array and array?

std::array is just a class version of the classic C array. That means its size is fixed at compile time and it will be allocated as a single chunk (e.g. taking space on the stack). The advantage it has is slightly better performance because there is no indirection between the object and the arrayed data.

What is the difference between std :: array and std::vector?

Difference between std::vector and std::array in C++Vector is a sequential container to store elements and not index based. Array stores a fixed-size sequential collection of elements of the same type and it is index based. Vector is dynamic in nature so, size increases with insertion of elements.

Is STD array faster than vector C++?

There is a myth that for run-time speed, one should use arrays. A std::vector can never be faster than an array, as it has (a pointer to the first element of) an array as one of its data members. But the difference in run-time speed is slim and absent in any non-trivial program.


2 Answers

  • Are there implementation guarantees in the stl that vector is,
    internally, consecutive in memory

Yes, it is a dynamic array. Standard guarantees that the objects inside vector are stored consecutively.

  • Can I safely cast a std::vector to int* and expect that to work?

No, but you can use begin() and use that as the pointer.

  • Are there implementation guarantees in the stl that vector is,
    internally, consecutive in memory

No, since vector may contain some internal member variables the whole 2D array will not be continuos memory location

like image 23
Naveen Avatar answered Sep 30 '22 14:09

Naveen


Are there implementation guarantees in the stl that vector is, internally, consecutive in memory?

As of C++03, yes, a vector is guaranteed to use contiguous storage. (In C++98, there was an accidental loophole so an implementation could hypothetically use non-contiguous storage, but it was fixed in the 2003 revision of the standard - and no implementation actually used non-contiguous storage because it'd be a terrible idea)

Can I safely cast a std::vector to int* and expect that to work?

The usual way is &v[0]. (&*v.begin() would probably work too, but I seem to recall there's some fluffy wording in the standard that makes this not 100% reliable)

No. Why would you expect that to work? A vector is a class. It is not a pointer. It just contains a pointer.

In the case of a vector of vectors, can I still assume this holds true? I would expect the vector to hold other state data, or alignment issues, or maybe something else...

The vector behaves the same whatever you store in it. If you make a vector of vectors, you end up with an object which contains a pointer to a heap-allocated array, where each element is an object which contains a pointer to a heap-allocated array.

As for how you should approach this, it depends on a lot of factors. How big is your total dataset? You might want to have the entire table allocated contiguously. With a vector of vectors, each row is a separate allocation.

like image 50
jalf Avatar answered Sep 30 '22 14:09

jalf