title: Segmentation Fault due to vectors
body: <p>I have a program that is supposed to calculate the trajectory of a pendulum with the fourth order runge kutta method and the euler cromer method, unfortunately... I have a segfault.</p>
  
  <p>I have no idea how to fix this, as you can see I have a few std::cout 's to attempt debugging, but they don't even output. This leads me to believe that the function never makes it to executing main();</p>
  
  <p>My current intuition says that it errors out during the function declarations, but if I am wrong I would like to know. If at all possible, can you describe the techniques you used to debug?</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cmath&gt;
  #include &lt;string&gt;
  #include &lt;vector&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;fstream&gt;
  
  #define N 1
  
  // Pendulum Variables
  double d_pendLength = 1.0, d_pendMass = 1.0; // Pendulum Properties
  double d_viscParam = 1.0, d_gravity = 1.0; //9.80665; // Enviromental Factors
  double d_dAmp = 0.0, d_dFreq = 0.0; // Driving Force
  
  double d_initTheta = 0.0, d_initAVel = 0.0; //Initial Conditions
  
  void v_rungekutta(double d_time, std::vector&lt;double&gt; d_pendulum, double d_step);
  void v_eulercromer(double d_time, std::vector&lt;double&gt; d_pendulum, double d_step);
  double d_derivative(double d_time, std::vector&lt;double&gt; d_pendulum, int i_de);
  
  int main(void)
  {
      // Numerical Variables
      double d_step = 0.01, d_mTime = -1, d_mPeriod  = 300;
  
      // Global Variables
      double d_time = 0.0, d_period = 0.0, d_limit;
  
      // Function Vectors
      std::vector&lt;double&gt; d_pendulum (2);
  
      // Settings
      std::string plot = "theta", algo = "rk4";
  
  
      std::cout &lt;&lt; "START!";
      std::ofstream file_output;
      std::cout &lt;&lt; "HAIDATA!";
      file_output.open("pendulum.data");
  
      std::cout &lt;&lt; "HAI!";
      d_pendulum.at(0)= d_initTheta;  //Initial Theta
      d_pendulum.at(1)= d_initAVel;   //Initial Omega
      std::cout &lt;&lt;"BAI!";
  
      if (d_mPeriod &gt; 0)
      {
          d_limit = d_mPeriod;
      }
      else if (d_mTime &gt; 0)
      {
          d_limit = d_mTime;
      }
      else
      {
          std::cout &lt;&lt; "No Limit Specified";
          exit(1);
      }
  
      for (int i_currentStep=1; i_currentStep*d_step&lt;=d_limit;i_currentStep++)       
      {
          d_time = i_currentStep*d_step;
  
          if (algo == "rk4")
          {
              v_rungekutta(d_time, d_pendulum, d_step);
          }
          else if (algo == "ec")
          {
              v_eulercromer(d_time, d_pendulum, d_step);
          }
          file_output &lt;&lt; d_time &lt;&lt; d_pendulum.at(0) &lt;&lt; d_pendulum.at(1) &lt;&lt; std::endl;
      }
  
  
      file_output.close();
      d_pendulum.clear();
  
      return 0;
  }
  
  
  void v_rungekutta(double d_time, std::vector&lt;double&gt; d_pendulum, double d_step)
  {
      double h=d_step/2.0;  
      std::vector&lt;double&gt; t1, t2, t3, k1, k2, k3, k4;
      int i;
  
      for (i=N;i&gt;0;i--) t1[i]=d_pendulum.at(i)+0.5*(k1[i]=d_step*d_derivative(d_time, d_pendulum, i));
      for (i=N;i&gt;0;i--) t2[i]=d_pendulum.at(i)+0.5*(k2[i]=d_step*d_derivative(d_time+h, t1, i));
      for (i=N;i&gt;0;i--) t3[i]=d_pendulum.at(i)+ (k3[i]=d_step*d_derivative(d_time+h, t2, i));
      for (i=N;i&gt;0;i--) k4[i]=d_step*d_derivative(d_time+d_step, t3, i);
  
      for (i=N;i&gt;0;i--) d_pendulum.at(i) += (k1[i]+2*k2[i]+2*k3[i]+k4[i])/6.0;
  }
  
  void v_eulercromer(double d_time, std::vector&lt;double&gt; d_pendulum, double d_step)
  {
      int i;
      for (i=N;i&gt;0;i--) d_pendulum.at(i) += d_derivative(d_time, d_pendulum, i)*d_step;
  }
  
  double d_derivative(double d_time, std::vector&lt;double&gt; d_pendulum, int i_de)
   {
      double dtheta = d_pendulum.at(1);
    if (i_de==0) return dtheta;
    double domega = d_pendulum.at(1)+((-d_gravity/d_pendLength)*sin(d_pendulum.at(0)))+(-d_viscParam*(d_pendulum.at(1)))+(d_dAmp*sin(d_dFreq*d_time));
    if (i_de==1) return domega; 
    if (i_de &lt; 0) return 0; // 0 is a possible value, exit with exit func.
  }
  </code></pre>
  
  <h1>SOLUTION</h1>
  
  <p>My problem was that I had not been properly accessing the vectors in rungekutta and I had not given them a starting size.</p>
  
  <p>Before</p>
  
  <pre><code>    void v_rungekutta(double d_time, std::vector&lt;double&gt; d_pendulum, double d_step)
      {
          double h=d_step/2.0;  
          std::vector&lt;double&gt; t1, t2, t3, k1, k2, k3, k4;
          int i;
  
          for (i=N;i&gt;0;i--) t1[i]=d_pendulum.at(i)+0.5*(k1[i]=d_step*d_derivative(d_time, d_pendulum, i));
          for (i=N;i&gt;0;i--) t2[i]=d_pendulum.at(i)+0.5*(k2[i]=d_step*d_derivative(d_time+h, t1, i));
          for (i=N;i&gt;0;i--) t3[i]=d_pendulum.at(i)+ (k3[i]=d_step*d_derivative(d_time+h, t2, i));
          for (i=N;i&gt;0;i--) k4[i]=d_step*d_derivative(d_time+d_step, t3, i);
  
          for (i=N;i&gt;0;i--) d_pendulum.at(i) += (k1[i]+2*k2[i]+2*k3[i]+k4[i])/6.0;
      }
  </code></pre>
  
  <p>After:</p>
  
  <pre><code>    void v_rungekutta(double d_time, std::vector&lt;double&gt; d_pendulum, double d_step)
      {
          double h=d_step/2.0;  
          std::vector&lt;double&gt; t1 (2), t2 (2), t3 (2), k1 (2), k2 (2), k3 (2), k4 (2);
          int i;
  
          for (i=N;i&gt;0;i--) t1.at(i)=d_pendulum.at(i)+0.5*(k1.at(i)=d_step*d_derivative(d_time, d_pendulum, i));
          for (i=N;i&gt;0;i--) t2.at(i)=d_pendulum.at(i)+0.5*(k2.at(i)=d_step*d_derivative(d_time+h, t1, i));
          for (i=N;i&gt;0;i--) t3.at(i)=d_pendulum.at(i)+ (k3.at(i)=d_step*d_derivative(d_time+h, t2, i));
          for (i=N;i&gt;0;i--) k4.at(i)=d_step*d_derivative(d_time+d_step, t3, i);
  
          for (i=N;i&gt;0;i--) d_pendulum.at(i) += (k1[i]+2*k2[i]+2*k3[i]+k4[i])/6.0;
      }
  </code></pre>