简体   繁体   English

Can't pull my struct data to the main() function(C语言)

[英]Can't pull my struct data to the main() function (C language)

I'm writing Matrix library, I have MatrixMultiplication(Mat a, Mat b) function that returns Mat我正在编写 Matrix 库,我有 MatrixMultiplication(Mat a, Mat b) function 返回 Mat

inside the function everything is working correctly but when I'm pulling it to the main all the calculation disappears, although I'm using pointers.在 function 内部,一切正常,但是当我将它拉到 main 时,所有计算都消失了,尽管我使用的是指针。

my Mat struct is using pointer to a 1d array and n x k values for sizing.我的Mat 结构使用指向一维数组的指针和n x k值来调整大小。


INPUT:输入:


#include <stdio.h>
#include <stdlib.h>
#include <time.h> 
#define size 100
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////       ALL THIS FUNCTIONS WORK PERFECTLY       //////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int RandomRange(int l,int u){int a = (rand()%(u-l+1))+l;return a;}
void ArrayRandomizer(int *a ,int s,int l,int u){for(int i = 0; i < s; i++){
    int random = RandomRange(l,u);int *ptr2 = &random;Swap(a+i,ptr2);}}
typedef struct Mat {int * arr, rows, cols;} Mat;
Mat MatrixBuilder (int *a,int r,int c){Mat new;new.arr = a;new.rows = r;new.cols = c;return new;}
void PrintMatrix (Mat a){
    printf("\nMatrix %dx%d :\n",a.rows,a.cols);
    for (int i = 1, j = 0; i <= a.rows; i++ ,j = j + a.cols ){
    for (; j < (a.cols*a.rows); j++){
    if ((j+i)%a.cols == 1){printf(" [");}
    printf("%d ,",*(a.arr+(j+i-1)));
    if ((j+i)%a.cols == 0){printf("]\n");}}}}
int MatrixValueOfCell (Mat a,int r,int c){
    if (r > a.rows-1){printf("\n*****MatrixValueOfCell ROW IS OUT OF THE MATRIX , USE SMALLER INTEGER");return -9999999;}
    if (c > a.cols-1){printf("\n*****MatrixValueOfCell COL IS OUT OF THE MATRIX , USE SMALLER INTEGER");return -9999999;}
    if (r < 0){printf("\n*****MatrixValueOfCell CAN'T USE NEGATIVE ROW INDEX , USE BIGGER INTEGER");return -9999999;}
    if (c < 0){printf("\n*****MatrixValueOfCell CAN'T USE NEGATIVE COL INDEX , USE BIGGER INTEGER");return -9999999;}
    return *(a.arr+((r)*a.cols)+(c));}
void setMatrixValueOfCell (int s,Mat a,int r,int c){
    if (r > a.rows-1){printf("\n*****MatrixValueOfCell ROW IS OUT OF THE MATRIX , USE SMALLER INTEGER");}
    else if (c > a.cols-1){printf("\n*****MatrixValueOfCell COL IS OUT OF THE MATRIX , USE SMALLER INTEGER");}
    else if (r < 0){printf("\n*****MatrixValueOfCell CAN'T USE NEGATIVE ROW INDEX , USE BIGGER INTEGER");}
    else if (c < 0){printf("\n*****MatrixValueOfCell CAN'T USE NEGATIVE COL INDEX , USE BIGGER INTEGER");}
    else {*(a.arr+((r)*a.cols)+(c)) = s ;}}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Mat MatrixMultiplication (Mat a,Mat b){
int q [a.rows*b.cols]; int * ptr = q;
ArrayRandomizer(ptr,a.rows*b.cols,0,0);
Mat c = MatrixBuilder (ptr,a.rows,b.cols);
for (int i = 0; i < a.rows; i++){
    for (int j = 0; j < b.cols; j++){
        for (int k = 0; k < a.cols; k++){
            setMatrixValueOfCell((MatrixValueOfCell(c,i,j)+(MatrixValueOfCell(a,i,k)*MatrixValueOfCell(b,k,j))),c,i,j);}}}

printf("\n________________________________\nDirect From Function :");
PrintMatrix(c);
printf("________________________________\n");
return c;

}



