【问题标题】:Memory runtime error with 2D array in CC中二维数组的内存运行时错误
【发布时间】:2014-04-10 16:02:19
【问题描述】:

我正在尝试在 C 中实现 Dijkstra 算法,并且正在尝试将二维数组传递给函数。我尝试使用 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];
}

【问题讨论】:

  • 请显示更多代码,具体来说,调用 this 以及传入变量的声明。这还不够上下文。
  • 你能做一个独立的工作示例,以便我们运行它吗?
  • 我刚刚编辑了我所有的代码
  • 该函数最后取矩阵,大小倒数第二,但是你用大小和矩阵来调用它。这甚至可以编译吗?
  • 顺便说一句:这是“顶点”,而不是“顶点”。

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


【解决方案1】:

该函数最后获取矩阵,大小倒数第二个,但您以相反的方式使用大小和矩阵调用它。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-26
    • 1970-01-01
    • 2011-10-04
    • 2015-10-02
    • 2012-02-05
    • 1970-01-01
    • 2020-06-15
    相关资源
    最近更新 更多