C++ c++;检测到堆损坏

C++ c++;检测到堆损坏,c++,dynamic-arrays,heap-corruption,C++,Dynamic Arrays,Heap Corruption,在运行教科书中的代码时,我遇到了一个堆损坏错误。但是,当我通过联机编译器运行它时,它可以工作。我正在使用visual studio 2013。我假设代码是正确的;我的visual studio可能有问题吗?任何帮助都将不胜感激,谢谢 #include <iostream> #include<cstdlib> #include<ctime> #include<iomanip> using namespace std; int main() {

在运行教科书中的代码时,我遇到了一个堆损坏错误。但是,当我通过联机编译器运行它时,它可以工作。我正在使用visual studio 2013。我假设代码是正确的;我的visual studio可能有问题吗?任何帮助都将不胜感激,谢谢

#include <iostream>
#include<cstdlib>
#include<ctime>
#include<iomanip>
using namespace std;

int main() {
    // your code goes here
    srand(time(0));
    int* counts[10];
    // Allocate the rows
    for (int i = 0; i < 10; i++)
    {
        counts[i] = new int[i + 1];
        for (int j = 0; j <= 1; j++)
        {
            counts[i][j] = 0;
        }
    }
    const int RUNS = 1000;
    // Simulate 1,000 balls
    for (int run = 0; run < RUNS; run++)
    {
        // Add a ball to the top
        counts[0][0]++;
        // Have the ball run to the bottom
        int j = 0;
        for (int i = 1; i < 10; i++)
        {
            int r = rand() % 2;
            // If r is even, move down,
            // otherwise to the right
            if (r == 1)
            {
                j++;
            }
            counts[i][j]++;
        }
    }
    // Print all counts
    for (int i = 0; i < 10; i++)
    {
        for (int j = 0; j <= i; j++)
        {
            cout << setw(4) << counts[i][j];
        }
        cout << endl;
    }
    // Deallocate the rows
    for (int i = 0; i < 10; i++)
    {
        delete[] counts[i];
    }

    return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main(){
//你的密码在这里
srand(时间(0));
整数*计数[10];
//分配行
对于(int i=0;i<10;i++)
{
计数[i]=新整数[i+1];
对于(intj=0;j此处

for(int i=0;i<10;i++)
{
计数[i]=新整数[i+1];
对于(intj=0;j此处

for(int i=0;i<10;i++)
{
计数[i]=新整数[i+1];
对于(intj=0;j此处

for(int i=0;i<10;i++)
{
计数[i]=新整数[i+1];
对于(intj=0;j此处

for(int i=0;i<10;i++)
{
计数[i]=新整数[i+1];


对于(int j=0;j哦..我明白你的意思。但是我如何分配第二个int?在内部循环中?对不起,我对c++@Ace_j非常陌生。我不太明白你想做什么,但是你可以尝试一些类似
counts[I]=new int[I+2];
我尝试了int[I+2],但仍然给了我同样的结果。我还尝试了
srand(time(0));int**counts=new int*[10];for(int i=0;i<10;i++){counts[i]=new int[i+1];}
也不起作用:(@Ace_J我只是检查
counts[i]=new int[i+2]
-在我的VS2010中工作正常。再次检查所有内容。这次没有错误消息,但输出仍然不正确:前两行是正确的,从第三行开始,它变成了243 508-84215002。我甚至重新安装了我的VS。哦……我明白你的意思。但是我如何分配第二个int?在内部循环中?对不起,我很抱歉ew to c++@Ace_J我不太明白你想做什么,但是你可以尝试一些类似于
counts[I]=new int[I+2];
I尝试了int[I+2],仍然给了我同样的东西。我还尝试了
srand(time(0));int**counts=new int*[10];for(int I=0;I<10;I++{counts[I]=new int[I+1]}
也不起作用:(@Ace_J我只是检查一下
计数[I]=new int[I+2]
-在我的VS2010中工作正常。再次检查所有内容。这次没有错误消息,但输出仍然不正确:前两行是正确的,从第三行开始,它变成了243 508-84215002。我甚至重新安装了我的VS。哦……我明白你的意思。但是我如何分配第二个int?在内部循环中?对不起,我很抱歉ew to c++@Ace_J我不太明白你想做什么,但是你可以尝试一些类似于
counts[I]=new int[I+2];
I尝试了int[I+2],仍然给了我同样的东西。我还尝试了
srand(time(0));int**counts=new int*[10];for(int I=0;I<10;I++{counts[I]=new int[I+1]}
也不起作用:(@Ace_J我只是检查一下
计数[I]=new int[I+2]
-在我的VS2010中工作正常。再次检查所有内容。这次没有错误消息,但输出仍然不正确:前两行是正确的,从第三行开始,它变成了243 508-84215002。我甚至重新安装了我的VS。哦……我明白你的意思。但是我如何分配第二个int?在内部循环中?对不起,我很抱歉ew to c++@Ace_J我不太明白你想做什么,但是你可以尝试一些类似于
counts[I]=new int[I+2];
I尝试了int[I+2],仍然给了我同样的东西。我还尝试了
srand(time(0));int**counts=new int*[10];for(int I=0;I<10;I++{counts[I]=new int[I+1]}
也不起作用:(@Ace_J我只是检查一下
计数[I]=new int[I+2];
-在我的VS2010中工作正常。再次检查。这次没有错误消息,但输出仍然不正确:前两行是正确的,从第三行开始,它变成了243 508-84215002。我甚至重新安装了我的VS。
 for (int i = 0; i < 10; i++)
    {
        counts[i] = new int[i + 1];
        for (int j = 0; j <= 1; j++)
        {
            counts[i][j] = 0;
        }
    }