分段故障(核心转储)代码显示此错误

时间:2020-12-15 21:38:30

I've been trying to multiply Multidimensional arrays using Threads. While the code executes properly, I keep getting this particular error

我一直在尝试使用Threads来乘法多维数组。代码执行正常时,我不断收到此特定错误

#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<malloc.h>
#include<unistd.h>
#include<fcntl.h>
#include<errno.h>
#define R_FLAGS O_RDONLY

int arraySize;
int **oarray;
int **resultarray; /*the two multidimensional arrays declared*/

void *multiply(void *arg);  
/*The function used to showcase Mutliplication*/

int** DynamicAllocate(int Size) {
    int i;
    int **array;
    array = malloc(Size * sizeof(int *));
    if(array = NULL){
        fprintf(stderr,"No Memory\n");
        return;
    }

    for(i=0;i<Size;i++){
        array[i] = malloc(Size * sizeof(int));
        /*using the same no of rows for the columns as instructed*/
        if(array[i] == NULL){
            fprintf(stderr,"No Memory\n");
            return;
        }
    }

    return array;
}

void *multiply(void *arg) {
    int i,j,*k,v,Sum=0;

    k=(int *)arg;
    v=(int)k;

    for(i=0;i<arraySize;i++){
        for(j=0;j<arraySize;j++){
            Sum= Sum + oarray[v][j]*oarray[j][i];
        }       

        Sum = resultarray[v][i];
        Sum=0;
    }
}

int main(int argc, char *argv[]){
    int error,*join;
    char nrows,*intarray;
    int row,i,j,value=0;
    pthread_t tid;

    if((error = open("File",O_RDONLY))==-1){
        perror("Unable to Open File");
    }

    read(error,&nrows,1);
    row = atoi(&nrows);
    arraySize = row;
    oarray = DynamicAllocate(row);
    resultarray = DynamicAllocate(row);

    intarray = malloc(1024);   /*read until given file ends*/
    read(error,intarray,1024);

    printf("****Assignment # 3****\n");
    printf("****By:Hammad Faisal****\n");
    printf("****DDP-SP12-BCS-026****\n\n");

    /*temporary array that is being used for storage and multiplication*/

    for(i=0;i<row;i++){
        for(j=0;j<row;j++){
            oarray[i][j]=atoi(&intarray[value]);
            value+=2;
        }
    }

    /*using for loop to attach thread to each row allocated*/

    for(i=0;i<row;i++){
        join = (int *)i;
        pthread_create(&tid,NULL,multiply,join);
        pthread_join(tid,NULL);
    }

    /*display the resultant matrix*/

    for(i=0;i<row;i++){
        for(j=0;j<row;j++){
            printf("%d\t",resultarray[i][j]);
        }
        printf("\n");
    }
    return 0;
}

The above mentioned code compiles perfectly on Ubuntu but the Segmentation fault is the problem. Every time I try to execute the code it gives the same error over and over again although from what I have read the code should execute fine. I'm new Linux and I would greatly appreciate any help.

上面提到的代码在Ubuntu上完美编译,但Segmentation错误是问题所在。每次我尝试执行代码时,它会一遍又一遍地给出相同的错误,尽管从我读过的代码应该执行得很好。我是新Linux,我非常感谢任何帮助。

1 个解决方案

#1


First of all, you are using = to compare, as:

首先,您使用=来比较,如:

if(array = NULL){
    fprintf(stderr,"No Memory\n");
    return;
}

It should be:

它应该是:

if(array == NULL){
    fprintf(stderr,"No Memory\n");
    return;
}

Also, this is wrong:

这也是错的:

int i,j,*k,v,Sum=0;

k=(int *)arg;
v=(int)k;

v is not a pointer as k and many other errors...

v不是指针作为k和许多其他错误......

#1


First of all, you are using = to compare, as:

首先,您使用=来比较,如:

if(array = NULL){
    fprintf(stderr,"No Memory\n");
    return;
}

It should be:

它应该是:

if(array == NULL){
    fprintf(stderr,"No Memory\n");
    return;
}

Also, this is wrong:

这也是错的:

int i,j,*k,v,Sum=0;

k=(int *)arg;
v=(int)k;

v is not a pointer as k and many other errors...

v不是指针作为k和许多其他错误......