【问题标题】:#EMERGENCY!!! Run-Time Check Failure #2 - Stack around the variable 'b' was corrupted#紧急情况!!!运行时检查失败 #2 - 变量“b”周围的堆栈已损坏
【发布时间】:2017-07-06 20:37:54
【问题描述】:

我知道这似乎是一个老问题,但没有人回答我搜索过的问题。 我一直收到“运行时检查失败 #2 - 围绕变量 'b' 的堆栈已损坏。”当我尝试进行 [4][2]*[2][3] 矩阵乘法时。

有人发现问题了吗?

#include <stdio.h>
int main() {
    int a[4][2] = {0};
    int b[2][3] = {0};
    int c[3][3] = {0};
    int i, j;
    printf("Please enter first matrix value\n");
    for (i = 0; i < 4; i++) {
        for (j = 0; j < 2; j++) {
            printf("%d row, %d column:", i + 1, j + 1);
            scanf("%d", &a[i][j]);
        }
    }
    printf("Please enter second matrix value\n");
    for (i = 0; i < 2; i++) {
        for (j = 0; j < 3; j++) {
            printf("%d row, %d column:", i + 1, j + 1);
            scanf("%d", &b[i][j]);
        }
    }
    printf("\n the result is :\n");//
    for (i = 0; i < 4; i++) {
        printf("[");
        for (j = 0; j < 3; j++) {
            c[i][j] = (a[i][0] * b[0][j]) + (a[i][1] * b[1][j]);
            printf(" %4d ", c[i][j]);
        }
        printf("]\n");
    }
    return 0;
}

【问题讨论】:

  • ##不要喊!!!!
  • 应该是int c[4][3] = {0}

标签: c


【解决方案1】:

我没有彻底检查你的代码,但是你将 c 定义为 3x3,并且在这里

for (i = 0; i < 4; i++) {
    printf("[");
    for (j = 0; j < 3; j++) {
        c[i][j] = (a[i][0] * b[0][j]) + (a[i][1] * b[1][j]);

...你访问 c[3],它是 c 的 fourth 元素,并且不存在。这一定会写在其他地方

所以检查你的索引(正如@ptb 观察到的,c 实际上应该是四行深)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-21
    • 2013-12-13
    • 2013-12-10
    • 2015-02-09
    • 2015-05-24
    • 2021-12-31
    • 2014-10-20
    相关资源
    最近更新 更多