linux - 段错误(核心转储)代码显示此错误

标签 linux shell ubuntu multidimensional-array fault

我一直在尝试使用线程来乘以多维数组。虽然代码正确执行,但我不断收到此特定错误

#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;
}

上述代码在 Ubuntu 上可以完美编译,但问题在于段错误。每次我尝试执行代码时,它都会一遍又一遍地给出相同的错误,尽管从我读到的内容来看,代码应该执行得很好。 我是 Linux 新手,非常感谢任何帮助。

最佳答案

首先,您使用=进行比较,如下:

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

应该是:

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

此外,这是错误的:

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

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

v 不是一个指针,如 k 和许多其他错误...

关于linux - 段错误(核心转储)代码显示此错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30037191/

相关文章:

linux - 如何使用 Red Hat Linux 上的标准工具随机化文件中的行?

java - 使用 jmap 查找当前堆大小

shell - 如何使用端口号删除Docker容器

bash - 在 bash shell 中的文件顶部添加一行新文本

php - 如何使用 php 找出已安装的 linux 发行版?

私有(private) Linux 主机上的 Node.js 应用程序

c - 在 X11 上移动鼠标光标无法按预期工作

bash 对列进行排序但不对相同的列进行排序

linux - EC2 实例, "General error mounting filesystems"

linux - 如何永久授予用户 sudo 访问权限?