problem with std::vector
-
in my header file i wrote this code:
class CMyDlg : public CDialog
{
public:
int varCount;
vector< intVec;// ...
}
and in the cpp file wrote this code :
void CQMDlg::OnGetvarCountButton()
{
// initialize varCountfor(int i=0;i!=varCount;i++) { intVec.push\_back(i); }
}
there is no error in compilation but when i press that specific button, program hangs. can somebody help me? thanks
How exactly have you initialized
varCount
? Your loop has a problem:for(int i=0;i!=varCount;i++)
while i is not equal to varCount, thats just asking for trouble. Try changing it tofor(int i=0;i<varCount;i++)
-
How exactly have you initialized
varCount
? Your loop has a problem:for(int i=0;i!=varCount;i++)
while i is not equal to varCount, thats just asking for trouble. Try changing it tofor(int i=0;i<varCount;i++)
-
thank you guys. yes, my problem was in varCount initialization. and i have another problem. when i write
intVec.
the property container menu of intVec doesn't appears.
Intellisense doesn't work ? That doesn't really matter, does it ?
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
thank you guys. yes, my problem was in varCount initialization. and i have another problem. when i write
intVec.
the property container menu of intVec doesn't appears.
Many times, deleting
.ncb
file in project folder and rebuilding application do work.:)Prasad Notifier using ATL | Operator new[],delete[][^]
-
How exactly have you initialized
varCount
? Your loop has a problem:for(int i=0;i!=varCount;i++)
while i is not equal to varCount, thats just asking for trouble. Try changing it tofor(int i=0;i<varCount;i++)
waldermort wrote:
while i is not equal to varCount, thats just asking for trouble
How?
Prasad Notifier using ATL | Operator new[],delete[][^]
-
Many times, deleting
.ncb
file in project folder and rebuilding application do work.:)Prasad Notifier using ATL | Operator new[],delete[][^]
-
waldermort wrote:
while i is not equal to varCount, thats just asking for trouble
How?
Prasad Notifier using ATL | Operator new[],delete[][^]
Because, it can easily happen, that you skip that specific value, for whatever reason. You could accidently increase the counter twice, initialize it with a value larger than varCount and stuff like that. It's just a matter of clean and readable code. If you want a loop to run as long as i is less or equal some other value, then you should write your condition that way. Lots of problems and timewasting during debugging happen due to such dirty practices. Then again, STL-Iterators work just that way ( it = YourVector.begin(); it != YourVector.end(); it++ ), but that don't mean it's good =).
-
Because, it can easily happen, that you skip that specific value, for whatever reason. You could accidently increase the counter twice, initialize it with a value larger than varCount and stuff like that. It's just a matter of clean and readable code. If you want a loop to run as long as i is less or equal some other value, then you should write your condition that way. Lots of problems and timewasting during debugging happen due to such dirty practices. Then again, STL-Iterators work just that way ( it = YourVector.begin(); it != YourVector.end(); it++ ), but that don't mean it's good =).
Actually, I dont follow coding style, by OP,too. All points mentioned by you are valid. But in given code , that doesn't matter. As revealed later, it was initializatin problem.:)
Prasad Notifier using ATL | Operator new[],delete[][^]
-
Actually, I dont follow coding style, by OP,too. All points mentioned by you are valid. But in given code , that doesn't matter. As revealed later, it was initializatin problem.:)
Prasad Notifier using ATL | Operator new[],delete[][^]
prasad_som wrote:
But in given code , that doesn't matter. As revealed later, it was initializatin problem.
Actually, he probably would have caught it sooner because I'm guessing it was something like this:
void CQMDlg::OnGetvarCountButton() { varCount = -1; // just as an example for(int i=0; i!=varCount; ++i) { intVec.push_back(i); } }
Which will run forever, whereas if the code was written as:
void CQMDlg::OnGetvarCountButton() { varCount = -1; // just as an example for(int i=0; i<varCount; ++i) { intVec.push_back(i); } }
It would have been caught immediately since the vector would have been empty. As a side note, STL's iterators use != for a reason. For most of the containers, there is no guarantee that the memory locations are sequential, so you can't do a < comparison to find the end of the loop. -- modified at 10:18 Thursday 21st September, 2006
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
-
prasad_som wrote:
But in given code , that doesn't matter. As revealed later, it was initializatin problem.
Actually, he probably would have caught it sooner because I'm guessing it was something like this:
void CQMDlg::OnGetvarCountButton() { varCount = -1; // just as an example for(int i=0; i!=varCount; ++i) { intVec.push_back(i); } }
Which will run forever, whereas if the code was written as:
void CQMDlg::OnGetvarCountButton() { varCount = -1; // just as an example for(int i=0; i<varCount; ++i) { intVec.push_back(i); } }
It would have been caught immediately since the vector would have been empty. As a side note, STL's iterators use != for a reason. For most of the containers, there is no guarantee that the memory locations are sequential, so you can't do a < comparison to find the end of the loop. -- modified at 10:18 Thursday 21st September, 2006
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
Zac Howland wrote:
for(int i=0; i!=varCount; ++i
True, I never come across such code,too.
Prasad Notifier using ATL | Operator new[],delete[][^]