By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,741 Members | 1,973 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,741 IT Pros & Developers. It's quick & easy.

Getting "vector subscript out of range" error

P: n/a
Han
when I exe my project in vs.net2005,I got the error following:
Debug Assertion Failed!
Program:........
File:c:\program files\microsoft visual studio 8\vc\include\vector
Line:756

Expression:vector subscript out of range.

I just push_back some int less than 30 to the vector. But in the DevC+
+5.0,everything is all right. Could anyone give me some hint what is
the wrong thing i made?

Nov 13 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Nov 13, 11:35 am, Han <Mingwei....@gmail.comwrote:
when I exe my project in vs.net2005,I got the error following:
Debug Assertion Failed!
Program:........
File:c:\program files\microsoft visual studio 8\vc\include\vector
Line:756

Expression:vector subscript out of range.

I just push_back some int less than 30 to the vector. But in the DevC+
+5.0,everything is all right. Could anyone give me some hint what is
the wrong thing i made?
FAQ 5.8 may help:

http://www.parashift.com/c++-faq-lit...t.html#faq-5.8

Best regards,

Tom

Nov 13 '07 #2

P: n/a
On Nov 13, 5:35 pm, Han <Mingwei....@gmail.comwrote:
when I exe my project in vs.net2005,I got the error following:
Debug Assertion Failed!
Program:........
File:c:\program files\microsoft visual studio 8\vc\include\vector
Line:756

Expression:vector subscript out of range.

I just push_back some int less than 30 to the vector. But in the DevC+
+5.0,everything is all right. Could anyone give me some hint what is
the wrong thing i made?
You will have to post a test case - minimal compilable code that
demonstrates the problem.

Regards

Nov 13 '07 #3

P: n/a
On 2007-11-13 17:35, Han wrote:
when I exe my project in vs.net2005,I got the error following:
Debug Assertion Failed!
Program:........
File:c:\program files\microsoft visual studio 8\vc\include\vector
Line:756

Expression:vector subscript out of range.

I just push_back some int less than 30 to the vector. But in the DevC+
+5.0,everything is all right. Could anyone give me some hint what is
the wrong thing i made?

It is not the push_back()s that it is complaining about, it is when you
later try to access the elements in the vector. "subscript out of range"
means that you are trying to access an element that does not exist, example:

std::vector<intvec;
vec.push_back(1);
vec.push_back(2);
vec.push_back(3);
vec.push_back(4);
std::cout << vec[4]; // There is no element at index 4

The reason that DevC++ does not complain is that it does not have as
advanced debug capabilities in its library implementation, instead it
lets the program continue running with undefined behaviour (which is the
enemy of all C++ programmers).

--
Erik Wikström
Nov 13 '07 #4

P: n/a
Han
On 11 14 , 12 49 , diligent.sn...@gmail.com wrote:
On Nov 13, 5:35 pm, Han <Mingwei....@gmail.comwrote:
when I exe my project in vs.net2005,I got the error following:
Debug Assertion Failed!
Program:........
File:c:\program files\microsoft visual studio 8\vc\include\vector
Line:756
Expression:vector subscript out of range.
I just push_back some int less than 30 to the vector. But in the DevC+
+5.0,everything is all right. Could anyone give me some hint what is
the wrong thing i made?

You will have to post a test case - minimal compilable code that
demonstrates the problem.

Regards
Thanks,I have already found the way to avoid this problem in MSDN.
This problem is caused by the vector operator[].Instead of [],we
should use the at() more .

Nov 14 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.