470,626 Members | 2,172 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,626 developers. It's quick & easy.

vector::pop_back issue

Dear All,

at the time class destruction, application error occurs. Seems the
heap was corrupted with the pop_back operation on empty vector.

the code failed to execute in Visual C++ 6 and 2008. but worked fine
in DevCPP. I admit that the pop function should check for empty
vector. but still the different behavior concludes, it's a bug in
Microsoft STL Implementation
template<class T>
class TestStack
{
public:
TestStack<T>() { _stack.reserve(1024); }
~TestStack<T>() { _stack.clear(); }
T pop()
{
T Obj = _stack.back();
_stack.pop_back();
return Obj;
}
void push( const T& element )
{ _stack.push_back( element ); }
bool clear( void ) { _stack.clear(); }
private:
deque<T_stack;
};

int main( void )
{
TestStack<intStackObj;
StackObj.pop();
StackObj.push( 10 );
StackObj.push( 20 );
StackObj.push( 30 );
StackObj.push( 40 );
return 0;
}
Jul 17 '08 #1
3 2541
On Thu, 17 Jul 2008 01:27:08 -0700, Sarath wrote:
Dear All,

at the time class destruction, application error occurs. Seems the heap
was corrupted with the pop_back operation on empty vector.
How surprising.
the code failed to execute in Visual C++ 6 and 2008. but worked fine in
DevCPP. I admit that the pop function should check for empty vector. but
still the different behavior concludes, it's a bug in Microsoft STL
Implementation
Errrm... right... and what do you think *should* happen if you pop an
empty deque?

--
Lionel B
Jul 17 '08 #2
Sarath <CS*****@gmail.comwrote:
at the time class destruction, application error occurs. Seems the
heap was corrupted with the pop_back operation on empty vector.

the code failed to execute in Visual C++ 6 and 2008. but worked fine
in DevCPP. I admit that the pop function should check for empty
vector. but still the different behavior concludes, it's a bug in
Microsoft STL Implementation
As far as I know, the result of calling pop_back() on an empty container
is undefined. As such, no implementation could possibly be broken when
you do such a thing because the implementation can do whatever it wants.
template<class T>
class TestStack
Why not use std::stack?
{
public:
TestStack<T>() { _stack.reserve(1024); }
~TestStack<T>() { _stack.clear(); }
T pop()
{
T Obj = _stack.back();
_stack.pop_back();
return Obj;
}
What are you going to do in the above if T's copy constructor throws
when Obj is returned? You don't get the popped object and it is no
longer in the container.

Better would be to maintain command-query separation. Use back() to
access the last element and pop_back when it is no longer needed.
void push( const T& element )
{ _stack.push_back( element ); }
bool clear( void ) { _stack.clear(); }
private:
deque<T_stack;
};

int main( void )
{
TestStack<intStackObj;
StackObj.pop();
StackObj.push( 10 );
StackObj.push( 20 );
StackObj.push( 30 );
StackObj.push( 40 );
return 0;
}
Jul 18 '08 #3
On Jul 18, 6:43*am, "Daniel T." <danie...@earthlink.netwrote:
Sarath <CSar...@gmail.comwrote:
at the time class destruction, application error occurs. Seems the
heap was corrupted with the pop_back operation on empty vector.
the code failed to execute in Visual C++ 6 and 2008. *but worked fine
in DevCPP. I admit that the pop function should check for empty
vector. but still the different behavior concludes, it's a bug in
Microsoft STL Implementation

As far as I know, the result of calling pop_back() on an empty container
is undefined. As such, no implementation could possibly be broken when
you do such a thing because the implementation can do whatever it wants.
template<class T>
class TestStack

Why not use std::stack?
{
public:
* * TestStack<T>() { _stack.reserve(1024); }
* * ~TestStack<T>() { _stack.clear(); }
* * T pop()
* *{
* * * * * *T Obj = _stack.back();
* * * * * *_stack.pop_back();
* * * * * *return Obj;
* *}

What are you going to do in the above if T's copy constructor throws
when Obj is returned? You don't get the popped object and it is no
longer in the container.

Better would be to maintain command-query separation. Use back() to
access the last element and pop_back when it is no longer needed.
* * void push( const T& element )
* * { _stack.push_back( element ); }
* * bool clear( void ) { _stack.clear(); }
private:
* * deque<T_stack;
};
int main( void )
{
* * TestStack<intStackObj;
* * StackObj.pop();
* * StackObj.push( 10 );
* * StackObj.push( 20 );
* * StackObj.push( 30 );
* * StackObj.push( 40 );
* * return 0;
}

thanks Daniel.
Jul 18 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by James Brown | last post: by
2 posts views Thread by laniik | last post: by
5 posts views Thread by laniik | last post: by
10 posts views Thread by Bob | last post: by
3 posts views Thread by Jeff | last post: by
reply views Thread by James Kanze | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.