Getting "vector subscript out of range" error

H

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?
 
T

Thomas Tutone

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-lite/how-to-post.html#faq-5.8

Best regards,

Tom
 
D

diligent.snail

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
 
G

Guest

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<int> vec;
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).
 
H

Han

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 .
 

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
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top