简体   繁体   English

为什么我的基数排序会出现 C6385 错误

[英]Why do i get the C6385 error in my radix sort

I am trying to write my own version of radix sort in order to better understand the algorithm.我正在尝试编写自己的基数排序版本,以便更好地理解算法。 The problem is that in the first inner for loop("j") i get the C6385 error in VS.问题是在第一个内部 for 循环(“j”)中,我在 VS 中遇到了 C6385 错误。 I have no idea of how to rephrase that line in order to make it work.我不知道如何改写该行以使其正常工作。 What am i doing wrong here?我在这里做错了什么?

The warning is:警告是:

warning C6385: Reading invalid data from 'countQueues': the readable size is '400' bytes, but '4000' bytes may be read.警告 C6385:从“countQueues”读取无效数据:可读大小为“400”字节,但可能会读取“4000”字节。


void radixSort(int arr[], int arraySize)
{
    int countDigits = GetMax(arr, arraySize);   //max number of digits
    queue<int> countQueues[10];
    int modulo = 1;

    for (int i = 0; i < countDigits; i++)
    {
        modulo *= 10;
        for (int j = 0; j < arraySize; j++)     //store the values in the queues
        {
            countQueues[arr[j] % modulo].push(arr[j]);  //error here
        }


        for (int k = 0; k < 10; k++)        // move them back in the array
        {
            while (!countQueues[k].empty())
            {
                arr[i] = countQueues[k].front();
                countQueues[k].pop();
            }
        }
    }
}

Consider the second iteration of the first loop when modulo = 100. Then this line of code:考虑当 modulo = 100 时第一个循环的第二次迭代。然后这行代码:

            countQueues[arr[j] % modulo].push(arr[j]);  //error here

could be trying to access countQueues[up to 99], but countQueues has a size of 10.可能试图访问 countQueues[最多 99],但 countQueues 的大小为 10。

This is an issue with trying to implement radix sort most significant digit first.这是尝试首先实现基数排序最高有效数字的问题。 The 1st iteration uses 10 queues, 2nd iteration 100 queues, 3rd iteration 1000 queues, ... , which could be simplified using recursion, but would consume a lot of space, 10 raised to the countDigits power.第 1 次迭代使用 10 个队列,第 2 次迭代 100 个队列,第 3 次迭代 1000 个队列,...,这可以使用递归来简化,但会消耗大量空间,10 增加到 countDigits 次方。

Implementing a radix sort least significant digit first avoids this problem.首先实现基数排序最低有效位可以避免这个问题。 A [countDigits][10] matrix could be used.可以使用 [countDigits][10] 矩阵。 The initial pass generates counts and stores them into the matrix, where matrix[i][] is an array of 10 counts for the number of occurrences of each digit, 0 through 9. The counts are converted into the starting indexes for each logical bin of matrix[i].初始传递生成计数并将它们存储到矩阵中,其中 matrix[i][] 是一个包含 10 个计数的数组,表示每个数字的出现次数,从 0 到 9。计数被转换为每个逻辑 bin 的起始索引矩阵[i]。 The radix sort is then done in countDigits passes, least significant digit/bin first.然后在 countDigits 遍中完成基数排序,首先是最低有效数字/bin。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM