Segmentation violation error

hello rooters,

I’m new to root and not really a veteran when it comes to c++ programming.
Despite that, I managed to write a rather simple c++ code that does work IF I do not run it on root, meaning that it goes just fine compiling it with g++ and running the a.out on the terminal. This is the program:

#include "particleType.h"
#include "resonanceType.h"
#include "particle.h"
#include <cstdlib>
#include <cmath>
#include <iostream>


int main(){
 
 particle::addParticleType("pi+", 0.13957, 1);
 particle::addParticleType("pi-", 0.13957, -1);
 particle::addParticleType("ka+", 0.49367, 1);
 particle::addParticleType("ka-", 0.49367, -1);
 particle::addParticleType("pr+", 0.93827, 1);
 particle::addParticleType("pr-", 0.93827, -1);
 particle::addParticleType("K*", 0.89166, 0, 0.050);

 particle::printParticleType();

 double phi; //azimuthal coordinate 0-2pi uniform distribution
 double theta; //polar coordinate 0-pi uniform distribution
 double t; // 0-1 uniform distribution
 const float pi = 3.141592;

 double p;  
 double prm=1; //parameter of exponential distribution for impulse 
 double x;  //needed for proportion among generated types
 double y;  // needed for proportion between negative charge and positive ones
 double invrand=1./RAND_MAX; //invrand needed for distributions (example: rand()*invrand generates random number between 0 and 1)
 double Px,Py,Pz;
 particle particles[200];
 int Nparticle; //number of generated particles
 
 for(int i=0; i<100000; i++){ //10^5 events
  Nparticle = 0;
  for(int k=0; k<100; k++){ //100 particles generated per event
   t=rand()*invrand; //uniform distribution between 0 and 1
   phi=rand()*invrand*2*pi; //uniform distribution between 0 and 2pi
   theta=rand()*invrand*pi; //uniform distribution between 0 and pi
   p=-log(1-t)/prm; //exponential distribution of impulse p with parameter prm
   Px=p*cos(phi)*sin(theta);
   Py=p*sin(phi)*sin(theta);
   Pz=p*cos(theta);

   particles[Nparticle].setP(Px, Py, Pz);

   x=rand()*invrand; //random distribution between 0 and 1
   y=rand()*invrand; //same as above
   if (x<0.8){  //80% probability to generate pi
    if(y<0.5)  particles[Nparticle].changeParticleType("pi+"); //50% probability for pi to have positive charge
    else particles[Nparticle].changeParticleType("pi-"); } //same for pi having negative charge
   else if (x<0.9){ //10% probability to generate ka
    if(y<0.5) particles[Nparticle].changeParticleType("ka+");
    else particles[Nparticle].changeParticleType("ka-");}
   else if (x<0.99){ //9% probability to generate pr
    if(y<0.5) particles[Nparticle].changeParticleType("pr+");
    else particles[Nparticle].changeParticleType("pr-");}
   else { particles[Nparticle].changeParticleType("K*"); //1% probability to generate resonance K
    if(y<0.5){
     particles[Nparticle+1].changeParticleType("pi+");
     particles[Nparticle+2].changeParticleType("ka-"); }
    else {
     particles[Nparticle+1].changeParticleType("pi-");
     particles[Nparticle+2].changeParticleType("ka+"); } //resonance K decays in pi+ and k- or pi- and k+ with equal probability
   particles[Nparticle].Decay2body(particles[Nparticle+1], particles[Nparticle+2]); 
   //Decay2body gives decayed particles impulses resulting from decayment
   Nparticle++;
   Nparticle++;} //if K is generated, 3 particles in total must be added to Nparticles
   Nparticle++;}
   }
}

the .h and .cpp files listed below:
particleType.cpp (344 Bytes)
particleType.h (543 Bytes)
resonanceType.cpp (411 Bytes)
resonanceType.h (357 Bytes)
particle.cpp (6.13 KB)
particle.h (1.57 KB)

this is the output i get when running it through root:

root [0] .L particle.cpp
root [1] .L resonanceType.cpp
root [2] .L particleType.cpp
root [3] .L main.cpp
root [4] .x main.cpp
name: pi+
mass: 0.13957
charge: 1
name: pi-
mass: 0.13957
charge: -1
name: ka+
mass: 0.49367
charge: 1
name: ka-
mass: 0.49367
charge: -1
name: pr+
mass: 0.93827
charge: 1
name: pr-
mass: 0.93827
charge: -1
name: K*
mass: 0.89166
charge: 0
width: 0.05


 *** Break *** segmentation violation



===========================================================
There was a crash.
This is the entire stack trace of all threads:
===========================================================
#0  0x00007f4871b5764a in __GI___waitpid (pid=26737, stat_loc=stat_loc
entry=0x7ffe2eb129c0, options=options
entry=0) at ../sysdeps/unix/sysv/linux/waitpid.c:29
#1  0x00007f4871ad0fab in do_system (line=<optimized out>) at ../sysdeps/posix/system.c:148
#2  0x00007f4872848dd4 in TUnixSystem::StackTrace() () from /usr/lib/x86_64-linux-gnu/libCore.so.5.34
#3  0x00007f487284b03c in TUnixSystem::DispatchSignals(ESignals) () from /usr/lib/x86_64-linux-gnu/libCore.so.5.34
#4  <signal handler called>
#5  0x00007f48711cde29 in G__LD_P10_char () from /usr/lib/x86_64-linux-gnu/libCint.so.5.34
#6  0x00007f487129d02b in G__exec_asm () from /usr/lib/x86_64-linux-gnu/libCint.so.5.34
#7  0x00007f48712a48b2 in G__exec_bytecode () from /usr/lib/x86_64-linux-gnu/libCint.so.5.34
#8  0x00007f48711e1427 in G__interpret_func () from /usr/lib/x86_64-linux-gnu/libCint.so.5.34
#9  0x00007f48712a22ff in G__exec_asm () from /usr/lib/x86_64-linux-gnu/libCint.so.5.34
#10 0x00007f48712a48b2 in G__exec_bytecode () from /usr/lib/x86_64-linux-gnu/libCint.so.5.34
#11 0x00007f48711e1427 in G__interpret_func () from /usr/lib/x86_64-linux-gnu/libCint.so.5.34
#12 0x00007f48712b3c7b in G__getfunction () from /usr/lib/x86_64-linux-gnu/libCint.so.5.34
#13 0x00007f487123fa00 in G__getstructmem(int, G__FastAllocString&, char*, int, char*, int*, G__var_array*, int) () from /usr/lib/x86_64-linux-gnu/libCint.so.5.34
#14 0x00007f4871235f3b in G__getvariable () from /usr/lib/x86_64-linux-gnu/libCint.so.5.34
#15 0x00007f48711a7dc3 in G__getitem () from /usr/lib/x86_64-linux-gnu/libCint.so.5.34
#16 0x00007f48711ae6e0 in G__getexpr () from /usr/lib/x86_64-linux-gnu/libCint.so.5.34
#17 0x00007f487127877e in G__exec_statement () from /usr/lib/x86_64-linux-gnu/libCint.so.5.34
#18 0x00007f487127faae in G__exec_statement () from /usr/lib/x86_64-linux-gnu/libCint.so.5.34
#19 0x00007f487127faae in G__exec_statement () from /usr/lib/x86_64-linux-gnu/libCint.so.5.34
#20 0x00007f487127ed9b in G__exec_statement () from /usr/lib/x86_64-linux-gnu/libCint.so.5.34
#21 0x00007f487127ed9b in G__exec_statement () from /usr/lib/x86_64-linux-gnu/libCint.so.5.34
#22 0x00007f487128383c in ?? () from /usr/lib/x86_64-linux-gnu/libCint.so.5.34
#23 0x00007f487128082b in G__exec_statement () from /usr/lib/x86_64-linux-gnu/libCint.so.5.34
#24 0x00007f487128383c in ?? () from /usr/lib/x86_64-linux-gnu/libCint.so.5.34
#25 0x00007f487128082b in G__exec_statement () from /usr/lib/x86_64-linux-gnu/libCint.so.5.34
#26 0x00007f48711e23e0 in G__interpret_func () from /usr/lib/x86_64-linux-gnu/libCint.so.5.34
#27 0x00007f48712b3ce0 in G__getfunction () from /usr/lib/x86_64-linux-gnu/libCint.so.5.34
#28 0x00007f48711a82a6 in G__getitem () from /usr/lib/x86_64-linux-gnu/libCint.so.5.34
#29 0x00007f48711ae6e0 in G__getexpr () from /usr/lib/x86_64-linux-gnu/libCint.so.5.34
#30 0x00007f48711b8274 in G__calc_internal () from /usr/lib/x86_64-linux-gnu/libCint.so.5.34
#31 0x00007f48712bdfb4 in G__process_cmd () from /usr/lib/x86_64-linux-gnu/libCint.so.5.34
#32 0x00007f487280d8b0 in TCint::ProcessLine(char const*, TInterpreter::EErrorCode*) () from /usr/lib/x86_64-linux-gnu/libCore.so.5.34
#33 0x00007f487280c5f7 in TCint::ProcessLineSynch(char const*, TInterpreter::EErrorCode*) () from /usr/lib/x86_64-linux-gnu/libCore.so.5.34
#34 0x00007f48727e77dc in TApplication::ExecuteFile(char const*, int*, bool) () from /usr/lib/x86_64-linux-gnu/libCore.so.5.34
#35 0x00007f48727e8880 in TApplication::ProcessLine(char const*, bool, int*) () from /usr/lib/x86_64-linux-gnu/libCore.so.5.34
#36 0x00007f48724093fb in TRint::HandleTermInput() () from /usr/lib/x86_64-linux-gnu/libRint.so.5.34
#37 0x00007f487284a70c in TUnixSystem::CheckDescriptors() () from /usr/lib/x86_64-linux-gnu/libCore.so.5.34
#38 0x00007f487284b36a in TUnixSystem::DispatchOneEvent(bool) () from /usr/lib/x86_64-linux-gnu/libCore.so.5.34
#39 0x00007f48727bb904 in TSystem::InnerLoop() () from /usr/lib/x86_64-linux-gnu/libCore.so.5.34
#40 0x00007f48727b9bdf in TSystem::Run() () from /usr/lib/x86_64-linux-gnu/libCore.so.5.34
#41 0x00007f48727e5e1f in TApplication::Run(bool) () from /usr/lib/x86_64-linux-gnu/libCore.so.5.34
#42 0x00007f487240a4bb in TRint::Run(bool) () from /usr/lib/x86_64-linux-gnu/libRint.so.5.34
#43 0x0000000000400ffc in main ()
===========================================================


