gpt4 book ai didi

c - 3 天后段错误会杀了我

转载 作者:行者123 更新时间:2023-12-03 12:47:09 26 4
gpt4 key购买 nike

<分区>

我想通过多线程将两个随机生成的矩阵相乘。当我编译并输入线程数 a 时出现段错误然后我在 GDB 上调试它显示如下错误:

    [New Thread 0x7ffff7dca700 (LWP 6328)]
[New Thread 0x7ffff75c9700 (LWP 6329)]
[New Thread 0x7ffff6dc8700 (LWP 6330)]
[New Thread 0x7ffff65c7700 (LWP 6331)]
[New Thread 0x7ffff5dc6700 (LWP 6332)]
[New Thread 0x7ffff55c5700 (LWP 6333)]
[New Thread 0x7ffff4dc4700 (LWP 6334)]
[New Thread 0x7ffff45c3700 (LWP 6335)]
[New Thread 0x7ffff3dc2700 (LWP 6336)]
[New Thread 0x7ffff35c1700 (LWP 6337)]
[New Thread 0x7ffff2dc0700 (LWP 6338)]
[New Thread 0x7ffff25bf700 (LWP 6339)]
[New Thread 0x7ffff1dbe700 (LWP 6340)]
[New Thread 0x7ffff15bd700 (LWP 6341)]
[New Thread 0x7ffff0dbc700 (LWP 6342)]
[New Thread 0x7ffff05bb700 (LWP 6343)]
[New Thread 0x7fffefdba700 (LWP 6344)]
[New Thread 0x7fffef5b9700 (LWP 6345)]
[New Thread 0x7fffeedb8700 (LWP 6346)]
[New Thread 0x7fffee5b7700 (LWP 6347)]
[New Thread 0x7fffeddb6700 (LWP 6348)]
[New Thread 0x7fffed5b5700 (LWP 6349)]
[New Thread 0x7fffecdb4700 (LWP 6350)]
[New Thread 0x7fffec5b3700 (LWP 6351)]
[New Thread 0x7fffebdb2700 (LWP 6352)]
[Thread 0x7fffee5b7700 (LWP 6347) exited]
[Thread 0x7fffecdb4700 (LWP 6350) exited]
[Thread 0x7fffed5b5700 (LWP 6349) exited]
[Thread 0x7fffec5b3700 (LWP 6351) exited]
[Thread 0x7fffeddb6700 (LWP 6348) exited]
[Thread 0x7fffeedb8700 (LWP 6346) exited]
[Thread 0x7fffef5b9700 (LWP 6345) exited]
[Thread 0x7fffefdba700 (LWP 6344) exited]
[Thread 0x7ffff05bb700 (LWP 6343) exited]
[Thread 0x7fffebdb2700 (LWP 6352) exited]
[Thread 0x7ffff0dbc700 (LWP 6342) exited]
[Thread 0x7ffff15bd700 (LWP 6341) exited]
[Thread 0x7ffff1dbe700 (LWP 6340) exited]
[Thread 0x7ffff25bf700 (LWP 6339) exited]
[Thread 0x7ffff2dc0700 (LWP 6338) exited]
[Thread 0x7ffff35c1700 (LWP 6337) exited]
[Thread 0x7ffff3dc2700 (LWP 6336) exited]
[Thread 0x7ffff45c3700 (LWP 6335) exited]
[Thread 0x7ffff4dc4700 (LWP 6334) exited]
[Thread 0x7ffff55c5700 (LWP 6333) exited]
[Thread 0x7ffff5dc6700 (LWP 6332) exited]
[Thread 0x7ffff65c7700 (LWP 6331) exited]
[Thread 0x7ffff6dc8700 (LWP 6330) exited]
[Thread 0x7ffff75c9700 (LWP 6329) exited]
[Thread 0x7ffff7dca700 (LWP 6328) exited]

Thread 1 "a.out" received signal SIGSEGV, Segmentation fault.
__GI___libc_free (mem=0xf) at malloc.c:3102
3102 malloc.c: No such file or directory.

我研究了段错误,发现它在分配问题时发生。所以,对于这个你有什么想法?我应该使用 malloc 作为线程号吗?代码在这里:


#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>

#define MAT_SIZE 10
#define MAX_THREADS 100


int i,j,k,thread_number; //Parameters For Rows And Columns
int matrix1[MAT_SIZE][MAT_SIZE]; //First Matrix
int matrix2[MAT_SIZE][MAT_SIZE]; //Second Matrix
int result [MAT_SIZE][MAT_SIZE]; //Multiplied Matrix

//Type Defining For Passing Function Argumnents
typedef struct parameters {
int x,y;
}args;

int random_num() {
return rand() % 10;
}

//Function For Calculate Each Element in Result Matrix Used By Threads - - -//
void* mult(void* arg){

args* p = arg;

//Calculating Each Element in Result Matrix Using Passed Arguments
for(int a=0;a<j;a++){
result[p->x][p->y] += matrix1[p->x][a]*matrix2[a][p->y];
}
sleep(3);

//End Of Thread
pthread_exit(0);
}


int main(){

//Initializing All Defined Matrices By Zero - - - - - - - - - - - - - - -//
for(int x=0;x<10;x++){
for(int y=0;y<10;y++){
matrix1[x][y] = 0;
matrix2[x][y] = 0;
result[x][y] = 0;
}
}


//Getting Matrix1 And Matrix2 Info from User - - - - - - - - - - - - - - -//

printf(" --- Defining Matrix 1 ---\n\n");

// Getting Row And Column(Same As Row In Matrix2) Number For Matrix1
printf("Enter number of rows for matrix 1: ");
scanf("%d",&i);
printf("Enter number of columns for matrix 1: ");
scanf("%d",&j);

printf("\n --- Initializing Matrix 1 ---\n\n");
for(int x=0;x<i;x++){
for(int y=0;y<j;y++){
matrix1[x][y]=random_num();
}
}

printf("\n --- Defining Matrix 2 ---\n\n");

// Getting Column Number For Matrix2
printf("Number of rows for matrix 2 :");
scanf("%d",&j);
printf("Enter number of columns for matrix 2: ");
scanf("%d",&k);

printf("\n --- Initializing Matrix 2 ---\n\n");
for(int x=0;x<j;x++){
for(int y=0;y<k;y++){
matrix2[x][y]=random_num();
}
}


//Printing Matrices - - - - - - - - - - - - - - - - - - - - - - - - - - -//

printf("\n --- Matrix 1 ---\n\n");
for(int x=0;x<i;x++){
for(int y=0;y<j;y++){
printf("%5d",matrix1[x][y]);
}
printf("\n\n");
}

```

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com