Discussion:
STL vector resize vs reserve
(too old to reply)
Roger
2006-11-06 15:02:10 UTC
Permalink
I thought the difference between vector reserve and resize was:

reserve - just allocates space but doesn't create any elements in the vector
i.e. push_backs are required
resize - creates the elements so push_backs are not required

If that is true how come the following works in both cases:

ElapsedTimer(START);
for (int i = 0; i < 1000000 ;++i)
{
stTest.nTest = i;
stTest.dTest = i * 3.14;
vTest[i] = stTest;
} // End of For ()
ElapsedTimer(END);
cout << vTest[200].dTest << endl;
vTest.clear();
cout << vTest[200].dTest << endl; // Also why isn't this number cleared

Are these bugs in BCB6?

Roger
Chris Uzdavinis (TeamB)
2006-11-06 15:16:43 UTC
Permalink
Post by Roger
reserve - just allocates space but doesn't create any elements in the vector
i.e. push_backs are required
resize - creates the elements so push_backs are not required
Correct.
You forget that undefined behavior can do anything, including giving
the appearance of 'working'. But appearances can be very deceiving.
Post by Roger
ElapsedTimer(START);
for (int i = 0; i < 1000000 ;++i)
{
stTest.nTest = i;
stTest.dTest = i * 3.14;
vTest[i] = stTest;
} // End of For ()
ElapsedTimer(END);
cout << vTest[200].dTest << endl;
vTest.clear();
cout << vTest[200].dTest << endl; // Also why isn't this number cleared
Are these bugs in BCB6?
No, there are bugs in your code. If you're only calling reserve() on
a default-constructed vector, then accessing any elements through
operator[] has undefined behavior. (But it's hard to tell since you
don't show the construction or setup (neither reserve() nor resize())
on your vector.

Also, try calling size() on the vector and you might notice a
difference.

Of course, if you do the resize() first and then call reserve() on the
same vector, it will have the elements in it.
--
Chris (TeamB);
Roger
2006-11-06 15:42:16 UTC
Permalink
Thanks Chris,

as a follow-on I guess I shouldn't be using "int" in the for loop either?
(assuming int still 16 bits)

also why doesn't vTest.clear() not set the value to 0? If it isn't supposed
to, what does .clear() do?
cout << vTest[200000].dTest << endl; // Output = 628319
vTest.clear();
cout << vTest[200000].dTest << endl; // Output = 628319 - why isn't
this number cleared?
Roger
Chris Uzdavinis (TeamB)
2006-11-06 16:36:04 UTC
Permalink
Post by Roger
Thanks Chris,
as a follow-on I guess I shouldn't be using "int" in the for loop either?
(assuming int still 16 bits)
also why doesn't vTest.clear() not set the value to 0? If it isn't supposed
to, what does .clear() do?
It will logically remove elements fron the vector and destroy them.
However, ints do not have destructors, and so the vector is probably
just saying "I have no elements" by changing the size but not really
doing anything to the bit patterns in the data. Since you're not
supposed to touch that memory, it shouldn't matter what bit patterns
happen to be there.

If you assume an int is 16 bits, then should we also assume you're
using Borland C++ 3.1? If so how do you get to use the STL? That
compiler is so old as to pre-date the idea of the STL and most of the
template functionality that it requires.

However, if ints are assumed to be 16 bits, then you won't be able to
store values larger than 65536 unsigned, or 32767 signed, meaning the
answer to your question is yes--you shouldn't be using an int in the
for loop, as it cannot hold the value that will cause the loop to
terminate.
--
Chris (TeamB);
Remy Lebeau (TeamB)
2006-11-06 21:49:31 UTC
Permalink
Post by Roger
I thought the difference between vector reserve and resize was
Your thoughts are correct.
Post by Roger
If that is true how come the following works in both cases
Because the '[]' operator does not validate that the specified index is less
than the current size(). It accesses the raw memory directly. It *works*
because you pre-allocated the memory that you are making the '[]' operator
access. Although the code does not crash, it is not valid since no actual
items have been created inside the memory yet.
Post by Roger
vTest.clear();
cout << vTest[200].dTest << endl; // Also why isn't this number cleared
Because it is not supposed to be. Again, you are accessing memory that is
outside the current size() of the vector. The contents of that memory is
undefined. By calling clear(), the vector destroys just the items that it
actually contains (which is 0 in this case), leaving the rest of the memory
untouched. You are pre-allocating memory, but not adding any items to the
vector, and then directly manipulating the undefined memory, but not
clearing the memory you wrote to.
Post by Roger
Are these bugs in BCB6?
No. They are bugs in your code.


Gambit

Loading...