title: Segmentation fault happened in Linux but works in Mac
body: <p>I have a c code which wrote in my Mac laptop in Xcode but it didn't work in Linux system. </p>
  
  <p>I run this code by two ways:</p>
  
  <p>1.One is run in Eclipse but the while loop didn't look like finish.  Please find the message below:</p>
  
  <blockquote>
    <p>Please wait while calculating... </p>
  </blockquote>
  
  <p>But not more message in the console. It looks like while loop can't finish by some reason.</p>
  
  <p>2.The second way is that I complier the code directly in Linux environment by the command:</p>
  
  <blockquote>
    <p>cc -std=c99 main.c -o main</p>
  </blockquote>
  
  <p>Then run by the command:</p>
  
  <blockquote>
    <p>./main</p>
  </blockquote>
  
  <p>The message shows that:</p>
  
  <blockquote>
    <p>Please wait while calculating... Segmentation fault (core dumped)</p>
  </blockquote>
  
  <p>I checked by gdb</p>
  
  <blockquote>
    <p>Program received signal SIGSEGV, Segmentation fault.
    0x00007ffff7a9bd4a in ?? () from /lib/x86_64-linux-gnu/libc.so.6</p>
  </blockquote>
  
  <p>My data is saved in:</p>
  
  <blockquote>
    <p>/home/alan_yu/workspace/scandi.csv</p>
  </blockquote>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  #include &lt;stdlib.h&gt;
  
  #include &lt;string.h&gt;
  
  #include &lt;time.h&gt;
  
  char **split(char *line, char sep, int fields) {
  
      char **r = (char **)malloc(fields * sizeof(char*));
  
      int lptr = 0, fptr = 0;
  
      r[fptr++] = line;
  
      while (line[lptr]) {
  
          if (line[lptr] == sep) {
  
              line[lptr] = '\0';
  
              r[fptr] = &amp;(line[lptr+1]);
  
              fptr++;
  
          }
  
          lptr++;
  
      }
  
      return r;
  
  }
  
  int cmpfunc (const void * a, const void * b)
  
  {
  
      return *(double *)a &gt; *(double *)b ? 1 : -1;
  
  }
  
  #define LINE_SIZE 1000000
  
  #define EXPECTED_STOCK_SIZE 10000000
  
  void calculate2(char * fileName) {
  
       printf("Please wait while calculating...\n");
  
      // Open the file for reading.
  
      FILE *file = fopen(fileName, "r");
  
      // maximun size of the line to read.
  
      // memory allocation for the line to read.
  
      char* line = malloc(LINE_SIZE);
  
  //    char **stockNameArray = malloc( sizeof(char *) * EXPECTED_STOCK_SIZE);
  
  //    int stockNameArrayPos = 0;
  
      double *bidArray = malloc( sizeof(double) * EXPECTED_STOCK_SIZE );
  
      int bidArrayPos = 0;
  
      double *askArray = malloc( sizeof(double) * EXPECTED_STOCK_SIZE);
  
      int askArrayPos = 0;
  
      double *spreadArray = malloc( sizeof(double) * EXPECTED_STOCK_SIZE);
  
      int spreadArrayPos = 0;
  
      double sum=0;
  
      int i=0,j=0;
  
      while (fgets(line, LINE_SIZE, file)!= NULL){
  
      //  printf("Please wait while ...%d\n ", j);
          j++;
          char **fields = split(line, ',', 15);
  
          const char * volvbEquity = "VOLVB SS Equity";
  
          int comp = strcmp(fields[0], volvbEquity);
  
          if (comp == 0) {
  
              double bidValue = atof(fields[2]);
  
              double askValue = atof(fields[3]);
  
              bidArray[bidArrayPos++] = bidValue;
  
              askArray[askArrayPos++] = askValue;
  
              if (askValue - bidValue &gt; 0) {
  
                  double spreadValue = ((askValue - bidValue) / (askValue + bidValue) * 20000);
  
                  spreadArray[spreadArrayPos++] = spreadValue;
  
                  sum = sum + spreadValue;
  
              }
  
          }
  
      }
  
  
      //quick sort the spread
  
      qsort(spreadArray, spreadArrayPos, sizeof(double), cmpfunc);
  
      int mediumPos;
  
      double mean;
  
      double medium;
  
      if(spreadArrayPos % 2 == 0) {
  
          mediumPos = spreadArrayPos / 2;
  
          medium = (spreadArray[mediumPos] + spreadArray[mediumPos+1]) / 2;
  
      } else {
  
          mediumPos = (spreadArrayPos)/2 + 1;
  
          medium = spreadArray[mediumPos];
  
      }
  
      mean = sum / spreadArrayPos;
  
      printf("Please find mean and medium %f %f\n", mean, medium);
  
      free(bidArray);
  
      free(askArray);
  
      free(spreadArray);
  
  }
  
  int main(int argc, char **argv) {
  
      calculate2("/home/alan_yu/workspace/scandi.csv");
  
      return(0);
  
  }
  </code></pre>