Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. problem with std::vector

problem with std::vector

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++graphicsquestion
12 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E erfi

    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 varCount

     for(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

    P Offline
    P Offline
    prasad_som
    wrote on last edited by
    #2

    Have stepped in to the code ? what varCount is expected to initialize with? Probably its not initializing properly and going into loop.

    Prasad Notifier using ATL | Operator new[],delete[][^]

    1 Reply Last reply
    0
    • E erfi

      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 varCount

       for(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

      W Offline
      W Offline
      Waldermort
      wrote on last edited by
      #3

      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 to for(int i=0;i<varCount;i++)

      E P 2 Replies Last reply
      0
      • W Waldermort

        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 to for(int i=0;i<varCount;i++)

        E Offline
        E Offline
        erfi
        wrote on last edited by
        #4

        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.

        C P 2 Replies Last reply
        0
        • E erfi

          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.

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #5

          Intellisense doesn't work ? That doesn't really matter, does it ?

          Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

          1 Reply Last reply
          0
          • E erfi

            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.

            P Offline
            P Offline
            prasad_som
            wrote on last edited by
            #6

            Many times, deleting .ncb file in project folder and rebuilding application do work.:)

            Prasad Notifier using ATL | Operator new[],delete[][^]

            E 1 Reply Last reply
            0
            • W Waldermort

              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 to for(int i=0;i<varCount;i++)

              P Offline
              P Offline
              prasad_som
              wrote on last edited by
              #7

              waldermort wrote:

              while i is not equal to varCount, thats just asking for trouble

              How?

              Prasad Notifier using ATL | Operator new[],delete[][^]

              M 1 Reply Last reply
              0
              • P prasad_som

                Many times, deleting .ncb file in project folder and rebuilding application do work.:)

                Prasad Notifier using ATL | Operator new[],delete[][^]

                E Offline
                E Offline
                erfi
                wrote on last edited by
                #8

                thanks i deleted the .ncb file and now it's working.

                1 Reply Last reply
                0
                • P prasad_som

                  waldermort wrote:

                  while i is not equal to varCount, thats just asking for trouble

                  How?

                  Prasad Notifier using ATL | Operator new[],delete[][^]

                  M Offline
                  M Offline
                  Mr Brainley
                  wrote on last edited by
                  #9

                  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 =).

                  P 1 Reply Last reply
                  0
                  • M Mr Brainley

                    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 =).

                    P Offline
                    P Offline
                    prasad_som
                    wrote on last edited by
                    #10

                    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[][^]

                    Z 1 Reply Last reply
                    0
                    • P prasad_som

                      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[][^]

                      Z Offline
                      Z Offline
                      Zac Howland
                      wrote on last edited by
                      #11

                      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

                      P 1 Reply Last reply
                      0
                      • Z Zac Howland

                        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

                        P Offline
                        P Offline
                        prasad_som
                        wrote on last edited by
                        #12

                        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[][^]

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups