Game of life C++ implementation memory access bug I'm learning C++ so I thought I'd make game of life for practice and explore pointers, 1.5D arrays and stuff like that.

I have "finished" my implementation, but I have trouble with memory access. It seems like I'm stepping over the bounds somewhere and I simply can't figure out where. Everything looks fine to me.

Problem is most likely in the function called void World::follow that is checking surrounding cells if they are alive and than sets state for center cell for the next generation/tick depending of how many neighbors it has but I simply don't see it.

Here is my code:

source.cpp

#include "World.h"

int main() {
    World world = World(40, 20, 20);
    while (world.update())
        world.render();
}


Organism.h

#pragma once

class Organism {
private:
    bool cState, nState;
public:
    Organism(bool state);
    void perish();
    void revive();
    bool status();
    void update();
};


World.h

#pragma once
#include "Organism.h"

class World {
private:
    Organism** plain;
    int population;
    short width, height;
public:
    World(short width, short height, short chance);
    ~World();
    bool update();
    void render();
    void follow(short x, short y);
};


Organism.cpp

#include "Organism.h"

Organism::Organism(bool state) {
    this-&gt;cState = state;
}

void Organism::perish() {
    this-&gt;nState = false;
}

void Organism::revive() {
    this-&gt;nState = true;
}

bool Organism::status() {
    return this-&gt;cState;
}

void Organism::update() {
    this-&gt;cState = this-&gt;nState;
}


World.cpp

#include "World.h"

#include &lt;time.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;iostream&gt;
#include &lt;Windows.h&gt;

using namespace std;

World::World(short width, short height, short chance) {
    this-&gt;population = 0;
    this-&gt;width = width; this-&gt;height = height;

    this-&gt;plain = new Organism*[width*height];

    bool state;
    srand(time(nullptr));
    for (int i = 0; i &lt; (width*height); i++) {
        state = rand() % 100 &lt; chance;
        this-&gt;plain[i] = new Organism(state);
    }

    for (short x = 0; x &lt; this-&gt;width; x++)
        for (short y = 0; y &lt; this-&gt;height; y++)
            this-&gt;follow(x, y);
}

bool World::update() {
    this-&gt;population = 0;

    for (int i = 0; i &lt; (width*height); i++) {
        this-&gt;plain[i]-&gt;update();
        if (this-&gt;plain[i]-&gt;status())
            this-&gt;population++;
    }

    for (short x = 0; x &lt; this-&gt;width; x++)
        for (short y = 0; y &lt; this-&gt;height; y++)
            this-&gt;follow(x,y);

    return this-&gt;population &gt; 0;
}

void World::follow(short x, short y) {
    bool alive;
    short pals = 0;
    Organism* current = this-&gt;plain[x*this-&gt;width + y];

    short sx = (x - 1 &lt; 0) ? x : x - 1;
    short sy = (y - 1 &lt; 0) ? y : y - 1;
    short ex = (x + 1 == this-&gt;width - 1) ? x : x + 1;
    short ey = (y + 1 == this-&gt;height - 1) ? y : y + 1;

    for (short row = sx; row &lt; ex; row++) {
        for (short col = sy; col &lt; ey; col++) {
            alive = this-&gt;plain[row*this-&gt;width + col]-&gt;status();
            if (row != x &amp;&amp; col != y &amp;&amp; alive) pals++;
        }
    }

    if (!current-&gt;status() &amp;&amp; pals == 3)
        current-&gt;revive();
    else if (current-&gt;status() &amp;&amp; pals != 2 &amp;&amp; pals != 3)
        current-&gt;perish();
}

void World::render() {
    Sleep(500);
    system("cls");
    for (short x = 0; x &lt; this-&gt;width; x++) {
        for (short y = 0; y &lt; this-&gt;height; y++) {
            cout &lt;&lt; (this-&gt;plain[x*this-&gt;width + y]-&gt;status()) ? " " : "x";
        }
        cout &lt;&lt; endl;
    }
}

World::~World() {
    for (int i = this-&gt;width * this-&gt;height; i &gt;= 0; i--)
        delete this-&gt;plain[i];
    delete[] this-&gt;plain;
}
Stop doing manually memory management (this is not C).
Organism** plain;

There is no reason to have an array of pointers. You could just have an array of object (thus you don't need to manage the individual cells).
Organism*  plain;
....
plain = new Organism[width * height]; // Allocates and calls default constructor.
                                      // You will still need to loop over
                                      // them to reset them to an initial good
                                      // state (but you will not have) to
                                      // manage the memory for individual cells.

Because the cells are no longer pointers you will need to replace -&gt; with . (a dot). But other than than no difference.
Part 2 of memory management
Take this a step further. You don't need to manually manage the memory for the array of plain. We have classes that do that for you.
std::vector&lt;Organism&gt;  plain;        // initially this holds zero elements
...

plain.reserve(width * height);       // Let the object know you are going to
                                     // add a lot of cells so it can pre-allocate space.

for (int i = 0; i &lt; (width * height); i++) {
    state = rand() % 100 &lt; chance;
    plain.emplace_back(state);       // add a new element to the end.
    // If you are using C++03 you may not have emplace_back()
    // So use the regular push_back instead (it does the same thing).
    //    plain.push_back(Organism(state));
}

You can access plain (a std::vector&lt;&gt;) with operator [] like you would an array. So no other code needs to change (you just need any new or delete calls). So you have no memory management calls and thus no headaches with leaks.
Now a nice bit is you can replace plain[x] with plain.at(x) and it checks to make sure you access to the array is in bounds (and will throw an exception if not). This may help you track down your error.
Initialize srand() one
You should only initializre the random number generator once in a program.
srand(time(nullptr));

So it is best to do this in main(). In your application you are doing this once for each game object. This may not seem like a big thing. But of your code get's re-used by another application that does not realize you are doing this. You mess up other people's code.
You should also note. This old random number generator is considered pretty bad. You should probably start looking at the new ones provided by C++11. They are much better and can be used in ways that don't interact (so you don't screw over other people's random numbers).
Move Platform specific code to its own function.
Sleep(500);
system(&quot;cls&quot;);

You should probably put this in its own function. So when people come to port your code to another platform they just need to provide an alternative implementation to a couple of specific functions and the rest of the code can stay the same.