int main (void){

int a[size];ArrayRandomizer(a,size,0,46);int * ptr = a;
Mat test = MatrixBuilder(ptr , 4 , 3);

int b[size];ArrayRandomizer(b,size,0,9);int * ptr2 = b;
Mat test2 = MatrixBuilder(ptr2 , 3 , 4);

PrintMatrix(test);
PrintMatrix(test2);

Mat Result = MatrixMultiplication(test,test2);

PrintMatrix(Result);

}




OUTPUT: OUTPUT:


Matrix 4x3 :
    [38 ,4 ,18 ,]
    [23 ,17 ,26 ,]
    [16 ,3 ,28 ,]
    [28 ,10 ,17 ,]

Matrix 3x4 :
    [5 ,0 ,4 ,8 ,]
    [7 ,1 ,7 ,2 ,]
    [7 ,2 ,2 ,6 ,]

________________________________
Direct From Function :
Matrix 4x4 :
    [344 ,40 ,216 ,420 ,]
    [416 ,69 ,263 ,374 ,]
    [297 ,59 ,141 ,302 ,]
    [329 ,44 ,216 ,346 ,]
________________________________

Matrix 4x4 :
    [1 ,0 ,1 ,0 ,]
    [2 ,0 ,3 ,0 ,]
    [297 ,59 ,10 ,0 ,]
    [1564489376 ,32593 ,1979723792 ,21998 ,]



I also tried returning a pointer instead of Mat struct, and then made a new Matrix using this pointer and it didn't work, I would like to know how this could be fixed without allocating dynamic memory with functions like malloc() calloc() thanks:)我还尝试返回一个指针而不是 Mat 结构,然后使用这个指针创建一个新的 Matrix 但它不起作用,我想知道如何在不使用 malloc() calloc() 等函数分配动态 memory 的情况下解决这个问题谢谢:)

by the way I'm using Ubuntu on virtual machine if this connected somehow顺便说一句,如果以某种方式连接,我在虚拟机上使用 Ubuntu

In MatrixMultiplication function, the array q is allocated on the stack.MatrixMultiplication function 中,数组q分配在堆栈上。 So at function exit, its memory is freed and matrix datas may be replaced with other values.所以在 function 退出时,它的 memory 被释放,矩阵数据可能被其他值替换。 The solution is to allocate memory with malloc so it is kept even outside of the function.解决方案是将 memory 分配给malloc ,这样它甚至可以保留在 function 之外。

From your code, I just removed the variable q and replace ptr with a memory allocation:从您的代码中,我刚刚删除了变量q并将ptr替换为 memory 分配:

Mat MatrixMultiplication (Mat a,Mat b){
int * ptr = malloc(a.rows * b.cols * sizeof(*ptr));
ArrayRandomizer(ptr,a.rows*b.cols,0,0);
Mat c = MatrixBuilder (ptr,a.rows,b.cols);
for (int i = 0; i < a.rows; i++){
    for (int j = 0; j < b.cols; j++){
        for (int k = 0; k < a.cols; k++){
            setMatrixValueOfCell((MatrixValueOfCell(c,i,j)+(MatrixValueOfCell(a,i,k)*MatrixValueOfCell(b,k,j))),c,i,j);}}}

printf("\n________________________________\nDirect From Function :");
PrintMatrix(c);
printf("________________________________\n");
return c;

}

Beware also that the this code just to point out the problem you exposed.还要注意这段代码只是为了指出您暴露的问题。 It can't be a global solution because it leads to another problem: memory leak.它不能成为全局解决方案,因为它会导致另一个问题:memory 泄漏。

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

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