Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. unexplained change of int value, when std::cout is added or commented out
Forum Updated to NodeBB v4.3 + New Features

unexplained change of int value, when std::cout is added or commented out

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 4 Posters 1.4k 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.
  • M Offline
    M Offline
    micha_eleric
    wrote on last edited by micha_eleric
    #1
        void CGUI::SetNewPoint(int a, int b, int c, int x, int y, int z)
        {
            std::cout << "============== CGUI::SetNewPoint =========== \n" << std::flush;
            if(m_iNewPoint == m_iMaxPoint)
            {
                const int temp = m_iMaxPoint;
                //std::cout << "temp[0] = " << temp << '\n' << std::flush;
                //std::cout << "m_iMaxPoint = " << m_iMaxPoint << '\n' << std::flush;
                m_iMaxPoint += m_iGrowArray;
    
                int tempArray[temp];
                EmptyNewArray(tempArray);
                //-----------m_iPointA
                int i = 0;
                //std::cout << "i[1] = " << i << '\n' << std::flush;
                //std::cout << "temp[1] = " << temp << '\n' << std::flush;
                while(i < temp)
                {
                    tempArray[i] = m_iPointA[i];
                    i++;
                }
                delete [] m_iPointA;
                m_iPointA = new int[m_iMaxPoint];
    
                i = 0;
                //std::cout << "i[2] = " << i << '\n' << std::flush;
                //std::cout << "temp[2] = " << temp << '\n' << std::flush;
                while(i < temp)
                {
                    m_iPointA[i] = tempArray[i];
                    i++;
                }
    
                //-----------m_iPointB
                EmptyNewArray(tempArray);
                std::cout << "SetNewPoint m_iPointB \n" << std::flush;
                i = 0;
                std::cout << "i[3] = " << i << '\n' << std::flush;
                std::cout << "temp[3] = " << temp << '\n' << std::flush;
                while(i < temp)
                {
                    std::cout << "tempArray before " << std::flush;
                    std::cout << "i[4] = " << i << '\n' << std::flush;
                    //std::cout << tempArray[i] << std::flush;
                    std::cout << "m_iPointB " << std::flush;
                    //std::cout << m_iPointB[i] << std::flush;
                    //tempArray[i] = m_iPointB[i];
                    //std::cout << ", tempArray after " << tempArray[i] << '\n' << std::flush;
                    i++;
                }
                std::cout << "i[5] = " << i << '\n' << std::flush;
                delete [] m_iPointB;
                m_iPointB = new int[m_iMaxPoint];
    
    

    program crashed when it m_iPointB while loop, so i added cout statements, crashed, so broke cout statements up, and commented out parts.
    m_iPointB while loop crashes if printing i value. even had crash before m_iPointA while loop, if std::cout << "i[5] was not commented out. only change to code is cout statements. it does not make sense that cout a value will cause a crash before code gets to cout statement, not int value will change with printing value.
    int temp is set once, and never changed, but value in cout does change. how? it is not in my code, but changes, some times.
    if std::cout << "i[5] was not commented out, then int temp changes to 0, if if std::cout << "i[5] was commented out, then int temp remains at 100, and did not not crash m_iPointA while loop.
    uncomment std::cout << tempArray[i] << std::flush;, then int temp goes to 0

    anyone have a clue or guess?

    JonBJ 1 Reply Last reply
    0
    • M micha_eleric

      @ChrisW67 said in unexplained change of int value, when std::cout is added or commented out:

      @micha_eleric said in unexplained change of int value, when std::cout is added or commented out:

      anyone have a clue or guess?

      This question does not have anything to do with Qt.

      The most likely cause of issues (crashes and all sorts of data corruption) in this code is the manipulation of, and probable out-of-bounds access in, bare C++ arrays on the heap. Rather than spend a large amount of time trying to find the culprit, my advice is that you should abandon the use of bare arrays and use either std::vector or QVector (both handle resizing and space reservation).

      int temp is randomly changing to 0. 0 size anything will crash

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #6

      @micha_eleric
      Everything @ChrisW67 has said. Most likely is

      const int temp = m_iMaxPoint;
      int tempArray[temp];
      

      These are both stack variables. If, for whatever reason, you write into tempArray beyond its bounds you will be writing into temp.... And if you write further beyond bounds you will bump into the actual call stack and that will "crash" your program....

      By now you could/should have run under debugger, put temp into Watch window, single step over each statement, look to see when temp gets changed.

      int tempArray[temp];
      EmptyNewArray(tempArray);
      

      Show the code of EmptyArray(int *array). Since it does not know what the size of the plain array passed to it is [Hint: if it thinks the passed array is size m_iMaxPoint it is not!], I would bet $5 it writes beyond its end bound, and hence corrupts temp variable. Am I right...?

      M 1 Reply Last reply
      2
      • C Offline
        C Offline
        ChrisW67
        wrote on last edited by ChrisW67
        #2

        @micha_eleric said in unexplained change of int value, when std::cout is added or commented out:

        anyone have a clue or guess?

        This question does not have anything to do with Qt.

        The most likely cause of issues (crashes and all sorts of data corruption) in this code is the manipulation of, and probable out-of-bounds access in, bare C++ arrays on the heap. Rather than spend a large amount of time trying to find the culprit, my advice is that you should abandon the use of bare arrays and use either std::vector or QVector (both handle resizing and space reservation).

        M 1 Reply Last reply
        1
        • M micha_eleric
              void CGUI::SetNewPoint(int a, int b, int c, int x, int y, int z)
              {
                  std::cout << "============== CGUI::SetNewPoint =========== \n" << std::flush;
                  if(m_iNewPoint == m_iMaxPoint)
                  {
                      const int temp = m_iMaxPoint;
                      //std::cout << "temp[0] = " << temp << '\n' << std::flush;
                      //std::cout << "m_iMaxPoint = " << m_iMaxPoint << '\n' << std::flush;
                      m_iMaxPoint += m_iGrowArray;
          
                      int tempArray[temp];
                      EmptyNewArray(tempArray);
                      //-----------m_iPointA
                      int i = 0;
                      //std::cout << "i[1] = " << i << '\n' << std::flush;
                      //std::cout << "temp[1] = " << temp << '\n' << std::flush;
                      while(i < temp)
                      {
                          tempArray[i] = m_iPointA[i];
                          i++;
                      }
                      delete [] m_iPointA;
                      m_iPointA = new int[m_iMaxPoint];
          
                      i = 0;
                      //std::cout << "i[2] = " << i << '\n' << std::flush;
                      //std::cout << "temp[2] = " << temp << '\n' << std::flush;
                      while(i < temp)
                      {
                          m_iPointA[i] = tempArray[i];
                          i++;
                      }
          
                      //-----------m_iPointB
                      EmptyNewArray(tempArray);
                      std::cout << "SetNewPoint m_iPointB \n" << std::flush;
                      i = 0;
                      std::cout << "i[3] = " << i << '\n' << std::flush;
                      std::cout << "temp[3] = " << temp << '\n' << std::flush;
                      while(i < temp)
                      {
                          std::cout << "tempArray before " << std::flush;
                          std::cout << "i[4] = " << i << '\n' << std::flush;
                          //std::cout << tempArray[i] << std::flush;
                          std::cout << "m_iPointB " << std::flush;
                          //std::cout << m_iPointB[i] << std::flush;
                          //tempArray[i] = m_iPointB[i];
                          //std::cout << ", tempArray after " << tempArray[i] << '\n' << std::flush;
                          i++;
                      }
                      std::cout << "i[5] = " << i << '\n' << std::flush;
                      delete [] m_iPointB;
                      m_iPointB = new int[m_iMaxPoint];
          
          

          program crashed when it m_iPointB while loop, so i added cout statements, crashed, so broke cout statements up, and commented out parts.
          m_iPointB while loop crashes if printing i value. even had crash before m_iPointA while loop, if std::cout << "i[5] was not commented out. only change to code is cout statements. it does not make sense that cout a value will cause a crash before code gets to cout statement, not int value will change with printing value.
          int temp is set once, and never changed, but value in cout does change. how? it is not in my code, but changes, some times.
          if std::cout << "i[5] was not commented out, then int temp changes to 0, if if std::cout << "i[5] was commented out, then int temp remains at 100, and did not not crash m_iPointA while loop.
          uncomment std::cout << tempArray[i] << std::flush;, then int temp goes to 0

          anyone have a clue or guess?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #3

          @micha_eleric said in unexplained change of int value, when std::cout is added or commented out:

          anyone have a clue or guess?

          In addition to all the points made by @ChrisW67, use a debugger to help see what is going on with "crashes".

          1 Reply Last reply
          1
          • C ChrisW67

            @micha_eleric said in unexplained change of int value, when std::cout is added or commented out:

            anyone have a clue or guess?

            This question does not have anything to do with Qt.

            The most likely cause of issues (crashes and all sorts of data corruption) in this code is the manipulation of, and probable out-of-bounds access in, bare C++ arrays on the heap. Rather than spend a large amount of time trying to find the culprit, my advice is that you should abandon the use of bare arrays and use either std::vector or QVector (both handle resizing and space reservation).

            M Offline
            M Offline
            micha_eleric
            wrote on last edited by
            #4

            @ChrisW67 said in unexplained change of int value, when std::cout is added or commented out:

            @micha_eleric said in unexplained change of int value, when std::cout is added or commented out:

            anyone have a clue or guess?

            This question does not have anything to do with Qt.

            The most likely cause of issues (crashes and all sorts of data corruption) in this code is the manipulation of, and probable out-of-bounds access in, bare C++ arrays on the heap. Rather than spend a large amount of time trying to find the culprit, my advice is that you should abandon the use of bare arrays and use either std::vector or QVector (both handle resizing and space reservation).

            int temp is randomly changing to 0. 0 size anything will crash

            JonBJ 1 Reply Last reply
            0
            • C Offline
              C Offline
              ChrisW67
              wrote on last edited by
              #5

              That does not change the advice.

              Something that is not a deliberate C++ access is changing the memory that temp occupies. It cannot be a C++ statement because temp is const and attempting to modify that value with an assignment will be stopped at compile time. That leaves unintended, un-coded memory changes. Things like out-of-bounds accesses on arrays or accesses through stale pointers can stomp on memory anywhere, and moving things around in memory, e.g. by adding code, can make the breakages unpredictable.

              1 Reply Last reply
              2
              • M micha_eleric

                @ChrisW67 said in unexplained change of int value, when std::cout is added or commented out:

                @micha_eleric said in unexplained change of int value, when std::cout is added or commented out:

                anyone have a clue or guess?

                This question does not have anything to do with Qt.

                The most likely cause of issues (crashes and all sorts of data corruption) in this code is the manipulation of, and probable out-of-bounds access in, bare C++ arrays on the heap. Rather than spend a large amount of time trying to find the culprit, my advice is that you should abandon the use of bare arrays and use either std::vector or QVector (both handle resizing and space reservation).

                int temp is randomly changing to 0. 0 size anything will crash

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #6

                @micha_eleric
                Everything @ChrisW67 has said. Most likely is

                const int temp = m_iMaxPoint;
                int tempArray[temp];
                

                These are both stack variables. If, for whatever reason, you write into tempArray beyond its bounds you will be writing into temp.... And if you write further beyond bounds you will bump into the actual call stack and that will "crash" your program....

                By now you could/should have run under debugger, put temp into Watch window, single step over each statement, look to see when temp gets changed.

                int tempArray[temp];
                EmptyNewArray(tempArray);
                

                Show the code of EmptyArray(int *array). Since it does not know what the size of the plain array passed to it is [Hint: if it thinks the passed array is size m_iMaxPoint it is not!], I would bet $5 it writes beyond its end bound, and hence corrupts temp variable. Am I right...?

                M 1 Reply Last reply
                2
                • JonBJ JonB

                  @micha_eleric
                  Everything @ChrisW67 has said. Most likely is

                  const int temp = m_iMaxPoint;
                  int tempArray[temp];
                  

                  These are both stack variables. If, for whatever reason, you write into tempArray beyond its bounds you will be writing into temp.... And if you write further beyond bounds you will bump into the actual call stack and that will "crash" your program....

                  By now you could/should have run under debugger, put temp into Watch window, single step over each statement, look to see when temp gets changed.

                  int tempArray[temp];
                  EmptyNewArray(tempArray);
                  

                  Show the code of EmptyArray(int *array). Since it does not know what the size of the plain array passed to it is [Hint: if it thinks the passed array is size m_iMaxPoint it is not!], I would bet $5 it writes beyond its end bound, and hence corrupts temp variable. Am I right...?

                  M Offline
                  M Offline
                  micha_eleric
                  wrote on last edited by
                  #7

                  @JonB

                  By now you could/should have run under debugger, put temp into Watch window, single step over each statement, look to see when temp gets changed.

                  Well, i am a wee bit new to qtcreater, and never used debugger in qtcreater.
                  where is the Watch window to put temp in?

                  JonBJ 1 Reply Last reply
                  0
                  • M micha_eleric

                    @JonB

                    By now you could/should have run under debugger, put temp into Watch window, single step over each statement, look to see when temp gets changed.

                    Well, i am a wee bit new to qtcreater, and never used debugger in qtcreater.
                    where is the Watch window to put temp in?

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #8

                    @micha_eleric
                    Have you tried running the debugger? If you look around when you are doing so you will see the Watch window/pane.

                    S 1 Reply Last reply
                    0
                    • JonBJ JonB

                      @micha_eleric
                      Have you tried running the debugger? If you look around when you are doing so you will see the Watch window/pane.

                      S Offline
                      S Offline
                      SimonSchroeder
                      wrote on last edited by
                      #9

                      @JonB said in unexplained change of int value, when std::cout is added or commented out:

                      @micha_eleric
                      Have you tried running the debugger? If you look around when you are doing so you will see the Watch window/pane.

                      Well, I already had a new Qt Creator installation where this didn't show up by default. I believe that if it is not there you turn it on in the 'Windows' menu while you are in debugging mode.

                      JonBJ 1 Reply Last reply
                      0
                      • S SimonSchroeder

                        @JonB said in unexplained change of int value, when std::cout is added or commented out:

                        @micha_eleric
                        Have you tried running the debugger? If you look around when you are doing so you will see the Watch window/pane.

                        Well, I already had a new Qt Creator installation where this didn't show up by default. I believe that if it is not there you turn it on in the 'Windows' menu while you are in debugging mode.

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by JonB
                        #10

                        @SimonSchroeder
                        That is the sort of action I would expect to have to look for if I were a programmer. Yes, I might have to look through menu items. No, I do not recall whether the Watch does or does not appear by default, though I have a feeling it did for me. Qt Creator also has Help to look this sort of thing up.

                        M 1 Reply Last reply
                        1
                        • JonBJ JonB

                          @SimonSchroeder
                          That is the sort of action I would expect to have to look for if I were a programmer. Yes, I might have to look through menu items. No, I do not recall whether the Watch does or does not appear by default, though I have a feeling it did for me. Qt Creator also has Help to look this sort of thing up.

                          M Offline
                          M Offline
                          micha_eleric
                          wrote on last edited by
                          #11

                          @JonB i have always used print, to track what was happing

                          JonBJ 1 Reply Last reply
                          0
                          • M micha_eleric

                            @JonB i have always used print, to track what was happing

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by
                            #12

                            @micha_eleric
                            Not quite sure what you mean by "print" from C++. Do you mean you have used gdb? Anyway it's worth spending a day to learn how to use a UI debugger like Qt's. Watch window, local variables window, stack trace window, all very useful.

                            M 1 Reply Last reply
                            0
                            • JonBJ JonB

                              @micha_eleric
                              Not quite sure what you mean by "print" from C++. Do you mean you have used gdb? Anyway it's worth spending a day to learn how to use a UI debugger like Qt's. Watch window, local variables window, stack trace window, all very useful.

                              M Offline
                              M Offline
                              micha_eleric
                              wrote on last edited by
                              #13

                              @JonB std::cout and std::ofstream

                              1 Reply Last reply
                              0

                              • Login

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • Users
                              • Groups
                              • Search
                              • Get Qt Extensions
                              • Unsolved