'malloc(): 损坏的顶部大小' 分配超过 200K int 后

'malloc(): corrupted top size' After allocating more than 200K int

提问人:der Kirschbaum 提问时间:10/16/2023 最后编辑:Florian Weimerder Kirschbaum 更新时间:10/17/2023 访问量:89

问:

我接到的任务是使用 openMP 进行 Bucket Sort,我决定对每个 Bucket 进行快速排序。该要求希望我通过不断增加整数数量并更改线程数来进行测试,直到达到 100 万个整数和 16 个线程。

这是我的 C 代码:

#include <stdio.h>
#include <omp.h>
#include <time.h>
#include <stdlib.h>
#include <math.h>

void swap(int* a, int* b) {
    int temp = *a;
    *a = *b;
    *b = temp;

}

int partition(int arr[], int low, int high) {

    int pivot = arr[high];
    int i = low - 1;

    for (int j = low; j < high; j++) {

        if (arr[j] <= pivot) {
            i++;
            swap(&arr[i], &arr[j]);
        }

    }

    swap(&arr[i + 1], &arr[high]);

    return (i + 1);
}

void quickSort(int arr[], int low, int high) {

    if (low < high) {
        int pi = partition(arr, low, high);

        quickSort(arr, low, pi - 1);
        quickSort(arr, pi + 1, high);
    }

}

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

    //Check arguments
    if (argc > 3 || argc < 3) {
        fprintf(stderr, "Error: Invalid arguments. This program require 2 arguments.\nUsage: ./bucketSort <thread number> <amount of random number>\n");
        return 1;
    }

    printf("Random seed");

    //Initialize random seed
    srand((unsigned)time(NULL));

    int threadNum = atoi(argv[1]);
    int randomTime = atoi(argv[2]);

    int* numArr = (int*)malloc(randomTime * sizeof(int));

    if(numArr == NULL){
        fprintf(stderr, "Error: Unable to allocate memory.");
        return 1;
    }

    printf("\nStart random");

    //Since RAND_MAX is limited to 0x7FFF (32,767), so we need to get creative to random beyond RAND_MAX
    for (int i = 0; i < randomTime; i++) {

        int rand1 = rand();
        int rand2 = rand();
        int rand3 = rand();

        int combinedRandom = ((rand1 % 100) * 1000) + ((rand2 % 100) * 10) + (rand3 % 10);

        numArr[i] = combinedRandom;

    }

    printf("\nFinished Random");

    double timeSpent = 0;

    int rangePerBucket = ceil(99999 / threadNum);

    int* outputArr = (int*)malloc(randomTime * sizeof(int));

    int* groupMemberCount = (int*)malloc(threadNum * sizeof(int));

    if(outputArr == NULL || groupMemberCount == NULL){
        fprintf(stderr, "Error: Unable to allocate memory.");
        return 1;
    }

    clock_t begin = clock();

    printf("\nStart parallel section.");

    #pragma omp parallel shared(numArr, outputArr, groupMemberCount) num_threads(threadNum)
    {

        int myID = omp_get_thread_num();
        int totalThread = omp_get_num_threads();

        int beginRange = myID * rangePerBucket;
        int endRange = (myID + 1) * rangePerBucket - 1;

        int* temp = (int*)omp_alloc(rangePerBucket * sizeof(int), omp_large_cap_mem_alloc);

        if( temp == NULL ){
            fprintf(stderr, "Error: Thread %d is unable to allocate memory.", myID);

        }

        int memberCount = 0;

        //Put in bucket
        for (int j = 0; j < randomTime; j++)
        {
            if (numArr[j] >= beginRange && numArr[j] <= endRange) {
                temp[memberCount] = numArr[j];
                memberCount++;
            }
        }

        groupMemberCount[myID] = memberCount;

        int* myGroup = (int*)omp_alloc(memberCount * sizeof(int), omp_large_cap_mem_alloc);

        if( myGroup == NULL ){
            fprintf(stderr, "Error: Thread %d is unable to allocate memory.", myID);
        }

        for (int i = 0; i < memberCount; i++) {
            myGroup[i] = temp[i];
        }

        //Sort
        quickSort(myGroup, 0, memberCount - 1);
        printf("\nThread %d of %d has finished sorting.", myID, totalThread);

        //Find the start of output array
        int startIndex = 0;
        for( int i = 0; i < myID; i++ ){
            startIndex += groupMemberCount[i];
        }

        //Combine array
        for (int k = 0; k < memberCount; k++) {

            outputArr[startIndex + k] = myGroup[k];

        }

        printf("\nArray from thread %d has combined.", myID);

        omp_free(myGroup, omp_large_cap_mem_alloc);
        omp_free(temp, omp_large_cap_mem_alloc);
    }

    free(numArr);
    free(outputArr);
    free(groupMemberCount);

    clock_t end = clock();

    timeSpent = (double)(end - begin) / CLOCKS_PER_SEC;

    printf("\nTime spent sorting: %f seconds.\n", timeSpent);

    return 0;
}

我用 .在我开始使用 100K 整数进行测试之前,一切都运行良好(我在主题中写了 200K,因为我的程序分配了两次)。程序在打印后立即返回(所以前 100K 输入可以吗?这是我第一次使用,所以如果我做错了什么,请随时纠正我。顺便说一句,我在 Ubuntu WSL 中运行这段代码。gcc -fopenmp ./bucketSort.c -o ./bucketSortmalloc(): corrupted top sizeFinished RandomnumArrmalloc()omp_alloc()

我尝试过:

  • 我试过了,但结果是一样的,第二次之后出错。calloc()calloc()
  • 我试着增加到.ulimitunlimit
c 排序 malloc openmp 缓冲区溢出

评论


答:

2赞 Florian Weimer 10/17/2023 #1

通常,valgrind 或对此类错误进行良好的诊断。-fsanitize=address

编译和链接显示此行存在堆溢出:-fsanitize=address

                temp[memberCount] = numArr[j];

此时变量等于。两者都比 少一个。但是数组只有元素,因此索引超出了范围。memberCountrangePerBucketrandomTimetemprangePerBucket

评论

0赞 Darth-CodeX 10/17/2023
TBH,带有标志,非常强大valgrind-g -ggd3
0赞 der Kirschbaum 10/17/2023
哇!非常感谢!我完全忘记了可能存在重复的数字,因此存储桶中的数字可能会超出存储桶范围!我实际上也尝试过使用,但我无法理解输出,所以我不知道该怎么做。再次感谢!valgrind