为2D阵列分配内存时的分段错误

时间:2016-07-25 00:36:08

标签: c segmentation-fault pthreads

该程序从命令行获取参数,并使用线程显示其主要因素。我正在使用结构在线程之间传递信息。结构有一个2D数组,用于保存参数,然后保存它们的因子。我现在分配的方式允许我访问我的两个参数,并给我一个分段错误(代码中记录了分段错误的位置)。我究竟做错了什么?所有代码均在下面提供。

#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-- 我测试过的论点是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

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

我使用gdb来调试你的代码,我发现buffer->factors的大小是8,这还不够。

你应该做

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

segmentation fault错误可以解决。

此外,您的代码中还有另一个错误。如果consumer首先执行,则输出将为空,因此您需要先使用cond生成producer exectue。

这里有一些提示:

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

displayFactors还需要添加互斥锁

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

在哪里发表这些陈述,你应该自己弄清楚。 希望能帮到你。