1

I am currently creating a program that simulates a Galton board in C++. I understand how to create a pointer, create an array of pointers, and point each one at another array of ints. My problem is occuring when i try to descruct my pointer array, its telling me:

"Debug Error!

HEAP CORRUPTION DETECTED: after Normal block (#927) at 0x0115E978. CRT detected that the application wrote to memory after end of heap buffer."

I've been banging my head against the wall with this one, as it seems all the examples I can find online have this exact approach. I even rewrote my program into a class to make it more simple. The program runs and does exactly what it's supposed to until the ob1 starts to descruct, which is when the program pukes. I'm stuck.

Here is my code:

#include <iostream>
#include <string>
#include <algorithm>
#include <iomanip>
#include <cstdlib>
#include <ctime>

using namespace std;

class foo
{
public:
    foo();
    foo(int);
    ~foo();
    void allocateSpace();
    void runGame();
    void printResults();
private:
    int bins;
    int** p;
};

foo::foo()
{
    this->bins = 0;
}

foo::foo(int bins)
{
    this->bins = bins; 
    this->p = new int*[bins]; //setting p to array of pointers
}

foo::~foo()
{
     for (int i = 0; i < bins; i++)
     {
         delete[] this->p[i];
     }
     delete[] p;
}

void foo::allocateSpace()
{
    for (int i = 0; i < bins; i++)
    {
        this->p[i] = new int[i]; //creatung an int array of size i at each pointer array cell

        for (int j = 0; j <= i; j++)
        {
            this->p[i][j] = 0;
        }
    }
}

void foo::runGame()
{
    const int numOfRuns = 1000;
    for (int i = 0; i < numOfRuns; i++)
    {
        this->p[0][0]++; //each ball hits the first peg, so always increment it before anything else
        int j = 0; //setting j = 0 sets it to the left
        for (int i = 1; i < bins; i++)
        {
            int rando = rand() % 2;
            if (rando == 1) //move right
            {
                j++;
            }
            this->p[i][j]++;
        }
    }
}

void foo::printResults()
{
    for (int i = 0; i < bins; i++)
    {
        for (int j = 0; j <= i; j++)
        {
            cout << setw(5) << this->p[i][j];
        }
        cout << endl;
    }
}

int main()
{
    srand(time(0));
    int numOfBins;

    cout << "Enter the number of bins: ";
    cin >> numOfBins;
    cout << endl;

    foo ob1(numOfBins);

    for (int i = 0; i < 50; i++)
    {
        ob1.allocateSpace();
        ob1.runGame();
        ob1.printResults();
    }

    system("pause");
    return 0;
}

Any help would be much appreciated.

2
  • 1
    Glad that you got the answer, but read the error message again, carefully paying attention to the time. It says that it detected an error and that you wrote beyon the end of a buffer. In other words, your problem is not with deleting but only detected while deleting! That said, use std::vector. Commented Apr 7, 2015 at 19:42
  • I thought it was weird it was giving me a memory write error when it was trying to destruct, i'm a student still learning so now when this happens I can actually have an idea of the situation. As for the vector, i'd love to use one, but the assignment wanted it this way. Thank you Commented Apr 9, 2015 at 13:43

1 Answer 1

2

In allocateSpace, you write beyond the allocated object. This corrupts your heap.

    this->p[i] = new int[i];

    for (int j = 0; j <= i; j++)
    {
        this->p[i][j] = 0;
    }

printResults has a similar problem: You read beyond the allocated object.

Then, in runGame, you attempt to increment a 0 sized object.

    this->p[0][0]++;

The fix:

It seems you need to increase your allocation by 1.

    this->p[i] = new int[i+1];

This will avoid the heap corruption issue. You still have a memory leak issue, because you allocate new memory on top of your existing memory on each iteration in main().

Your code would be safer if you adopted the use of vector<> instead of managing dynamically allocated arrays.

Sign up to request clarification or add additional context in comments.

7 Comments

or get rid of the double pointers in favor of std::vector<std::vector>>
I changed the <= to < and it still throws the error, and doesnt show the correct output either. Unfortunately, I must use a double pointer.
The increment works, it increments the first cell [0][0] which represents the first peg 1000 times (1000 balls), the program works correctly. Its bombs at the desctructor, I've stepped through the program.
When you write beyond an object boundary, it causes undefined behavior. Undefined behavior may look like it is working until the point it does not.
Ok ill keep looking into it
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.