从函数返回到main的指针数组是NULL

时间:2018-03-31 11:32:59

标签: c arrays segmentation-fault malloc

我被要求在C中创建一个函数,它获取一个int的数组,它的大小和一个字符,表示排序是需要升序还是降序(0表示升序,任何其他字符如果降序),并返回一个指针数组。如果排序是升序的,则指针将从int的数组中的值的最小值到最大值进行排序。 Like that 一切都运行良好,直到指针数组返回主值为NULL。我的意思是,在函数结束并返回数组之前,数组中有值(并且它们也很好地排序),但是当main从函数获取地址的那一刻,它就是null 。有人熟悉这个问题吗?

谢谢!

#include <stdio.h>

#define SIZE 100

// Functions declaration
int** pointerSort(int* arr, unsigned int size, char ascend_flag);
void sortMerge (int** sortedArr, int left, int right, char ascend_flag);
void mergeSortAscend (int** arr, int left, int middle, int right);
void mergeSortDescend (int** arr, int left, int middle, int right);
void copyAddress(int** addressArray, int* arr, unsigned int size);

int main()
{
    int** sortedArr;
    int  arr[5] = {8,1,7,2,4};
    int size = 5;

    sortedArr = pointerSort(arr, size, '1');

    return 0;
}

// The pointerSort function gets an array, its size and a flag representing "sort by" (ascending, descending)
int** pointerSort(int* arr, unsigned int size, char ascend_flag)
{
    // Variables declaration
    int* sortedArr[size];

    // Copying the addresses of each value in arr to an addresses array
    copyAddress(sortedArr, arr, size);

    // Sending the array of addresses, its start and end and the ascend_flag to the sortMerge function
    sortMerge(sortedArr, 0, size - 1, ascend_flag);

    return (sortedArr);
}

void sortMerge (int** sortedArr, int left, int right, char ascend_flag)
{
    // Variables declaration
    int middle = (left + right) / 2;

    // Checking if there are more than one cell
    if (left < right)
    {
        // Splitting the array into two
        sortMerge(sortedArr, left, middle, ascend_flag);
        sortMerge(sortedArr, middle + 1, right, ascend_flag);

        // Checking if the sort requested is ascending or descending
        if (ascend_flag != '0')
        {
            mergeSortAscend(sortedArr, left, middle, right);
        }
        else
        {
            mergeSortDescend(sortedArr, left, middle, right);
        }
    }
}

void mergeSortAscend (int** arr, int left, int middle, int right)
{
    int l = left, m = middle + 1, r = right, i = left;
    int* temp[SIZE];

    while ((l <= middle) && (m <= right))
    {
        if (*(arr[l]) > *(arr[m]))
        {
            temp[i] = arr[m];
            m++;
        }
        else
        {
            temp[i] = arr[l];
            l++;
        }
        i++;
    }

    if (l <= middle)
    {
        while (l <= middle)
        {
            temp[i] = arr[l];
            l++;
            i++;
        }
    }

    if (m <= right)
    {
        while (m <= right)
        {
            temp[i] = arr[m];
            m++;
            i++;
        }
    }
    for (int k = left; k <= right; k++)
    {
        arr[k] = temp[k];
    }
}

void copyAddress(int** addressArray, int* arr, unsigned int size)
{
    int i;

    for (i = 0; i < size; i++)
    {
        addressArray[i] = &(arr[i]);
    }
}

void mergeSortDescend (int** arr, int left, int middle, int right)
{
    int l = left, m = middle + 1, r = right, i = left;
    int* temp[SIZE];

    while ((l <= middle) && (m <= right))
    {
        if (*(arr[l]) <= *(arr[m]))
        {
            temp[i] = arr[m];
            m++;
        }
        else
        {
            temp[i] = arr[l];
            l++;
        }
        i++;
    }

    if (l <= middle)
    {
        while (l <= middle)
        {
            temp[i] = arr[l];
            l++;
            i++;
        }
    }
    if (m <= right)
    {
        while (m <= right)
        {
            temp[i] = arr[m];
            m++;
            i++;
        }
    }
    for (int k = left; k <= right; k++)
    {
        arr[k] = temp[k];
    }
}

1 个答案:

答案 0 :(得分:0)

函数返回后int* sortedArr[size];中的int** pointerSort(int* arr, unsigned int size, char ascend_flag)变为无效,因为它是分配给堆栈的自动变量。

您需要永久存储空间。例如,sortedArr函数在堆上为malloc分配内存并将其返回。

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

#define SIZE 100

// Functions declaration
int** pointerSort(int* arr, unsigned int size, char ascend_flag);
void sortMerge (int** sortedArr, int left, int right, char ascend_flag);
void mergeSortAscend (int** arr, int left, int middle, int right);
void mergeSortDescend (int** arr, int left, int middle, int right);
void copyAddress(int** addressArray, int* arr, unsigned int size);

int main(void)
{
    int** sortedArr;
    int  arr[5] = {8,1,7,2,4};
    int size = 5;
    int i =0;

    sortedArr = pointerSort(arr, size, '1');

    if(sortedArr) // memory was allocated
       for(i=0; i<5; i++){
           printf("%d ", *sortedArr[i]);
       }

    free(sortedArr);
    return 0;
}

// The pointerSort function gets an array, its size and a flag representing "sort by" (ascending, descending)
int** pointerSort(int* arr, unsigned int size, char ascend_flag)
{
    // Variables declaration
    //int* sortedArr[size];
    int ** sortedArr = malloc( sizeof(int *) * size);
    if(sortedArr == NULL) return NULL; // no memory

    // Copying the addresses of each value in arr to an addresses array
    copyAddress(sortedArr, arr, size);

    // Sending the array of addresses, its start and end and the ascend_flag to the sortMerge function
    sortMerge(sortedArr, 0, size - 1, ascend_flag);

    return (sortedArr);
}

void sortMerge (int** sortedArr, int left, int right, char ascend_flag)
{
    // Variables declaration
    int middle = (left + right) / 2;

    // Checking if there are more than one cell
    if (left < right)
    {
        // Splitting the array into two
        sortMerge(sortedArr, left, middle, ascend_flag);
        sortMerge(sortedArr, middle + 1, right, ascend_flag);

        // Checking if the sort requested is ascending or descending
        if (ascend_flag != '0')
        {
            mergeSortAscend(sortedArr, left, middle, right);
        }
        else
        {
            mergeSortDescend(sortedArr, left, middle, right);
        }
    }
}

void mergeSortAscend (int** arr, int left, int middle, int right)
{
    int l = left, m = middle + 1, r = right, i = left;
    int* temp[SIZE];

    while ((l <= middle) && (m <= right))
    {
        if (*(arr[l]) > *(arr[m]))
        {
            temp[i] = arr[m];
            m++;
        }
        else
        {
            temp[i] = arr[l];
            l++;
        }
        i++;
    }

    if (l <= middle)
    {
        while (l <= middle)
        {
            temp[i] = arr[l];
            l++;
            i++;
        }
    }

    if (m <= right)
    {
        while (m <= right)
        {
            temp[i] = arr[m];
            m++;
            i++;
        }
    }
    for (int k = left; k <= right; k++)
    {
        arr[k] = temp[k];
    }
}

void copyAddress(int** addressArray, int* arr, unsigned int size)
{
    int i;

    for (i = 0; i < size; i++)
    {
        addressArray[i] = &(arr[i]);
    }
}

void mergeSortDescend (int** arr, int left, int middle, int right)
{
    int l = left, m = middle + 1, r = right, i = left;
    int* temp[SIZE];

    while ((l <= middle) && (m <= right))
    {
        if (*(arr[l]) <= *(arr[m]))
        {
            temp[i] = arr[m];
            m++;
        }
        else
        {
            temp[i] = arr[l];
            l++;
        }
        i++;
    }

    if (l <= middle)
    {
        while (l <= middle)
        {
            temp[i] = arr[l];
            l++;
            i++;
        }
    }

    if (m <= right)
    {
        while (m <= right)
        {
            temp[i] = arr[m];
            m++;
            i++;
        }
    }

    for (int k = left; k <= right; k++)
    {
        arr[k] = temp[k];
    }
}

测试:

1 2 4 7 8