The crash is most likely caused by a problem in your script.
Try to compile it (.L myscript.C+g) and fix any errors.
If that does not help then please submit a bug report at
http://root.cern.ch/bugs. Please post the ENTIRE stack trace
from above as an attachment in addition to anything else
that might help us fixing this issue.


Root > Function main() busy flag cleared
Function changeParticleType() busy flag cleared
Function FindParticle() busy flag cleared

at the current state, if i run it without root, i get the same output with no errors at all, as it should be. clearly it takes a while (3 to 4 seconds) to complete the double for cycle, but still i get no errors.
I have no idea why root complains, though I am pretty sure I made some trivial mistake.
I am on ubuntu 16.04 LTS, ROOT version: 5.34

thanks in advance,

lorenzo

Try your macros with the latest ROOT 6 release.

If you want to stay with ROOT 5, try to use your macros as ACLiC precompiled:
root [0] .L particle.cpp++
root [1] .L resonanceType.cpp++
root [2] .L particleType.cpp++
root [3] .x main.cpp++

I already made an attempt using ACLiC pre-compilation but it did not work: compilation failed due to some errors regarding missing references.
Anyway, I’ve tried again changing the loading order of the macros and everything went fine. Apparently, the problem with ACLiC was that I couldn’t load particle.cpp before particleType.cpp, since some calls made in the former were defined only in the latter. A trivial mistake, my bad.

Pre-compiling the macros in the right order works well: no segmentation error.
Thanks for the help!