使用pthread时为什么会出现Sysmalloc错误?

时间:2019-11-14 22:33:14

标签: pthreads malloc

我收到一条错误消息,提示“ malloc.c:2399:sysmalloc:断言(....)失败”。我找不到解决此问题的方法。我试图创建一个线程来计算矩阵的两列的点积,但是突然之间,当我没有更早开始时,我开始收到此错误。撤消更改时,sysmalloc错误不断出现。这是我的主要功能:

int main(int argc, char** argv) {

    // miscellaneous code ommitted // 

    int* firstFileValues = openFirstFile(matrix1File); 
    int* secondFileValues = openSecondFile(matrix2File); 

    // create the matrix 
    struct Matrix* firstMatrix; 
    firstMatrix = matrix_init_with_list(firstFileValues); 

    struct Matrix* secondMatrix; 
    secondMatrix = matrix_init_with_list(secondFileValues); 

    printMatrix(firstMatrix); 
    printMatrix(secondMatrix);

    printf("here"); 

    messageBuffer message; 
    message = createMessageBuffer(1, 0, 5, 5, 1); 

    int i; 
    for (i = 0; i < 10; i++) {
        message.data[i] = i; 
    }

    printf("creating thread\n"); 
    pthread_t thread; 
    pthread_create(&thread, NULL, packagingThread, &message); 
    pthread_join(thread, NULL); 

}

线程永远不会运行,并且在它将要创建胎面时执行总是会立即停止。奇怪的是,当我注释掉用于创建第二个矩阵的代码时,即使是线程,代码也可以正常运行。有人能帮我吗?谢谢!

编辑更新:添加了matrix_init_with_list()代码。列表值的格式如下:     index0:矩阵中的行数     index1:矩阵中的列数     index2:矩阵中的第一个值           。           。           。     indexN:矩阵中的第n个值

struct Matrix* matrix_init_with_list(int* values) {

    // allocate the matrix 
    struct Matrix* theMatrix; 
    theMatrix = (struct Matrix*) malloc(sizeof(struct Matrix)); 

    // assign rows and values 
    int rows = values[0]; 
    int cols = values[1]; 
    theMatrix->rows = rows; 
    theMatrix->cols = cols; 

    // allocate the values matrix
    theMatrix->values = (int**) malloc(rows * sizeof(int*)); 
    int i; 
    for (i = 0; i < rows; i++) {
        theMatrix->values[i] = (int*) malloc(cols * sizeof(int)); 
    }

    // assign the values 
    int j; 
    int count = 2; 
    for (i = 0; i < rows; i++) {
        for (j = 0; j < cols; j++) {
            theMatrix->values[i][j] = values[count]; 
            count++; 
        }
    }

    return theMatrix; 


}

0 个答案:

没有答案