MSVC 14 STL vector reserve

l00k

I have noticed strange behavior (IMO) on MSVC 14 Comm. in Debug x86 solution. Code below throws Exception when vector::resize is not included. Note: after assigment some entities may be unassigned by passing nullptr.

vector<Entity*> m_entities;

(...)

// find empty slot
u_int id = m_entities.size();
for(u_int i=0; i<m_entities.size(); ++i)
{
    if(m_entities[i] == nullptr)
    {
        id = i;
        break;
    }
}

// vector realloc
if(id == m_entities.capacity())
{
    u_int newSize = m_entities.capacity() * 2;
    m_entities.reserve(newSize);
    //m_entities.resize(newSize);
}

// assign
entity->m_id = id;
m_entities[id] = entity;

exception debug

It looks like operator[] checks size() instead of capacity() - am I right?

Humam Helfawi

You can NOT access the reserved area of the vector if it was not initialized. reserve does not initialize anything it just reserve (as it was named) some memory to not reallocate the vector each time a new item is pushed back

Try to run this code:

#include <iostream>
#include <string>
#include <vector>
class my_class{
    public:
    my_class(){
        x="I am an initialized item";
    }
    std::string x;
};
int main()
{
    std::vector<my_class> v(2);
    v.reserve(3);
    std::cout << v[0].x <<std::endl<< v[1].x <<std::endl <<v[2].x;
}

You may got compiling error in the debug mode(depending on your compiler), it may pass and give undefined behaviour (NOT sure about undefined behaviour please someone edit this part). In best case, it would run with printing empty string for v[2].

Live Demo

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

STL vector: resize() and assign()

From Dev

Does a vector assignment invalidate the `reserve`?

From Dev

Vector of vectors, reserve

From Dev

stl - Is a string a vector?

From Dev

Is assigning to element of vector that has not been resized but reserve was called legal?

From Dev

Thread safety std::vector push_back and reserve

From Dev

STL vector element removal efficiency

From Dev

STL Algorithms to generate and copy a vector

From Dev

Is std::vector::reserve(0); legal?

From Dev

C++ vector<vector<int> > reserve size at beginning

From Dev

STL algorithm for Vector Add

From Dev

How to preallocate(reserve) a priority_queue<vector>?

From Dev

Undefined behavior for std::vector reserve()

From Dev

STL vector operator data

From Dev

How do I reserve memory for a std::vector at construction time?

From Dev

What is better: reserve vector capacity, preallocate to size or push back in loop?

From Dev

reserve() Implementation for std::vector in STL

From Dev

Does std::vector::insert reserve by definition?

From Dev

vector push_back in STL?

From Dev

Reserve a vector of reference_wrapper objects, how it is possible?

From Dev

array allocation followed by memcpy or vector with reserve?

From Dev

bad_alloc on vector.push() and vector.reserve()

From Dev

How does std::vector::reserve actually work?

From Dev

Assertion Error, using STL Vector

From Dev

Overloading operators for STL Vector

From Dev

STL vector element removal efficiency

From Dev

bad_alloc on vector.push() and vector.reserve()

From Dev

How reserve in std::vector works + Accessing vector with []

From Dev

C++ STL : Vector syntax