title: C++ Seg Fault at end of function. Reference line = closing brace
body: <p>In conclusion: Thanks so much everyone! All the responses posted below were correct. The initial error was me forgetting to leave room for the NULL terminator. Strcpy() is a dangerous function because when I used it, it didn't know when the end of the 'string' was. Therefore, strcpy() grabbed to much data and overwrote the return address. </p>
  
  <p>EDIT: added more code from the program</p>
  
  <p>SOLVED: Honestly, my initial implementation was crap. I don't even know why I wrote swap that way if I wanted to swap out elements of the array. (At the time, each element only had the a char array in it. So I was able to get away with the old implementation). I have re-written it to:</p>
  
  <pre><code>void swap(ArrayElement list[], int index1, int index2) {
       ArrayElement temp;
       temp = list[index1];
       list[index1] = list[index2];
       list[index2] = temp;
  }
  </code></pre>
  
  <hr>
  
  <p>I'm having problems with a segmentation fault at the end of the following function.</p>
  
  <pre><code>struct ArrayElement {
      char data[SIZE_OF_ELEMENT];
      // Implemented this way so that I can expand to multiple values later on
  }
  
  //In main:
  ArrayElement* list = new ArrayElement[NUM_OF_ELEMENTS];
  
  void swap(ArrayElement list[], int index1, int index2) {
       char temp[SIZE_OF_ELEMENT];
       strcpy(temp, list[index2].data);
       strcpy(list[index2].data, list[index1].data);
       strcpy(list[index1].data, temp);
  }
  </code></pre>
  
  <p>The error is a segmentation fault at line 45, which is the ending curly brace of the function. This was compiled using g++. I used gbd to try and debug it and everything works correctly until it hits the curly brace.</p>
  
  <p>I can give more code from the program if it is needed. I don't want to post the entire thing because this is for a class.</p>