【问题标题】:Data Does not stores in Structure数据不存储在结构中
【发布时间】:2011-04-17 21:08:34
【问题描述】:

您好,我必须将文本文件中的数据读取到结构中的数组中,然后启动多个线程来计算它并最终显示结果。现在我已经计算出了文件读取部分和线程部分以及显示部分.

剩下的是神秘的 BUG,当我从 MyFunctions.C 调用我的 ReadFile 函数时,它会很好地读取文件并显示良好但是如果我在同一个文件中调用相同的函数但从其他 .c 文件调用,那么它会显示垃圾values.我尝试了一切但无法追踪 BUG 以下是我的完整程序

myheader.h

struct Matrix {
    int m1[10][10];
    int row;
    int mult[10][10];
};

void *CalculateSquare(void *arguments);

Main.c

#include <stdio.h>
#include<stdlib.h>
#include <pthread.h>
#include <semaphore.h>

#include "myheader.h"

#define Row 4
#define Column 4


int main()
{
    pthread_t pth[4];

    struct Matrix args;

    int i=0,j=0,k=0;

    ReadFromFile(&args);

    printf("Matrix is :\n");
        for(i=0;i<4;i++)
    {
        for(j=0;j<4;j++)
        {
            printf("%d\t",(int)args.m1[i][j]);
        }
    printf("\n");
    }
       ///////////////////////// Create New Thread and Store its Id in an array for future Use//////////////////
    for(i=0;i<Row;i++)
    {
        args.row = i;
        pthread_create(&pth[i],NULL,CalculateSquare,(void *)&args);
    }
    ///////////////////////// Join All threads so that program waits for completion of each/////////////////    

    for(i=0;i<Row;i++)
    {
        pthread_join(pth[i],NULL);
    }
    printf("Waiting for Thread to Complete\n");

    /////////////////////// Display the Matrix ////////////       
    printf("Square of the Matrix is :\n");
        for(i=0;i<Row;i++)
        {
            for(j=0;j<Column;j++)
                printf("%d\t",args.mult[i][j]);
            printf("\n");
        }



    return 0;

}

MyFunctions.C

#include <stdio.h>
#include<stdlib.h>

#include "myheader.h"

struct Matrix ReadFromFile(struct Matrix args)
{


    int col = 4, row = 4, i, j;
                int  temp;  
    FILE *fin;
    fin=fopen("test.txt","r");
    if (!fin)
        perror("Can't open input");

    //args.array = (int **)(malloc(sizeof(int**) *row ));
    for(i=0;i<4;i++)
    {
        //args.array[i] = (int **)(malloc(sizeof(int**) * col));
        for(j=0;j<4;j++)
        {

            fscanf(fin,"%d \n",&temp);
            args.m1[i][j] = (int)temp;
        }
    }
    fclose(fin);

for(i=0;i<4;i++)
    {
        for(j=0;j<4;j++)
        {
            printf("%d\t",(int)args.m1[i][j]);
        }
    printf("\n");
    }

return args;
}
/* This is our thread function.It takes the Row to Process  */
void *CalculateSquare(void *arguments)
{
    struct Matrix args =*((struct Matrix *) arguments);

    int rowToProcess;
    int j = 0;
    int k = 0;

    rowToProcess = (int)args.row;
    printf("Thread calculating Row Number %d\n",rowToProcess);

    for(j=0;j<4;j++)
        {
        args.mult[rowToProcess][j]=0;
            for(k=0;k<4;k++)
        {
                //args.mult[rowToProcess][j] += (int)(args.m1[rowToProcess][k]*args.m1[k][j]);      
        }
        }   
//  sleep(5);
    return NULL;
}

输出是

1   2   3   4   
5   6   7   8   
9   10  11  12  
13  14  15  16  
Matrix is :
2   2   -1  0   
0   2   1   4242343 
3   -1075858606 1   0   
4193048 -1075858606 4242341 0   
Thread calculating Row Number 1
Thread calculating Row Number 2
Thread calculating Row Number 3
Thread calculating Row Number 3
Waiting for Thread to Complete
Square of the Matrix is :
0   909653609   0   0   
0   0   0   0   
0   0   0   15868726    
15892704    134513372   -1217004872 15949812    

【问题讨论】:

    标签: c linux data-structures


    【解决方案1】:

    在你的主文件中你调用ReadFromFile(&amp;args); - 所以函数参数是一个指向结构矩阵的指针。但是你的函数本身有这个声明:

    struct Matrix ReadFromFile(struct Matrix args)
    

    这意味着它需要一个结构矩阵——而不是指向它的指针。您可能希望将此参数更改为指针。 这当然也意味着您需要将对该结构的访问权限从args.m1[i][j] = (int)temp; 更改为args-&gt;m1[i][j] = (int)temp;

    编辑:另外,如果你发布了整个头文件,你错过了 ReadFromFile 的声明。

    说实话,我想知道为什么您的代码实际上首先编译时出现了这些错误,您可能希望在编译时输出所有警告(gcc 的-Wall 标志)

    【讨论】:

    • 这实际上是对 Matrix 结构的 reference,而不是指针。
    • @Chris Thompson:我以为 C 不知道引用?
    • 这行得通,但在最后一个显示矩阵平方的地方,同样的问题存在所以应该在那里改变什么,即使它在主文件中也不起作用
    • 在引用问题上,这里 & 是 addressof 运算符,为您提供变量 args 的地址,当然可以将其分配给指针。发生引用传递,但 C++ 引用类型略有不同 - 如果您有一个函数 int func(int&amp; someref),则不能在该函数中分配 someref 任何其他地址,而 可以 将堆栈指针分配给其他地址,如果你愿意的话。
    • @Chris,你可能是对的,我更像是一个 C++ 人。无论如何,您传递的是结构的地址,而不是指向它的指针。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-03
    • 1970-01-01
    • 2010-09-14
    • 1970-01-01
    相关资源
    最近更新 更多