引发异常:写访问冲突。质数数组

时间:2019-02-26 02:24:52

标签: c++ pointers

我正在编写一个c ++程序,其中我用一个从1到100,000的顺序值填充了一个大小为100,000的大型数组。现在,我试图使此数组仅填充质数。我有一个定义为“ mult”的变量,它从2开始并经过数组并删除每个第二个位置(2的倍数),并且我想再次循环对3、5、7等进行相同操作。但是,我得到一个抛出异常,并且我的指针p是一个非常大的数字。我会很感激为什么发生这种情况的任何线索。这是我的代码:

// Fill up the array with values 1 - 100,000.
void generateBigArray(int *p, int size)
{
int count = 1;
for (int i = 0; i < size; i++)
    {
        *p = count++;
        p = p + 1; // bump to the next location.
    }
}

void deleteMults(int *p, int size)
{
    int mult = 1;
    while (mult <= size)
    {
        for (int i = mult; i < size; i = i + mult)
        {
            p = p + 1; // starting at 2.
            mult = mult + 1;
            while (*p != 0)
            {
                *p = 0;
                p = p + mult;
            }
            mult = mult + 1;
        }
    }
 }


int main()
{
    // an array to hold prime numbers from 1 - 100,000
    const int BIGSIZE = 10000;
    int bigAry[BIGSIZE];

    // a pointer for the prime number array.
    int *bigptr;
    bigptr = bigAry;

    generateBigArray(bigptr, BIGSIZE);
    deleteMults(bigptr, BIGSIZE);

    return 0;
}

1 个答案:

答案 0 :(得分:0)

根据您的编译器,可以为数组分配的最大大小可能相差很大。由于您将Notice: wp_deregister_script was called <strong>incorrectly</strong>. Do not deregister the <code>jquery</code> script in the administration area. To target the front-end theme, use the <code>wp_enqueue_scripts</code> hook. Please see <a href="https://codex.wordpress.org/Debugging_in_WordPress">Debugging in WordPress</a> for more information. (This message was added in version 3.6.0.) in /app/public/wp-includes/functions.php on line 4204 改成bigptr,因此可以尝试以下方法:

bigAry

在退出int * bigptr = new int[BIGSIZE]; 之前,应添加以下内容:

main