分段故障chkstk_ms C ++ [英] Segmentation Fault chkstk_ms C++

查看:844
本文介绍了分段故障chkstk_ms C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要关于此以下计数排序实施的帮助。是因为x的值可能太大吗?我得到分段错误。这是gdb说的:

I need help with this following counting sort implementation. Is it because value of x can be too big? I am getting segmentation fault. This is what gdb says:

Program received signal SIGSEGV, Segmentation fault.
___chkstk_ms () at /usr/src/debug/gcc-5.4.0- 1/libgcc/config/i386/cygwin.S:146
146     /usr/src/debug/gcc-5.4.0-1/libgcc/config/i386/cygwin.S: No such file or directory.

这里是代码片段,

void radix_sort::sort_array(int array[], int n)
{
    int arrayB[n];

    auto k = *std::max_element(&array[0], &array[n - 1]);
    auto m = *std::min_element(&array[0], &array[n - 1]);

    long int x = k - m + 1;
    int arrayC[x];

    for (auto i = 0; i < n; i++)
        arrayC[array[i] - m]++;

    for (long int i = 1; i < x; i++)
        arrayC[i] = arrayC[i] + arrayC[i - 1];

    for (auto i = n - 1; i >= 0; i--)
    {
        arrayB[arrayC[array[i] - m] - 1] = array[i];
        arrayC[array[i] - m]--;
    }

    for (int i = 0; i < n; i++)
        array[i] = arrayB[i];
}


推荐答案

b
$ b

In

arrayC[array[i] - m]++;

此时代码中没有 arrayC 已分配,因此未知数增加。这可能会炸毁

at this point in the code no elements of arrayC have been assigned, so an unknown number is incremented. This may blow up

arrayB[arrayC[array[i] - m] - 1] = array[i];

几行,因为 arrayC [array [i] - m] 可能是负数或数十亿,超出了我们所知道的范围。

a few lines later because arrayC[array[i] - m] could be negative or a few billion and out of range for all we know.

这篇关于分段故障chkstk_ms C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