-2

this is my first post here. I've just started coding in C++ and tried to write a progamm, that does a physical simulation. It should take a command line argument and depending what is given creates a specific class, which determines what method is used.

static Integrator*
CreateIntegrator( int argc, char** argv )
{
Integrator * integrator;

if(string(argv[1])=="Euler")
{
    EulerIntegrator Euler;
    integrator=&Euler;
    return integrator;

}
else if (string(argv[1])=="Runge-Kutta-2")
{
    SecondOrderRungeKuttaIntegrator Kutta2;
    integrator=&Kutta2;
    return integrator;

}
else if (string(argv[1])=="Runge-Kutta-4")
{
    FourthOrderRungeKuttaIntegrator Kutta4;
    integrator=&Kutta4;
    return integrator;

}
else
{
    return 0;
}

}

static void
RunSimulation( std::vector<Particle>& particle, Integrator* integrator )
{
static const int    FrameCount  = 500;
static const int    StepCount   = 10;
static const double TimeStep    = 0.01;

for( int frame = 0; frame < FrameCount; ++frame )
{
    for( int step = 0; step < StepCount; ++step )
        particle = integrator->update_particles( particle, TimeStep );

    PrintParticles( particle, frame );
}
}

int
main( int argc, char** argv )
{
Integrator* integrator = CreateIntegrator( argc, argv );
if( integrator != 0 )
{
    std::vector<Particle> particle = ReadParticles();
    RunSimulation( particle, integrator );
}

When I run this my programm crashes and shows follow error:

Program received signal SIGSEGV, Segmentation fault. 0x000000000040259b in RunSimulation (particle=..., integrator=0x22fd30) at ..\test\test.cpp:116 116 particle = integrator->update_particles( particle, TimeStep );

But if I write create the class in the main function everything works fine:

int
main( int argc, char** argv )
{
FourthOrderRungeKuttaIntegrator* integrator;
FourthOrderRungeKuttaIntegrator Kutta4;
integrator=&Kutta4;
if( integrator != 0 )
{
    std::vector<Particle> particle = ReadParticles();
    RunSimulation( particle, integrator );
}

I hope somebody can help me to fix this problem. Thank you very much!

Tholtus
  • 25
  • 4
  • Your compiler does not give any warning about returning the address of a local variable? You should enable (more) warnings. See http://stackoverflow.com/questions/6441218/can-a-local-variables-memory-be-accessed-outside-its-scope – J.J. Hakala May 23 '16 at 23:26
  • Possible duplicate of [What is a dangling pointer](http://stackoverflow.com/questions/17997228/what-is-a-dangling-pointer) – user657267 May 23 '16 at 23:28

1 Answers1

0

The problem is that the Integrators you create are destroyed when the function CreateIntegrator finishes execution. For example:

if(string(argv[1])=="Euler") {
  EulerIntegrator Euler;
  integrator=&Euler;
  return integrator;
}

Here, the instance of Euler is local within this scope and will be destroyed when you exit the function. This instance is stored in the stack. You need to create it in the heap. To do so you need to do something like this:

if(string(argv[1])=="Euler") {
  EulerIntegrator *Euler = new Euler();
  integrator = Euler;
  return integrator;
}

Make sure to delete the pointer after you use it or, even better, use a unique_ptr if you use C++11 or above.

Ilias
  • 41
  • 1
  • 4