0

The core code of a simulation I am writing goes like:

for(int it = 0; it < Nt; it++)
{
   //cout << it << endl;
   variables = var_function(variables, it);
}

This code gives me SIGSEGV when run. To find out in which time it does the forbidden memory access happen I print on the screen with the line cout << it << endl;. The weird behavior is that when that line is un-commented, the SIGSEGV error disappears. What is going on?

  • Undefined Behaviour is going on. The "undefined" part means that it's perfectly fine to throw an error if there is no print and no error when print is added. – Yksisarvinen May 05 '21 at 09:44
  • @KarlKnechtel it does in a wide way, but would you know how could I find where exactly the code is leading to this undefined behavior? – Ali Esquembre Kucukalic May 05 '21 at 09:51
  • By using an actual debugger. Unfortunately, trace debugging will just not work for C or C++. – Karl Knechtel May 05 '21 at 09:52
  • @KarlKnechtel I am using the one that comes with Dev-C++ (I work on Windows). Any recommendation? Thank you for your answers :) – Ali Esquembre Kucukalic May 05 '21 at 10:10
  • @AliEsquembreKucukalic Keep removing code until the error goes away. Then add back the code you removed last. Repeat removing different code until you have a minimal piece of code that reproduces the error. Then share that code with us in a new question. – David Schwartz May 05 '21 at 12:03

0 Answers0