Memory C++;内存问题

Memory C++;内存问题,memory,c++-cli,numbers,Memory,C++ Cli,Numbers,我目前正在构建素数查找程序,但内存有问题: 这可能是由于堆损坏,这表明PrimeNumbers.exe或其加载的任何DLL中存在错误 请不要对我说,如果这不是找到素数的方法,我想自己解决它 代码: // PrimeNumbers.cpp : main project file. #include "stdafx.h" #include <vector> using namespace System; using namespace std; int main(array<

我目前正在构建素数查找程序,但内存有问题:

这可能是由于堆损坏,这表明PrimeNumbers.exe或其加载的任何DLL中存在错误

请不要对我说,如果这不是找到素数的方法,我想自己解决它

代码:

// PrimeNumbers.cpp : main project file.

#include "stdafx.h"
#include <vector>

using namespace System;
using namespace std;

int main(array<System::String ^> ^args)
{
Console::WriteLine(L"Until what number do you want to stop?");
signed const int numtstop = Convert::ToInt16(Console::ReadLine());
bool * isvalid = new bool[numtstop];


    int allattempts = numtstop*numtstop; // Find all the possible combinations of numbers

    for (int currentnumb = 0; currentnumb <= allattempts; currentnumb++) // For each number try to find a combination
    {
        for (int i = 0; i <= numtstop; i++)
        {
            for (int tnumb = 0; tnumb <= numtstop; tnumb++)
            {
                if (i*tnumb == currentnumb)
                {
                    isvalid[currentnumb] = false;
                    Console::WriteLine("Error");
                }
            }
        }
    }

    Console::WriteLine(L"\nAll prime number in the range of:" + Convert::ToString(numtstop));

    for (int pnts = 0; pnts <= numtstop; pnts++)
    {
        if (isvalid[pnts] != false)
        {
            Console::WriteLine(pnts);
        }
    }

return 0;
}
//PrimeNumbers.cpp:主项目文件。
#包括“stdafx.h”
#包括
使用名称空间系统;
使用名称空间std;
int main(数组^args)
{
控制台::WriteLine(L“直到您想停在哪个号码?”);
signed const int numstop=Convert::ToInt16(Console::ReadLine());
bool*isvalid=新bool[numstop];
int allattempts=numstop*numstop;//查找所有可能的数字组合

对于(int CurrNoStub=0;CurrutNub 问题是,在托管C++/CLI代码中使用本机C++。当然,使用新的而不删除。< /P>
`currentnumb` :   

大于数组的大小,该数组的大小仅为
numstop
。您可能会越界,这可能是您的问题。

您正在分配
numstop
布尔值,但您使用范围从零到
numstop*numstop
的变量对该数组进行索引。这将严重超出所有
nu的界限mstop
值大于
1

您应该分配更多布尔值(
numstop*numstop
),或者使用不同的变量索引到
isvalid
(例如,
i
,范围从
0
numstop
)对不起,我不能说得更准确,因为你要求不要评论你的素数算法。

另外,如果你想读一些关于寻找小素数的内容,这里有一个到a的链接。他教你如何为第35至49页上的前1000个素数构造一个程序。

你永远不会

删除[]
你的
是有效的
本地,这是内存泄漏