title: Malloc ~ Segfault C
body: <p>I've been trying to figure out this segfault for quite some time now, ran it through GDB and when I type 'where' this is what comes up:</p>
  
  <pre><code> #0  0x00007ffff7a9bd4a in ?? () from /lib/x86_64-linux-gnu/libc.so.6
   #1  0x00007ffff7a9dfb5 in malloc () from /lib/x86_64-linux-gnu/libc.so.6
   #2  0x00000000004013d1 in CreateGenetic (cityStart=8, cityNum=9, cityWeights=0x7fffffffd270, 
  Genetic=0x604160) at main.c:175
   #3  0x0000000000400cb9 in main (argc=2, argv=0x7fffffffdfd8) at main.c:87
  </code></pre>
  
  <p>Also tried compiling with the params of -Wall -Wextra -O and nothing worth noting came up.
  Anyways, here is the code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;sys/time.h&gt;
  #include &lt;time.h&gt;
  typedef struct BruteForce *BF;
  typedef struct GeneticAlgorithm *GA;
  int Fact(int);
  int CreateBrute(int, int, double[20][20], BF);
  int Permutations(int, int, int*, int, double**, double[20][20], BF);
  int CalcWeight(int, int, int*, double**, double [20][20], BF);
  int CreateGenetic(int, int, double [20][20], GA);
  int GeneticWeight(int, int, int *, double **, double [20][20], GA);
  int IterativePerm(int, int, int *, int, double **, double [20][20], GA);
  
  struct BruteForce {
  double eliteOne[20];
  double eliteTwo[20];
  int size;
  };
  
  struct GeneticAlgorithm {
  double elite[20][20];
  int size;
  int generations;
  int runs;
  double mutation;
  int permutation;
  };
  
  int main(int argc, char *argv[]) {
  int i = 0, j = 0;
  if (argc != 2) {
      printf("Missing arguments!\n");
      exit(1);
  }
  FILE *data = fopen(argv[1], "r");
  if (data == 0) {
      printf("Could not open file\n");
      exit(1);
  }
  double cityWeights[20][20];
  double temp;
  /********************************************
   *Inputs city weights into 2D array
   *******************************************/
  while (!feof(data)) {
      fscanf(data, "%lf", &amp;temp);
      if (i == j &amp;&amp; j != 19) {
          cityWeights[i][j] = 0;
          j++;
      } else if (i == j &amp;&amp; j &gt;= 19) break;
      cityWeights[i][j] = temp;
      cityWeights[j][i] = temp;
      if (j == 19 &amp;&amp; i != 19) {
          i++;
          j = i;
      } else j++;
  }
  fclose(data);
  BF Brute = malloc(sizeof (struct BruteForce));
  GA Genetic = malloc(sizeof (struct GeneticAlgorithm));
  int cityNum, cityStart;
  printf("Number of cities in current run : \n");
  scanf("%i", &amp;cityNum);
  printf("Starting city : \n");
  scanf("%i", &amp;cityStart);
  while (cityStart &gt; cityNum || cityStart &lt;= 0) {
      printf("Starting city must be greater than zero and less than number of cities\n");
      printf("Starting city : \n");
      scanf("%i", &amp;cityStart);
  }
  printf("Number of generations to run : \n");
  scanf("%i", &amp;Genetic-&gt;generations);
  printf("Number of individuals in each generation : \n");
  scanf("%i", &amp;Genetic-&gt;runs);
  printf("Percentage of each generation to be made with mutations : \n");
  scanf("%lf", &amp;Genetic-&gt;mutation);
  Genetic-&gt;mutation = (int) ((Genetic-&gt;runs * (Genetic-&gt;mutation / 100)) + 0.5);
  Genetic-&gt;permutation = Genetic-&gt;runs - Genetic-&gt;mutation;
  CreateBrute(cityStart, cityNum, cityWeights, Brute);
  CreateGenetic(cityStart, cityNum, cityWeights, Genetic);
  }
  
  int Fact(int x) {
  int i, z = 1;
  for (i = 2; i &lt;= x; i++) z *= i;
  return z;
  }
  
  int CreateBrute(int cityStart, int cityNum, double cityWeights[20][20], BF Brute) {
  int i = cityNum - 1;
  i = Fact(i);
  double** tour;
  int x,y;
  tour = (double **)malloc(i * sizeof (double*));
  for (x = 0; x &lt; i; x++)
      tour[x] = (double *) malloc(cityNum * sizeof (double));
  for (i = 0; i &lt; cityNum; i++)
      tour[0][i] = i;
  tour[0][0] = cityStart - 1;
  tour[0][cityStart - 1] = 0;
  i = cityNum - 1;
  i = Fact(i);
  int count = 1, *countP;
  countP = &amp;count;
  Brute-&gt;size = cityNum;
  Brute-&gt;eliteOne[cityNum] = 9999;
  Brute-&gt;eliteTwo[cityNum] = 9999;
  Permutations(i, cityNum, countP, cityNum, tour, cityWeights, Brute);
  printf("eliteOne is : \n");
  for (i = 0; i &lt;= cityNum; i++) printf("%lf ", Brute-&gt;eliteOne[i]);
  printf("\n");
  printf("eliteTwo is : \n");
  for (i = 0; i &lt;= cityNum; i++) printf("%lf ", Brute-&gt;eliteTwo[i]);
  printf("\n");
  i = cityNum - 1;
  i = Fact(i);
  
  }
  
  int Permutations(int row, int col, int* count, int n, double** tour, double cityWeights[20][20], BF Brute) {
  int temp, i, x;
  if (n == 1) CalcWeight(row, col, count, tour, cityWeights, Brute);
  else {
      for (i = 1; i &lt; n; i++) {
          temp = tour[0][i];
          tour[0][i] = tour[0][n - 1];
          tour[0][n - 1] = temp;
          Permutations(row, col, count, (n - 1), tour, cityWeights, Brute);
          temp = tour[0][i];
          tour[0][i] = tour[0][n - 1];
          tour[0][n - 1] = temp;
      }
  }
  }
  
  int CalcWeight(int row, int col, int* count, double** tour, double cityWeights[20][20], BF Brute) {
  double weight;
  int i, xOne, xTwo;
  weight = 0;
  //Calculates weight of tour
  for (i = 0; i &lt; (col - 1); i++) {
      xOne = tour[0][i];
      xTwo = tour[0][i + 1];
      weight += cityWeights[xOne][xTwo];
  }
  xOne = tour[0][col - 1];
  xTwo = tour[0][0];
  weight += cityWeights[xOne][xTwo];
  tour[0][col] = weight;
  if (*count == row) return;
  //Stores tours in 2D array
  for (i = 0; i &lt;= col; i++) {
      tour[*count][i] = tour[0][i];
  }
  //Finds best routes
  if (tour[*count][col] &lt; Brute-&gt;eliteOne[col])
      for (i = 0; i &lt;= col; i++) Brute-&gt;eliteOne[i] = tour[*count][i];
  else if (tour[*count][col] &lt; Brute-&gt;eliteTwo[col])
      for (i = 0; i &lt;= col; i++) Brute-&gt;eliteTwo[i] = tour[*count][i];
  *count += 1;
  }
  
  int CreateGenetic(int cityStart, int cityNum, double cityWeights[20][20], GA Genetic) {
  double **tour, **tourMut;
  int i, w, x, y, z, a;
  tour = (double **) malloc((Genetic-&gt;runs + 1) * sizeof (double*));
  for (x = 0; x &lt;= Genetic-&gt;runs; x++)
      tour[x] = (double *) malloc(cityNum * sizeof (double));
  for (i = 0; i &lt; cityNum; i++) {
      tour[0][i] = i;
  }
  tourMut = malloc(Genetic-&gt;runs * sizeof (double*));
  for (x = 0; x &lt; Genetic-&gt;runs; x++)
      tourMut[x] = malloc(cityNum * sizeof (double));
  for (i = 0; i &lt; cityNum; i++) {
      tourMut[0][i] = i;
  }
  tour[0][0] = (cityStart - 1) * 100;
  tour[0][cityStart - 1] = 0;
  Genetic-&gt;size = cityNum;
  Genetic-&gt;elite[0][cityNum] = 9999;
  Genetic-&gt;elite[1][cityNum] = 9999;
  int countPerm = 1, countMut = 1, *countPP, *countPG;
  countPP = &amp;countPerm;
  countPG = &amp;countMut;
  for (i = 0; i &lt; Genetic-&gt;generations; i++) {
          for (y = 0; y &lt; Genetic-&gt;permutation; y++)
              IterativePerm(Genetic-&gt;runs, cityNum, countPP, cityNum, tour, cityWeights, Genetic);
  
          for (a=0; a &lt;= cityNum; a++) tourMut[0][a] = Genetic-&gt;elite[0][a];
              tourMut[0][0] *= 100;
          for (z = 0; z &lt; Genetic-&gt;mutation; z++){
              IterativePerm(Genetic-&gt;runs, cityNum, countPG, cityNum, tourMut, cityWeights, Genetic);
      }
          while(countMut &gt; 1){
          for(w=0; w&lt;=cityNum; w++) {
              tour[countPerm][w] = tourMut[countMut-1][w];}
              countPerm += 1;
              countMut -= 1;
          }
          countPerm = 1;
          countMut = 1;
  }
      fprintf(stderr, "Genetic Results\n");
  fprintf(stderr, "eliteOne is : \n");
  for (i = 0; i &lt;= cityNum; i++) fprintf(stderr, "%lf ", Genetic-&gt;elite[0][i]);
  fprintf(stderr, "\n");
  fprintf(stderr, "eliteTwo is : \n");
  for (i = 0; i &lt;= cityNum; i++) fprintf(stderr, "%lf ", Genetic-&gt;elite[1][i]);
  fprintf(stderr, "\n");
  
  }
  
  int GeneticWeight(int row, int col, int *count, double **tour, double cityWeights[20][20], GA Genetic) {
  tour[0][0] /= 100;
  double weight;
  int i, xOne, xTwo;
  weight = 0;
  //Calculates weight of tour
  for (i = 0; i &lt; (col - 1); i++) {
      xOne = tour[0][i];
      xTwo = tour[0][i + 1];
      weight += cityWeights[xOne][xTwo];
  }
  xOne = tour[0][col - 1];
  xTwo = tour[0][0];
  weight += cityWeights[xOne][xTwo];
  tour[0][col] = weight;
  if (*count == row) return;
  //Stores tours in 2D array
  for (i = 0; i &lt;= col; i++) {
      tour[*count][i] = tour[0][i];
  }
  //Finds best routes
  if (tour[*count][col] &lt; Genetic-&gt;elite[0][col])
      for (i = 0; i &lt;= col; i++) Genetic-&gt;elite[0][i] = tour[*count][i];
  else if (tour[*count][col] &lt; Genetic-&gt;elite[1][col])
      for (i = 0; i &lt;= col; i++) Genetic-&gt;elite[1][i] = tour[*count][i];
  *count += 1;
  tour[0][0] *= 100;
  }
  
  int IterativePerm(int row, int col, int *count, int n, double **s, double cityWeights[20][20], GA Genetic) {
  int nf = n, i, m, k, p, q;
  nf = Fact(nf);
  double temp;
  for (i = 1; i &lt; nf; i++) {
      m = n - 2;
      while (s[0][m] &gt; s[0][m + 1]) m--;
      k = n - 1;
      while (m &lt; 0)m++;
      while (s[0][m] &gt; s[0][k]) k--;
      temp = s[0][m];
      s[0][m] = s[0][k];
      s[0][k] = temp;
      p = m + 1;
      q = n - 1;
  
      while (p &lt; q) {
          temp = s[0][p];
          s[0][p] = s[0][q];
          s[0][q] = temp;
          p++;
          q--;
      }
  }
  GeneticWeight(row, col, count, s, cityWeights, Genetic);
  }
  </code></pre>
  
  <p>The inputs I ran are as follow:</p>
  
  <p>9</p>
  
  <p>8</p>
  
  <p>12</p>
  
  <p>280</p>
  
  <p>37</p>
  
  <p>The program is crashing after the set of numbers for eliteTwo is printed.</p>
  
  <p>EDIT: Forgot to mention, program must be run with a .dat file containing number, i uploaded it here <a href="http://www.mediafire.com/download.php?scc4pi71trik48e" rel="nofollow">http://www.mediafire.com/download.php?scc4pi71trik48e</a>. (gcc name.c cityweights.dat) I don't know the rules regarding posting links to files so hopefully I'm not breaking any rules by doing so.</p>