编译C程序时出现总线错误:10

编译C程序时出现总线错误:10,c,pointers,segmentation-fault,int,bus,C,Pointers,Segmentation Fault,Int,Bus,我正在做一个类项目来解决流浪推销员问题,这基本上就是TSP,除非你不返回源代码。我采用的方法是找到一组顶点的所有可能排列,然后迭代计算长度并比较它们以找到最小的排列。我知道这不是最好的方法,但这是我们教授想要的 当我运行下面的代码时,它工作得很好,当输入数组小于7 X 7时,它会给我正确的答案。但当其为7x7时,返回总线错误:10,如果大小为12x12,则返回分段错误:11。我查了几个小时这些问题,都不知道出了什么问题。我不是C编程方面的专家,老实说,我对指针很困惑。非常感谢你的帮助,我非常感激

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

当我运行下面的代码时,它工作得很好,当输入数组小于7 X 7时,它会给我正确的答案。但当其为7x7时,返回总线错误:10,如果大小为12x12,则返回分段错误: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;
   }

你的代码乱七八糟,几乎无法阅读。然而,我把它作为一个挑战,看看我是否能发现你的错误。我看到的是,您在顶部声明了一个空数组:

int a[];
然后写入此数组:

a[x] = v[j];
x++;
您甚至从未在任何地方将x重置回0。所以基本上,从一开始你就在写未分配的内存,但是你的输入集越大,你写的未分配的内存就越多


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

我觉得你的标题不对,你在编译或运行程序时出错了?另外,你的程序看起来很糟糕,请在这里发布之前正确缩进你的代码。不要为凌乱的代码感到抱歉,清理它,这将帮助您自己理解它。当您的代码无法编译时,很难获得总线错误。int**realArrayPointer=v;存在类型不匹配错误;。那是因为int**不是int[N][M]的同义词,无论你用什么方法认为它们是错的。您还至少有一条内存泄漏线98,99,答案=。。。我只能假设其他的错误,但我不再考虑这些了。“总线错误”听起来并不是特别面向软件的。非常感谢您的努力。我再次为混乱的代码道歉。只是项目将在周一到期,我只是专注于代码工作,而不是美学。下一次我在这里发帖时,一定要确保代码看起来更干净。再次感谢你!