what's the bahaviro of pop_back for an empty vector?

A

Andy

Hi,

I did not find documentation on this. I try the following naive
program,

#include <vector>
#include <iostream>

using namespace std;

int main()
{
vector<int> myvec;

cout <<"first pop_back \n";
myvec.pop_back();

cout <<"second pop_back \n";
myvec.pop_back();

return 0;
}

Comipled with gcc-4.0, it just print the strings, then stay for a while
and terminated.

Any insights here?

Thanks,

Andy
 
V

Victor Bazarov

Andy said:
I did not find documentation on this. I try the following naive
program,

#include <vector>
#include <iostream>

using namespace std;

int main()
{
vector<int> myvec;

cout <<"first pop_back \n";
myvec.pop_back();

cout <<"second pop_back \n";
myvec.pop_back();

return 0;
}

Comipled with gcc-4.0, it just print the strings, then stay for a while
and terminated.

Any insights here?

Undefined behaviour. std::vector::pop_back() is equivalent to calling
erase(--end()); which when the vector is empty causes the call to 'erase'
with an invalid iterator value.

V
 
A

Andre Kostur

Hi,

I did not find documentation on this. I try the following naive
program,

#include <vector>
#include <iostream>

using namespace std;

int main()
{
vector<int> myvec;

cout <<"first pop_back \n";
myvec.pop_back();

cout <<"second pop_back \n";
myvec.pop_back();

return 0;
}

Comipled with gcc-4.0, it just print the strings, then stay for a while
and terminated.

Any insights here?

Undefined behaviour. Anything can happen.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,667
Latest member
DaniloB294

Latest Threads

Top