unexplained change of int value, when std::cout is added or commented out
-
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 0anyone have a clue or guess?
-
@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
@micha_eleric
Everything @ChrisW67 has said. Most likely isconst 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 intotemp
.... 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 whentemp
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 sizem_iMaxPoint
it is not!], I would bet $5 it writes beyond its end bound, and hence corruptstemp
variable. Am I right...? -
@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).
-
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 0anyone have a clue or guess?
@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".
-
@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).
@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
-
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 becausetemp
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. -
@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
@micha_eleric
Everything @ChrisW67 has said. Most likely isconst 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 intotemp
.... 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 whentemp
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 sizem_iMaxPoint
it is not!], I would bet $5 it writes beyond its end bound, and hence corruptstemp
variable. Am I right...? -
@micha_eleric
Everything @ChrisW67 has said. Most likely isconst 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 intotemp
.... 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 whentemp
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 sizem_iMaxPoint
it is not!], I would bet $5 it writes beyond its end bound, and hence corruptstemp
variable. Am I right...?By now you could/should have run under debugger, put
temp
into Watch window, single step over each statement, look to see whentemp
gets changed.Well, i am a wee bit new to qtcreater, and never used debugger in qtcreater.
where is the Watch window to puttemp
in? -
By now you could/should have run under debugger, put
temp
into Watch window, single step over each statement, look to see whentemp
gets changed.Well, i am a wee bit new to qtcreater, and never used debugger in qtcreater.
where is the Watch window to puttemp
in?@micha_eleric
Have you tried running the debugger? If you look around when you are doing so you will see the Watch window/pane. -
@micha_eleric
Have you tried running the debugger? If you look around when you are doing so you will see the Watch window/pane.@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.
-
@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.
@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. -
@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.@JonB i have always used print, to track what was happing
-
@JonB i have always used print, to track what was happing
@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. -
@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.@JonB std::cout and std::ofstream