C中的2D数组的内存运行时错误

时间:2014-04-10 16:02:20

标签: c memory multidimensional-array runtime-error gcc-warning

我试图在C中实现Dijkstra算法,而我试图将2D数组传递给函数。我尝试用C99和C11编译,所以我编写函数的方式应该(并且确实)编译。但是,当我进行调试时,我在所有数据中扫描并跳转到我的dijkstra()功能后,在控制台中出现以下错误,此功能暂时无效。如果有人能告诉我发生了什么,我会很感激。

如果它完全相关,我使用的是GCC编译器,Eclipse Kepler和Fedora 20

warning: Range for type <error type> has invalid bounds 0..-11105

#include <stdio.h>
#define INFINITY 4294967296

void dijkstra(int root, int end, int size, int adjMatrix[size][size]);
int main(void)
{
    /*------------------------------------------------------------------------*/
    /* Variable Declaration
     /*-----------------------------------------------------------------------*/
    int i, j; /* Temporary counting variables */
    int n; /* Number of verticies */
    int distance; /* Temporary distance variable */
    int root, end; /* Root and terminating verticies */

    /*------------------------------------------------------------------------*/
    /* Data Input
     /*-----------------------------------------------------------------------*/
    /*Get number of verticies and check for erroneous input */
    printf("Please enter the number of verticies in your graph: ");
    scanf("%d", &n);
    while (n <= 0)
    {
        printf("\nERROR: value must not be less than or equal to zero! \n\n"
               "Please enter the number of verticies in your graph: ");
        scanf("%d", &n);
    }

    /*Get root node and check for erroneous input */
    printf("Please enter the root vertex: ");
    scanf("%d", &root);
    while (root > n || root <= 0)
    {
        printf("\nERROR: value must be less than or equal to the total number"
                " of\n verticies and may not be less than or equal to "
                "zero! \n\nPlease enter the root vertex: ");
        scanf("%d", &root);
    }

    /*Get terminating node and check for erroneous input */
    printf("Please enter the terminating vertex: ");
    scanf("%d", &end);
    while (end > n || end <= 0)
    {
        printf("\nERROR: value must be less than or equal to the total number"
                " of\n verticies and may not be less than or equal to "
                "zero! \n\nPlease enter the terminating vertex: ");
        scanf("%d", &end);
    }

    /* Fill adjacency matrix */
    printf("\n\nPlease enter the distance between verticies i and j below.\nIf "
            "the two verticies are disjoint, enter a negative number "
            "instead.\n");

    int adjMatrix[n][n];
    /* Scan across entire array  */
    for (i = 0; i < n; ++i)
    {
        /* Scan across only half of array; no need to ask user for the same
         * distance twice.
         */
        for (j = 0; j < i; ++j)
        {
            printf("\ndistance(%d,%d) = ", i + 1, j + 1);
            scanf("%f", &distance);
            /* Distance from i to j is the same as the distance from j to i.
             * Hence we place it in two parts of the adjacency matrix.
             */
            adjMatrix[i][j] = distance;
            adjMatrix[j][i] = distance;
        }
        /*For the case of i = j, set distance to -1 as self-loops are not
         * taken into consideration here.
         */
        adjMatrix[j][i] = -1;
    }

    dijkstra(root,end,adjMatrix,n);

    return 0;
}

void dijkstra(int root, int end, int size, int adjMatrix[size][size])
{
    int i = adjMatrix[3][1];
}

1 个答案:

答案 0 :(得分:2)

该函数最后使用矩阵,并且大小倒数,但是你用相反的大小和矩阵来调用它。

当函数使用size作为指针时,这会导致undefined behavior