# Segmentation Violation in simulation-particles-code

**URL:** <https://root-forum.cern.ch/t/segmentation-violation-in-simulation-particles-code/27382>\
**Category:** ROOT\
**Created:** [December 26, 2017, 11:10pm UTC](https://root-forum.cern.ch/t/segmentation-violation-in-simulation-particles-code/27382 "2017-12-26T23:10:48Z")\
**Posts on this page:** 1\
**Showing post:** 8

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [December 30, 2017, 10:42pm UTC](https://root-forum.cern.ch/t/segmentation-violation-in-simulation-particles-code/27382/8 "2017-12-30T22:42:01Z")

</div>

```auto
{
  // name this ROOT macro file "RunMe.cxx" and then debug everything using:
  // valgrind --tool=memcheck --leak-check=full --suppressions=`root-config --etcdir`/valgrind-root.supp `root-config --bindir`/root.exe -b -n -q -l RunMe.cxx
  // valgrind --tool=exp-sgcheck --suppressions=`root-config --etcdir`/valgrind-root.supp `root-config --bindir`/root.exe -b -n -q -l RunMe.cxx
  // note: study valgrind's output messages from running the "Mainmodule"
  gROOT->LoadMacro("ParticleType.cxx++g");
  gROOT->LoadMacro("ResonanceType.cxx++g");
  gROOT->LoadMacro("Particle.cxx++g");
  gROOT->LoadMacro("Mainmodule.cxx++g");
  std::cout << " ... starting Mainmodule() ..." << std::endl;
  gROOT->ProcessLine("Mainmodule();");
  std::cout << " ... finished Mainmodule() ..." << std::endl;
}

```

[Particle.h](https://root-forum.cern.ch/uploads/default/original/2X/7/740ad694f431541b533339d8e0e9e864c64a2db4.h) (984 Bytes)  
[Particle.cxx](https://root-forum.cern.ch/uploads/default/original/2X/b/b2fb15b2fe753887ac51d07f6558f72813426aa3.cxx) (4.3 KB)  
[Particelle.root](https://root-forum.cern.ch/uploads/default/original/2X/9/9b6a2a833a6cea15f4b06fe6212955d42cc34de3.root) (8.6 KB)

I think, the main problem, [as reported by the AddressSanitizer](https://root-forum.cern.ch/t/addresssanitizer-memory-error-detector/21046), is in the “Mainmodule.cxx”, where you define `Particle particella [110];` but then you access `particella[100+n+1]` without any check that `n<=8`. The simplest “brutal fix” could be to define `Particle particella [(100 + (100 * 2))];`.

BTW. In the “Mainmodule.cxx”, better use `delete file;`, instead of `file->Close();`.

---

_[View the full topic](https://root-forum.cern.ch/t/segmentation-violation-in-simulation-particles-code/27382)._
