-1

I'm trying to implement a game of life program in C++. I wish to resort only to basic tools, ie no vector whatsoever, in order to understand what is going on behind the scene.

I have a world class, like this:

class World
{
private:
    int size[2];
    int flat_size;
    int *pop_n;
public:
    World();
    virtual ~World();
};

In the constructor, I ask for the world size and create the population array:

World::World(){
int counter = 0;
int n = 0;
string size_string;

cout << "Please give world size, as 'width height':";
getline(cin, size_string );
istringstream size_string_s(size_string); 
while (size_string_s >> n ){
    size[counter] = n;
    counter++;
}

flat_size = size[0]*size[1];
pop_n = new int[ flat_size ];

// initialize seed for rand
srand (time(NULL));

for (int i = 0; i < size[0]; i++){
    for (int j = 0; j < size[1]; j++){
        pop_n[ size[0] * i + j ] = rand() % 2;  
    }
}

cout << "A world is born" << endl;
}

I thought that I had to free the two arrays pop_n and pop_np1 in the destructor, so I wrote:

World::~World(){
delete [] pop_n;
cout << "A world has died" << endl;
}

The main is:

int main()
{
cout << "Hello Cthulhu!" << endl;
World sekai;

return 0;
}

but I get this error: Error in `./gol': double free or corruption (!prev): 0x0000000001b26070 * [1] 4582 segmentation fault (core dumped) ./gol

If i comment out the following lines, then every thing works fine...:

for (int i = 0; i < size[0]; i++){
    for (int j = 0; j < size[1]; j++){
        pop_n[ size[0] * i + j ] = rand() % 2;  
    }
}

Thanks

edit: Thanks to the comments, I edited the post with a minimal non working example and pin pointed the problem, that , however, I still don't understand.

Napseis
  • 833
  • 2
  • 10
  • 24
  • 1
    There is no excuse to use `new[]` in C++. Dynamic arrays are a misfeature that is never elegant and often wrong. Just use something sane instead. – Kerrek SB May 01 '14 at 18:30
  • I strongly agree with @KerrekSB, plus I run your code GCC 4.7.2 and it runs fine. – 101010 May 01 '14 at 18:31
  • Check whether you are messing something up in `World::evolve()` or `World::draw_text()`. – 101010 May 01 '14 at 18:33
  • 2
    I think @Napseis gives a good reason for using dynamic arrays: "to understand what is going on behind the scene". It's not what you would do in production code, but if you want to understand how C/C++ works then it's important to know about memory management issues. – user823981 May 01 '14 at 18:36
  • 1
    I see no error in constructor or destructor which would cause the error you're seeing -- the problem probably lies in code you have not supplied. Note, however, that it's generally better not to do user-centric input within a constructor. Get and validate the values first and *then* call the constructor. – Edward May 01 '14 at 18:42
  • Thanks for your comments. First, I emphasized that I specifically used new to understand how things works. I am to use GPGPU libraries, with and cuda by default comes with C type cudaMalloc, etc. If I am to understand C++ libraries based on CUDA, I believe I have to practice like this. Also, I edited my post to add a minimal non working example, and further localized the issue. I'll take into account Edward comment's and modify my code later on. – Napseis May 01 '14 at 19:05
  • When you say you want to know what's going on behind the scenes, it sounds like you probably want to learn C, rather than C++. – Dan May 01 '14 at 20:01

1 Answers1

0

Ok, I found the problem. The index in 'pop_n[ size[0] * i + j ]' is incorrect. The correct loop was:

for (int l = 0; l < size[1]; l++){
    for (int c = 0; c < size[0]; c++){
        pop_n[ size[0] * l + c ] = 1;//rand() % 2;  
        nbn[ size[0] * l + c ] = 0; 
    }
}

I still wonder why the delete failed though.

Napseis
  • 833
  • 2
  • 10
  • 24