Skip to content

push_back for preallocation, avoid possible out of bounds crash - #178

Open
seanpquinn wants to merge 1 commit into
attractivechaos:masterfrom
seanpquinn:kvec_test_pushback
Open

push_back for preallocation, avoid possible out of bounds crash#178
seanpquinn wants to merge 1 commit into
attractivechaos:masterfrom
seanpquinn:kvec_test_pushback

Conversation

@seanpquinn

Copy link
Copy Markdown

closes #151

Comment thread test/kvec_test.cc
std::vector<int> array;
array.reserve(N);
for (j = 0; j < N; ++j) array[j] = j;
for (j = 0; j < N; ++j) array.push_back(j);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that push_back here is useless, because array.reserve already reserves the space.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Potential crash: std::vector::resize and std::vector::reserve

2 participants