首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >动态内存分配嵌套for循环中的分段错误

动态内存分配嵌套for循环中的分段错误
EN

Stack Overflow用户
提问于 2018-06-04 01:10:01
回答 1查看 179关注 0票数 0

我正在准备使用C语言模拟距离矢量路由的代码,然而,我在运行时遇到了分段故障。代码:-

代码语言:javascript
复制
#include <stdio.h>
#include <math.h>
#include <stdlib.h>

/* Date : 03/06/2018
 *
 * Algorithm
 *
 * 1. get number of nodes from user
 * 2. dynamic alloc new matrix nxn
 * 3. create distance vector matrix, if dist > 1000 consider inf
 * _|   A   B   C   D   E   F
 * A|   0   5   2   3   i   i
 * B|   5   0   4   5   
 * C|
 * D|
 * E|
 * F|
 * ---------------
 *
 * 4. create new routing matrix of nxn
 * 5. create new minimizing array for the node
 * 6. find minimum of the array, allocate new value
 *
 * copyleft
 */

#define inf 1000

int min_r(int*, int*, int);
void dvr(int**, int**, char**, int);
void dvtDisp(int**, int);
void dvtDispNew(int **, char**, int);

int main(){
    int n;                              //No of nodes
    int i,j;                            //Counters

    printf("> enter the number of nodes in the network... ");
    scanf("%d",&n);

    int **DisMat = (int **)malloc(n * n * sizeof(int));     //Dynamic allocation of Distance Matrix

    for(i=0; i<n; i++){                     // x directional loop
        printf("> distance vector table for node %c\n",i+65);
        for(j=0; j<n; j++){                 // y directional loop
            printf("> distance from %c... ",j+65);
            if(j==i) { DisMat[i][j] = 0; printf("0");}
            else scanf("%d",&DisMat[i][j]);
        }// y directional loop
    }// x directional loop

    int **NewDisMat = (int **)malloc(n * n * sizeof(int));      //New Distance Matrix
    char **Hop = (char **)malloc(n * n * sizeof(char));     //New Hop Matrix

    for(i=0; i<n; i++){
        for(j=0; j<n; j++){
            Hop[i][j] = '-';                //All Hops Nullified
        }
    }

    dvr(DisMat, NewDisMat, Hop, n);                 //Distance Vector Routing

    return 0;
}//main

void dvr(int *dvt[], int *newdvt[], char *hopper[], int l){     //DVR function
    int x=0, y=0, z=0, conCount;
    int hopPoint;
    int *mini = (int *)malloc((l-1) * sizeof(int));
    int *mzer = (int *)malloc((l-1) * sizeof(int));

    for(x=0; x<l; x++){                     // x directional propagation
        mini[0] = x;
        z = 1; conCount=0;
        do{
            if((dvt[x][y] < inf) && (y != x)) {
                mini[z] = y;
                z++;
                conCount++;
            }
            y++;
        }while(y<l);

        y = 0; z = 0;

        for(y = 0; y<l; y++){
            while(z<conCount){
                mzer[z] = dvt[mini[z]][y];
                z++;
            }

            newdvt[x][y] = min_r(mzer, &hopPoint, conCount);
            hopper[x][y] = hopPoint + 65;
        }// y directional propagation
    }// x directional propagation
}//dvr

int min_r(int arr[], int *index, int len){
    //Sequential minimum search
    int min;
    int ind = 0;

    min = arr[ind];
    for(ind = 0; ind<len; ind++){
        if(arr[ind] < min){
            min = arr[ind];
            *index = ind;
        }
    }

    return min;
}//min_r

void dvtDisp(int *dvt[], int size){
    int x, y;
    printf("_ |");

    for(x = 0; x<size; x++){
        printf("\t%c",65 + x);
    }
    printf("\n");

    for(y = 0; y<size; y++){
        printf("%c |",y + 65);

        for(x = 0; x < size; x++)
            printf("\t%d",dvt[x][y]);
    }
}

void dvtDispNew(int *dvt[], char *hopto[], int size){
    int x, y;
    printf("_ |");

    for(x = 0; x<size; x++){
        printf("\t%c\thop",65 + x);
    }
    printf("\n");

    for(y = 0; y<size; y++){
        printf("%c |",y + 65);

        for(x = 0; x < size; x++)
            printf("\t%d\t%c",dvt[x][y],hopto[x][y]);
    }
}

在执行过程中,我在终端上得到了以下输出。

代码语言:javascript
复制
anwesh@bionic-Inspiron:~/Documents/NS2/LAB/prog5$ gcc main.c
anwesh@bionic-Inspiron:~/Documents/NS2/LAB/prog5$ ./a.out
> enter the number of nodes in the network... 5
> distance vector table for node A
Segmentation fault (core dumped)

我试图在gdb上运行它,但无法弄清楚结果是什么。下面是gdb的输出:

代码语言:javascript
复制
Starting program: /home/anwesh/Documents/NS2/LAB/prog5/a.out
> enter the number of nodes in the network... 5
> distance vector table for node A

Program received signal SIGSEGV, Segmentation fault.
0x000055555555487e in main ()
(gdb)

最初我认为这是一个与动态内存分配有关的问题,但我不知道确切的原因。我已经检查了代码多次,看看是否有任何幼稚的错误,但我不能。

请帮帮我!提前谢谢。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50668947

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档