BUS错误:10编译C程序时

时间:2014-12-06 22:47:36

标签: c pointers segmentation-fault int bus

我正在研究一个类项目来解决流浪的Salesman问题,这个问题基本上就是TSP,除非你没有回到源头。我采用了找到一组顶点的所有可能排列的方法,然后迭代地计算长度并比较它们以找到最小的顶点。我知道这不是最好的方法,而是我们教授想要的方法。

当我运行下面的代码时,它工作正常并在输入数组小于7 X 7时给出正确答案。但是当它的7 X 7时,它返回"总线错误:10" ,如果大小为12 x 12,则返回"分段错误:11"。我查了几个小时这些问题,无法弄清楚是什么问题。我不是C编程方面的专家,我老实说真的很困惑。非常感谢你的帮助,我非常感激!

哦,对于凌乱的代码感到抱歉。

#include <stdio.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include<stdlib.h>


int x = 0;
int a[];


void swap (int v[], int i, int j) {
    int     t;

    t = v[i];
    v[i] = v[j];
    v[j] = t;
 }

/* recursive function to generate permutations */
int* perm (int v[], int n, int i) {
    /* this function generates the permutations of the array
     * from element i to element n-1
     */
    int     j;

    /* if we are at the end of the array, we have one permutation
     * we can use (here we print it; you could as easily hand the
     * array off to some other function that uses it for something
     */
    if (i == n) {
            for (j=0; j<n; j++){ a[x] = v[j]; x++;} // printf ("%d ", v[j]);
    //      printf ("\n");

   }
     else
            /* recursively explore the permutations starting
             * at index i going through index n-1
             */
            for (j=i; j<n; j++) {
 /* try the array with i and j switched */

                    swap (v, i, j);
                    perm (v, n, i+1);

                    /* swap them back the way they were */

                    swap (v, i, j);
            }

                    return a;
}





int fact(int n){

if(n==1){
return 1;
 }

else{
return n * fact(n-1);
 }

 }


int findShortestPath(int **v , int length){


int pathArrayMultiplier = 0;

int ShortestPathLength = 99999;

printf("Called");

int arrayOfVertices[length-1];

for(int i=0 ; i<length-1 ; i++){


arrayOfVertices[i] = i+2;

}

int n = fact(length-1);

bool doBreak = false;

int pathArray[length-1];

//printf(" Called 3");
printf(" %d" , n);
int* Answer;
Answer = malloc(sizeof(int *));
Answer =  perm(arrayOfVertices , length-1 , 0);


printf("Called 4");

int j =-1;

for(int i=0 ; i< n*(length-1) ; i++){
doBreak = false;
j++;
printf("%d " , *(Answer + i));
pathArray[j] = *(Answer+i);



if(j == length-2)
{
j = -1;
// Check for negative values. If any value is negative, disregard path
int checklength = *((int *)v + 0 *length + (pathArray[0]-1));
if(checklength < 0){
printf("First check called");

  continue;}

for(int i =0 ; i<length-2 ; i++){
if(*((int *)v + (pathArray[i]-1) * length  + (pathArray[1 + i]-1)) < 0){
doBreak = true;
printf("Second Check called");
 break;}
  }
   if(doBreak) { pathArrayMultiplier++; continue;}


printf("\n");

 int pathLength = *((int *)v + 0 *length + (pathArray[0]-1));

  for(int i =0 ; i<length-2 ; i++){

 pathLength = pathLength + *((int *)v + (pathArray[i]-1) * length  + (pathArray[1 + i]-1));}

      printf("Path Length is %d\n" , pathLength);
     if(pathLength < ShortestPathLength) { ShortestPathLength = pathLength;}

 }

 }
 printf("\n\n Shortest Path Length is %d \n" , ShortestPathLength);
 return ShortestPathLength;
 }












 int main () {
    int len = 5;
    printf("Array is initialized");
    int     v[7][7] = {0,7,-1,10,1,-1,-1,7,0,-1,-1,10 ,-1 ,1,-1,-1,0,10,1,10,-1,10,-1,10,  0,8,-1,-1,-1,10,1,8,0,10,-1,-1,-1,10,-1,10,0,70,-1,1,-1,-1,-1, 70 , 0};
    printf("Array is initialized");
    int **realArrayPointer = v;
    findShortestPath(realArrayPointer, 7);
    return 0;
   }

1 个答案:

答案 0 :(得分:0)

你的代码很乱,几乎无法读取。但是,我把自己作为一个挑战,看看我是否能发现你的错误。我看到的是你在顶部声明一个空数组:

int a[];

然后你写这个数组:

a[x] = v[j];
x++;

你甚至不会在任何地方将x重置为0。所以基本上,从一开始你就是写入未分配的内存,但输入设置越大,你写入的内存就越多。

您的代码可能存在其他问题。我肯定看到了很多其他的东西,表明对指针的理解不正确,但它们不一定会导致致命的错误。请至少获得某种自动缩进程序,以使您的代码可读。