+ Reply to Thread
Results 1 to 2 of 2

Thread: Why this program does not crash?

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    28

    Why this program does not crash?

    Following program does not crash even if I try to access element which is not inserted in the vector. Why?

    Code:
    void Fun1()
    {
    	std::vector<int> vInts ;
    
    	vInts.push_back(11) ;
    	vInts.push_back(22) ;
    	vInts.push_back(33) ;
    
    	int nValue = vInts[3] ;
    }
    Please help.

  2. #2
    You should consider using 'at' method rather than using [].

    Following should crash as you expected!

    Code:
    void Fun1()
    {
    	std::vector<int> vInts ;
    
    	vInts.push_back(11) ;
    	vInts.push_back(22) ;
    	vInts.push_back(33) ;
    
    	int nValue = vInts.at(3) ;
    }
    Hope this helps.

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts