SIGTRAP does not show up if I write "cout"

I am writing a Particle-In-Cell code to simulate plasma-laser interactions. The core of the code is the following (all the variables have been initialized properly before this):

for(int it = 0; it < Nt-1; it++)
        //cout << "it = " << it << endl;
        pos = pos_update(pos, vel, qm, E_P, it);
        vel = vel_update(pos, vel, it);
        rho_part = rho_part_update(rho_part, pos, qm, it);
        rho = rho_update(rho, rho_part, it);
        E_P = E_P_update(E_P, pos, rho, rho_part, it);

As you can see cout is commented. If so, when executing the program it eventually stops and the debugger shows the message "Program received signal SIGTRAP, Trace/breakpoint trap". The weird thing is that if I allow that "cout" line of code (which I wrote to find out in which time the code fails), then there is no error and the code works as expected. Any idea of what is happening? If you need the detailed functions

1 answer

  • answered 2021-05-04 10:34 MSalters

    SIGTRAP suggests that your C++ code has Undefined Behavior. But SIGTRAP is just one of the infinite ways in which Undefined Behavior can manifest itself. It's not that strange that adding another statement causes Undefined Behavior to show up in a different manner.

    As for your comment, new[] and delete are primitives that should generally be avoided. std::vector is probably the cleaner solution. But if you do use then, then new[] must be paired with delete[], not delete.