Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint

Discussion

There are three major kinds of invalid objects:

  • Destroyed objects: Typical examples are automatic objects that have gone out of scope and deleted heap-based objects. After you call an object’s destructor, its lifetime is over and it is undefined and generally unsafe to do anything with it.

  • Semantically invalidated objects: Typical examples include dangling pointers to deleted objects (e.g., a pointer p after a delete p;) and invalidated iterators (e.g., a vector<T>::iterator i after an insertion at the beginning of the container the iterator refers into). Note that dangling pointers and invalidated iterators are conceptually identical, and the latter often directly contain the former. It is generally undefined and unsafe to do anything except assign another valid value to an invalidated object (e.g., p = new Object; or i = v.begin();).

  • Objects that were never valid: Examples include objects “obtained” by forging a pointer (using reinterpret_cast, see Item 92), or accessing past array boundaries.


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free 10-Day Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint