How can I get the address of the buffer allocated by vector::reserve()?

两盒软妹~` 提交于 2020-01-03 18:32:30

问题


I have a std::vector of values for which I know the maximum size, but the actual size will vary during usage:

void setupBuffer(const size_t maxSize) {
  myVector.reserve(maxSize);
}

void addToBuffer(const Value& v) {
  myVector.push_back(v);

  if (myVector.size() == maxSize) {
    // process data...
    myVector.clear();
  }
}

However, in setupBuffer, I need to obtain a pointer to the start of myVector's data. I'm using a third party library where I must cache this pointer up front for use in a call made during the "process data..." section.

void setupBuffer(const size_t maxSize) {
  myVector.reserve(maxSize);

  cachePtr(&(myVector[0])); // doesn't work, obviously
}

I don't want to resize() the vector up front, as I want to use vector.size() to mean the number of elements added to the vector.

So, is there any way to obtain the pointer to the vector's buffer after allocation (reserve()) but before it has any elements? I would imagine the buffer exists (and won't move as long as I restrict the number of push_back'd values)....maybe this isn't guaranteed?


回答1:


The vector buffer will not be moved after a call to reserve unless you exceed the reserved capacity. Your problem is getting the pointer to the first element. The obvious answer is to push a single fake entry into the vector, get the pointer to it, and then remove it.

A nicer approach would be if the library accepted a functor rather than a pointer, which it would call when it needed to access the buffer - you could make the functor put off getting the address until the buffer had some real contents. However, I realise you don't have the luxury of rewriting the library.




回答2:


No. You are not allowed to access any element in the fector with an index greater than size. Resize is your only option here.

What you can do is something like:

myvec.resize(SOME_LARGE_VALUE);
myLibrary(&myVec[0]);
myvec.resize(GetSizeUsedByLibrary());

When you resize, elements in the vector are not destroyed, except those which had an index above the new size. Elements below the number set in resize are left alone. Example using std::basic_string, but equally applicable to vector




回答3:


Did you try front()? Also, you could push_back an element, get the address as you did, and then erase it.

All of this is not guaranteed, but you could read the source of your vector<> to see if it's fine for you if you have to do it. You could also roll your own vector pretty easily that has a pointer to the data and never moves it.




回答4:


There are lot of hacks around it. But I recommend you valid way - override allocator, the second template argument:

typedef std::vector<MyItem, MyAllocator> myvector_t;

After it in MyAllocator provide fixed buffer allocation, and pass this allocator instance as arguemnt of vector's constructor



来源:https://stackoverflow.com/questions/3352838/how-can-i-get-the-address-of-the-buffer-allocated-by-vectorreserve

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!