繁体   English   中英

在C中打印矩阵时获取垃圾值

[英]Getting a garbage value while printing matrix in C

三年前,我学习了C编程语言,而现在,根据Java和C#的经验,当我重新使用它时,我遇到了一些指针问题。 所以我试图编写一个简单的矩阵加法程序,但我不知道为什么在打印矩阵时为什么会得到一些奇怪的值。

码:

#include <stdio.h>

int* sumOfMat(int* m1,int* m2)
{
    printf("Matrix A: \n");
    printMat(m1);
    printf("Matrix B: \n");
    printMat(m2);
    int mat3[3][3];
    int row=0,col=0,k=0,sum=0;
    for(;row<3;row++)
    {
        col=0;
        for (;col<3 ;col++ )
        {
            sum=(*m1+*m2);
            m1++;
            m2++;
            mat3[row][col]=sum;
        }
    }

    printf("Result: \n");
//    printMat(mat3);  //this statement is giving me a correct output.
    return mat3;
}

void printMat(const int m[3][3])
{
    int row,col;
    for (row=0;row<3 ;row++ )
    {
        for (col=0;col<3 ;col++ )
        {
            printf("%d\t",m[row][col]);
        }
        printf("\n");
    }

}


int main(void) {
int mat1[3][3]={{1,2,3},{4,5,6},{7,8,9}};
int mat2[3][3]={{1,2,3},{4,5,6},{7,8,9}};
//add
printf("Sum of the metrices : \n");
int* x=sumOfMat(&mat1,&mat2);
printMat(x);   // this call is providing me some garbage values at some locations.
return 0;
}

输出:

Success  time: 0 memory: 2292 signal:0
Sum of the metrices : 
Matrix A: 
1   2   3   
4   5   6   
7   8   9   
Matrix B: 
1   2   3   
4   5   6   
7   8   9   
Result: 
2   134514448   134514448   
8   10  12  
14  16  -1216458764 

演示版

问题:为什么我会收到此错误以及如何纠正它。

线

int mat3[3][3];

堆栈上分配2D数组

您正在返回一个指向该数组的指针。

return mat3;

不幸的是,当函数调用结束时,堆栈未展开,并且数组的内存不再存在,因此您有一个指向垃圾的指针。

一种解决方案是在main函数中分配数组,并将其作为参数传递给sumOfMat

永远不要返回指向自动变量的指针:

int *f(void)
{
    int i;
    ....
    return &i;
}

一旦f返回,变量i就不存在,因此指向它的指针将无效。
在您的情况下, mat3是一个自动变量, sumOfMat()返回一个指向mat3的指针,一旦sumOfMat()返回,该指针将不存在。
解决问题的方法之一是将mat3定义为全局变量。

我进一步指出了您的代码的一个主要问题:

的返回类型sumOfMat(int* m1,int* m2)是指向整数( int * ),而要返回mat3它的类型的int(*)[3]
编译您的代码时,我收到很多警告。 我纠正了大多数。
修改后的代码:

#include <stdio.h>
void printMat(int *m);
int* sumOfMat(int* m1,int* m2);
int mat3[3][3];

int* sumOfMat(int* m1,int* m2)
{
    printf("Matrix A: \n");
    printMat(m1);
    printf("Matrix B: \n");
    printMat(m2);
    //int mat3[3][3];
    int row=0,col=0,sum=0;
    for(;row<3;row++)
    {
        col=0;
        for (;col<3 ;col++ )
        {
            sum=(*m1+*m2);
            m1++;
            m2++;
            mat3[row][col]=sum;
        }
    }

   printf("Result: \n");
   //    printMat(mat3);  //this statement is giving me a correct output.
   return mat3[0];
}

void printMat(int *m)
{
    int row;
    for (row=1;row<=9 ;row++)
        {
            printf("%d\t",*m++);
            if(row%3 == 0)
                printf("\n");
        }
   /* {
        for (col=0;col<3 ;col++ )
        {
            printf("%d\t",m[row][col]);
        }
        printf("\n");
    }*/

}


int main(void) {
int mat1[3][3]={{1,2,3},{4,5,6},{7,8,9}};
int mat2[3][3]={{1,2,3},{4,5,6},{7,8,9}};
//add
printf("Sum of the metrices : \n");
int* x=sumOfMat(&mat1[0][0],&mat2[0][0]);
printMat(x);   // this call is providing me some garbage values at some locations.
return 0;
}  

这是因为您要返回的mat3是定义为

int mat3[3][3];

要解决此问题, mat3使用malloc动态分配mat3或将其作为out variable传递给函数sumOfMat作为第三个变量。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM