【问题标题】:Why is this program giving a stack overflow?为什么这个程序会出现堆栈溢出?
【发布时间】:2017-09-13 01:02:03
【问题描述】:

当我在程序顶部全局初始化数组直到堆栈溢出时,我的 C 合并代码才起作用。我正在尝试使用malloc 初始化数组,但是当我这样做时,代码只会读取两个整数并停止运行。

该程序从名为 alg.txt 的文件中提取随机数,然后对其进行排序。同样,当在程序顶部将 z 定义为要排序的整数数,并在全局范围内声明数组等于 arr[z] 时,代码仍然有效(直到 500k 整数)。我如何弄清楚发生了什么?

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

int count = 0;
void merge(int arr[], int l, int m, int r)
{
    int i, j, k;
    int n1 = m - l + 1;
    int n2 =  r - m;

    int L[n1], R[n2];

    for (i = 0; i < n1; i++)
        L[i] = arr[l + i];
        count++;

    for (j = 0; j < n2; j++)
        R[j] = arr[m + 1+ j];
        count++;

    i = 0;
    j = 0;
    k = l;
    while (i < n1 && j < n2)
    {
        if (L[i] <= R[j])
        {
            arr[k] = L[i];
            i++;
            count++;
        }
        else
        {
            arr[k] = R[j];
            j++;
            count++;
        }
        k++;
        count++;
    }

    while (i < n1)
    {
        arr[k] = L[i];
        i++;
        k++;
        count++;
    }

    while (j < n2)
    {
        arr[k] = R[j];
        j++;
        k++;
        count++;
    }
}

void mergeSort(int arr[], int l, int r)
{
    if (l < r)
    {
        int m = l+(r-l)/2;

        mergeSort(arr, l, m);
        mergeSort(arr, m+1, r);

        merge(arr, l, m, r);
    }
}

void printArray(int A[], int size)
{
    int i;
    for (i=0; i < size; i++)
        printf("%d ", A[i]);
    printf("\n");
}

int main()
{
    int i;
    FILE *myFile;
    myFile = fopen("alg.txt", "r");

    int z;
    printf("Enter the size of the array: ");
    scanf("%d", &z);
    int *arr = (int *)malloc(z*sizeof(int));
    int n = sizeof(arr)/sizeof(arr[0]);
    for(i=0; i < z; i++)
    {
        fscanf(myFile, "%d,", &arr[i]);
    }

    mergeSort(arr, 0, n - 1);

    printf("\nSorted array is \n");
    printArray(arr, n);
    printf("count is %d\n", count);
    return 0;
}

【问题讨论】:

  • int n = sizeof(arr)/sizeof(arr[0]);int n = sizeof(int *)/sizeof(int);
  • int L[n1], R[n2]; 他们每次调用都会消耗堆栈。无法保护太大的数组。
  • C 和 C++ 不一样(所以 C++ 标签是错误的)
  • 编译所有警告和调试信息(gcc -Wall -Wextra -gGCC)。然后使用调试器gdb了解发生了什么。

标签: c sorting mergesort


【解决方案1】:

您在本地声明 LR 数组(在 merge 函数内)。他们在每个函数调用上消耗堆栈。您应该全局初始化它们,以便它们可以存储在堆上。此外,您应该只声​​明一个数组(如以下代码中的 temp_arr),因为不需要两个单独的数组,如 LR

Local vs Global variable storage

你可以改进你的合并功能:

int temp_arr[500000];   // Global declaration
void merge(int arr[], int l, int m, int r)
{
    int i=l, j=m+1, k=l;
    while(i<=m && j<=r)
    {
        if(arr[i]<arr[j])
            temp_arr[++k] = arr[++i];
        else temp_arr[++k] = arr[++j];
    }
    while(i<=m)
        temp_arr[++k] = arr[++i];
    while(j<=r)
        temp_arr[++k] = arr[++j];
    i = l;
    while(i<=r)
        arr[i] = temp_arr[++i];  // Storing stored array in arr
}

【讨论】:

  • 该代码甚至无法编译。您不能使用 malloc 表达式(在任何例程之外)初始化全局变量,并且您不应该在 C 中转换 malloc 的结果。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-18
  • 1970-01-01
  • 1970-01-01
  • 2012-12-19
  • 2014-12-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多