Re: delete the dynamically allocated memory twice causes error
BobR wrote:
[snip]
#include <iostream>
#include <vector>
int main(){
std::vector<int> pi( 1, 12 );
// Tong "Another example is that C++ also won't check whether
// an array index is out of bound."
// And what does '.at()' do?
but vector is not the built-in `array'.
std::cout << pi.at(0);
return 0;
}
Much 'easier'! How much 'efficiency' did that give up?
Yes, `at' lose efficiency, here is a typical implementation of vector<>::at:
const_reference at(size_type _Pos) const
{
if (size() <= _Pos) // waste an extra compare here
_Xran();
return (*(begin() + _Pos));
}
Now add some more:
int *pi = new int(12);
int *pi2 = new int(12);
// ....
int *pi299 = new int(12);
int *pi300 = new int(12);
// yeah, array is better, but you still need to 'delete[]'
vs.
std::vector<int> pi( 300, 12 );
Now where's your 'efficiency'?
Which would you choose?
When I talk "C++", I only mean the syntax, not including the extra libraries
such as STL, though it's a part of C++.
sorry for my poor english, but When I talk "efficiency", I only mean the
runtime efficiency.
'C++' was designed to be [1] a faster way to program ('C'). That's the
a faster way to program without losing efficiency
'efficiency'!
May be we consider `efficiency' in different aspects.
[1] among other considerations (type safety?). <G>
--
Bob R
POVrookie