Erasing vector.end() fails


Why does using vector.erase(vector.end()) produces a

Segmentation fault (core dumped)

when using this code:

#include <iostream> #include <vector> using namespace std; void printMe(vector<int>& v){ for(auto &i:v) cout<<i<<" "; cout<<"\n"; } int main() { vector<int> c = { 1,2,3,4,5,6,7,8}; printMe(c); c.erase(c.begin()); printMe(c); c.erase(c.begin()); printMe(c); // c.erase(c.end()); //will produce segmentation fault // printMe(c); return 0; }

I'm a bit new to these iterators , so this caught me off guard. While I know there exists vector.pop_back(). I'm curious to know what exactly causes this.

A <a href="http://ideone.com/EGpVzn" rel="nofollow">link</a> to the program.


vector::end() does not point to the last element, it points to the element <em>just after</em> the last element.

Quoting <a href="http://www.cplusplus.com/reference/vector/vector/end/" rel="nofollow">cplusplus.com</a>,



Returns an iterator referring to the past-the-end element in the vector container.

The past-the-end element is the theoretical element that would follow the last element in the vector. It does not point to any element, and thus shall not be dereferenced.

<strong>Because the ranges used by functions of the standard library do not include the element pointed by their closing iterator, this function is often used in combination with vector::begin to specify a range including all the elements in the container.</strong>


Hence, it has nothing to erase() there, and hence the error.

<hr />




c.erase(c.end() - 1);


vector::end() points to one after the last element.

Therefore it does not point to an element.

Thus, there is nothing to delete.

If you wish to delete the last element, you need to delete the element before that, as you do in your ideone link.


Vector's end() returns an iterator referring to the element which should be AFTER the last element in container.

So you are trying to erase the memory which does not belong to you, this produces SIGSEGV (11).


As volerag mentioned above just replace c.erase(c.end()); with c.erase(c.end()-1);


  • Overriding operator*() of iterators
  • Adding and creating chunks of a list in python
  • C++ operating on “packed” arrays
  • Get the SQL query issued by a LINQ command in Linq to SQL
  • SSB dbgen Linux - Segmentation Fault
  • Is a NULL pointer's dereference also equals NULL?
  • Segmentation Fault 11 on upgrading to Swift 2.0
  • WebSphere MQ 6 message segmentation option ignored in put()?
  • What's the essential difference between these two variadic functions?
  • std::system Exception when instantiating a singleton object
  • c++ regex_replace not doing intended substitution
  • Hibernate to update table schema
  • Multiple producers single consumer locking schema
  • Access Android Market through SSH tunnel
  • Reduction and collapse clauses in OMP have some confusing points
  • Webgrid not refreshing after delete MVC
  • Jackson Parser: ignore deserializing for type mismatch
  • Can you perform a UNION without a subquery in SQLAlchemy?
  • Allowing both email and username for authentication
  • Jquery UI tool tip close icon
  • Change multiple background-images with jQuery
  • ImageMagick, replace semi-transparent white with opaque white
  • Get one-time binding to work for ng-if
  • output of program is not same as passed argument
  • Trying to switch camera back to front but getting exception
  • How do you troubleshoot character encoding problems?
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Akka Routing: Reply's send to router ends up as dead letters
  • Circular dependency while pushing http interceptor
  • Traverse Array and Display in markup
  • Linker errors when using intrinsic function via function pointer
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Qt: Run a script BEFORE make
  • unknown Exception android
  • Is there any way to bind data to data.frame by some index?
  • Checking variable from a different class in C#
  • Django query for large number of relationships
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?
  • How to push additional view controllers onto NavigationController but keep the TabBar?