title: C++: Compiler-specific segmentation fault when storing data from file into array
body: <p>I am writing a C++ program that takes in number from a file ("input10") of this format:</p>
  
  <pre><code>1    0    12
  2    3    24
  3    8    9
  4    9    2
  5    10   3
  6    11   4
  7    14   8
  8    16   10
  9    16   3
  10   17   12
  </code></pre>
  
  <p>Each number goes into an array <code>p_data[3]</code> (three numbers to each line).</p>
  
  <p>When running in Visual Studio, there is no error and the correct data is stored in the array. I include the <code>#define _CRT_SECURE_NO_WARNINGS</code> here so that I can use <code>strncpy</code>. I am not sure if <code>strncpy</code> is the source of the issue.</p>
  
  <p>When running with <code>g++ -std=c++11</code> on a CentOS terminal, I get the following error message:</p>
  
  <blockquote>
    <p>Segmentation fault (core dumped)</p>
  </blockquote>
  
  <p>I am finding this issue really difficult to debug. Any thoughts?</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cstring&gt;
  #include &lt;string&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;fstream&gt;
  using namespace std;
  
  void get_processes() {
      string curr_line;
      ifstream infile("input10");
      int p_data[3];
      int p = 0;
      char line[1024];
  
      while (getline(infile, curr_line)) {
          strncpy(line, curr_line.c_str(), sizeof(line));
          line[sizeof(curr_line) - 1] = 0;
  
          int p_data_count = 0;
          for (int i = 0; line[i] != '\0'; ++i) {
              if (line[i] != ' ') {
                  string num = "";
                  while (line[i] != ' ') {
                      num += line[i];
                      ++i;
                  }
  
                  if (p_data_count &lt; 3) {
                      p_data[p_data_count] = stoi(num);
                      ++p_data_count;
                  }
              }
          }
      }
  
      infile.close();
  }
  
  int main() {
      get_processes();
  
      return 0;
  }
  </code></pre>