简体   繁体   English

为2D数组分配内存时出现分段错误

[英]Segmentation fault when allocating memory for a 2D array

This program takes arguments from the command line and displays their prime factors using threads. 该程序从命令行获取参数,并使用线程显示其主要因素。 I am using a struct to pass info between threads. 我正在使用一种结构在线程之间传递信息。 The struct has a 2D array that holds the arguments and then their factors. 该结构具有一个2D数组,其中包含参数以及它们的因子。 The way i'm allocating now allows me to access two of my arguments and the gives me a Segmentation fault (Location of Segmentation fault is noted in the code). 现在,我分配的方式使我可以访问两个参数,这给了我一个Segmentation错误(Segmentation错误的位置在代码中指出)。 What am I doing wrong? 我究竟做错了什么? All code is provided below. 下面提供了所有代码。

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

// Global variables
#define MAX_FACTORS (11)
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;

struct Buffer {
    int **factors;
    int size;
};


void *factor ( void *buffer );
void *displayFactors (void *factors);


// MAIN
int main (int argc, char* argv[]) {
    // Create Buffer
    struct Buffer *buffer;
    buffer = malloc(sizeof(struct Buffer));
    buffer->size = argc - 1;

    // Allocate memory for factors 2d array
    buffer->factors = malloc(buffer->size);

    for( int i = 0; i < buffer->size; i++) {
        buffer->factors[i] = calloc(MAX_FACTORS, sizeof(int));
    }

    pthread_mutex_lock(&mutex1);

    if(buffer->size > 0) {
        for(int i = 1; i < argc; i++){
            buffer->factors[i - 1][0] = atoi(argv[i]);
            // ---SEGMENTATION FAULT OCCURS HERE AFTER LOOPING TWICE.----
        }
    }
    else
        return 0;

    pthread_mutex_unlock(&mutex1);

    pthread_t producer;
    pthread_t consumer;

    pthread_create(&producer, NULL, factor, (void*)buffer);
    pthread_create(&consumer, NULL, displayFactors, (void*)buffer);

    pthread_join(producer, NULL);
    pthread_join(consumer, NULL);

    free(buffer->factors);

    return 0;
}


// PRODUCER
void *factor ( void *buffer ) {
    struct Buffer *prod_buffer = (struct Buffer*)buffer;

    pthread_mutex_lock(&mutex1);

    // find numbers to factor factors
    int i = 0;
    while(i < prod_buffer->size) {
        // printf("Numbers to factor: %d\n", prod_buffer->factors[i][0]);
        prod_buffer->factors[i][0] = prod_buffer->factors[i][0];
        i++;
    }

    // Factor numbers
    for(int i = 0; i < prod_buffer->size; i++) {
        int j = 1;
        int toFactor = prod_buffer->factors[i][0];
        while(toFactor % 2 == 0) {
            prod_buffer->factors[i][j] = 2;
            toFactor = toFactor / 2;
            j++;
        }
        for(int k = 3; k <= toFactor; k += 2) {
            while (toFactor % k == 0) {
                prod_buffer->factors[i][j] = k;
                toFactor /= k;
                j++;
            }
        }
    }
    pthread_mutex_unlock(&mutex1);
    return NULL;
}


// CONSUMER
void *displayFactors (void *buffer) {
    struct Buffer *cons_buffer = (struct Buffer*)buffer;

    for (int i = 0; i < cons_buffer->size; i++) {
        printf("%d: ", cons_buffer->factors[i][0]);
        for (int j = 1; j < MAX_FACTORS; j++){
            if(cons_buffer->factors[i][j] != 0) printf("%d ", cons_buffer->factors[i][j]);
        }
        printf("\n");
    }
    return NULL;

}

EDIT-- The arguments i've been testing with are 4, 13, 46, 72, 5, and 12. The result should display: 4: 2 2 13: 13 46: 2 23 72: 2 2 2 3 3 5: 5 12: 2 2 3 编辑-我正在测试的参数是4,13,46,72,5和12。结果应显示为:4:2 2 13:13 46:2 23 72:2 2 2 3 3 5: 5 12:2 2 3

Thanks for the help! 谢谢您的帮助!

I use gdb to debug your code, I found that the size of buffer->factors is 8, that is not enough. 我使用gdb调试您的代码,发现buffer->factors的大小为8,这还不够。

you should do 你应该做

buffer->factors = malloc(buffer->size*sizeof(int *));

This segmentation fault error can fix. segmentation fault错误可以修复。

Also,There is another bug in your code. 另外,您的代码中还有另一个错误。 if the consumer execute first, the output will empty, so you need use the cond to make the producer exectue first. 如果consumer首先执行,则输出将为空,因此您需要使用cond首先使producer执行。

Here some tips: 这里有一些提示:

pthread_cond_t product = PTHREAD_COND_INITIALIZER; /*global*/ 
pthread_cond_signal(&product);  /*In factor function*/

Also the displayFactors need to add a mutex . 另外displayFactors需要添加一个互斥量

/*in displayFactors function*/
pthread_cond_wait(&product, &mutex1); 

And where to put those statements, you should figure it out by yourself. 在哪里放置这些语句,您应该自己弄清楚。 Hope that can help you. 希望能对您有所帮助。

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

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