Skip to main content
added 439 characters in body
Source Link

Your code is written in a hybrid C/C++ style. For instance your destructor has a delete (I can't find where the corresponding new is) and that is basically never needed. Use a std::vector to store array-like data.

Also you do a lot of parameter passing like void Nbody::update_position(Particle *p). Use references instead, and use const Particle &p if the particle is only read.

Otherwise it looks like an n-body code to me. It's quadratic rather than something more sophisticated/efficient, but that's probably ok.

Oh, I've found the new: you have Particle *p = new Particle[n]; in the class definition, but n is uninitialized. That is probably undefined behavior, definitely extremely dangerous, and most likely completely wrong.

Don't use new to allocate an array! Use std::vector, as follows:

std::vector<Particle> the_particles;
public:
  Particles(int n) : the_particles(vector<Particle>(n)) {}
}```

Your code is written in a hybrid C/C++ style. For instance your destructor has a delete (I can't find where the corresponding new is) and that is basically never needed. Use a std::vector to store array-like data.

Also you do a lot of parameter passing like void Nbody::update_position(Particle *p). Use references instead, and use const Particle &p if the particle is only read.

Otherwise it looks like an n-body code to me. It's quadratic rather than something more sophisticated/efficient, but that's probably ok.

Your code is written in a hybrid C/C++ style. For instance your destructor has a delete (I can't find where the corresponding new is) and that is basically never needed. Use a std::vector to store array-like data.

Also you do a lot of parameter passing like void Nbody::update_position(Particle *p). Use references instead, and use const Particle &p if the particle is only read.

Otherwise it looks like an n-body code to me. It's quadratic rather than something more sophisticated/efficient, but that's probably ok.

Oh, I've found the new: you have Particle *p = new Particle[n]; in the class definition, but n is uninitialized. That is probably undefined behavior, definitely extremely dangerous, and most likely completely wrong.

Don't use new to allocate an array! Use std::vector, as follows:

std::vector<Particle> the_particles;
public:
  Particles(int n) : the_particles(vector<Particle>(n)) {}
}```
added 21 characters in body
Source Link

Your code is written in a hybrid C/C++ style. For instance your destructor has a delete (I can't find where the corresponding new is) and that is basically never needed. Use a std::vector to store array-like data.

Also you do a lot of parameter passing like void Nbody::update_position(Particle *p). Use references instead, and use const Particle &p isif the particle is only read.

Otherwise it looks like an n-body code to me. It's quadratic rather than something smartermore sophisticated/efficient, but that's probably ok.

Your code is written in a hybrid C/C++ style. For instance your destructor has a delete (I can't find where the corresponding new is) and that is basically never needed. Use a std::vector to store array-like data.

Also you do a lot of parameter passing like void Nbody::update_position(Particle *p). Use references instead, and use const Particle &p is the particle is only read.

Otherwise it looks like an n-body code to me. It's quadratic rather than something smarter, but that's probably ok.

Your code is written in a hybrid C/C++ style. For instance your destructor has a delete (I can't find where the corresponding new is) and that is basically never needed. Use a std::vector to store array-like data.

Also you do a lot of parameter passing like void Nbody::update_position(Particle *p). Use references instead, and use const Particle &p if the particle is only read.

Otherwise it looks like an n-body code to me. It's quadratic rather than something more sophisticated/efficient, but that's probably ok.

Source Link

Your code is written in a hybrid C/C++ style. For instance your destructor has a delete (I can't find where the corresponding new is) and that is basically never needed. Use a std::vector to store array-like data.

Also you do a lot of parameter passing like void Nbody::update_position(Particle *p). Use references instead, and use const Particle &p is the particle is only read.

Otherwise it looks like an n-body code to me. It's quadratic rather than something smarter, but that's probably ok.