Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

corrington_j

problem with 2d arrary of base class pointers

This topic is 5291 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have the base class CCell, and the derived classes CLonerCell, and CChattyCell. I am trying to make a 2d array of CCell pointers, but when i go to create the memory for each cell, i get a crash. here is some of the code.

const int WORLD_WIDTH = 100;
const int WORLD_HEIGHT = 100;

CCell *m_cells[WORLD_WIDTH][WORLD_HEIGHT];	//cell data

	m_width = WORLD_WIDTH;
	m_height = WORLD_HEIGHT;

	//allocate memory for the cells
	for (int x = 0; x < m_width; x++)
	{
		for (int y = 0; y < m_height; y++)
		{
		    if (rand() % 2 == 0) 
			m_cells[x][y] = new CLonerCell;
                     else
			m_cells[x][y] = new CChattyCell;
		}
	}

 
thanks for the help

Share this post


Link to post
Share on other sites
Advertisement
Seems to work for me. What compiler are you using? Are you sure you don''t have any buffer overruns preceding the execution of this code that causes heap corruption? You aren''t doing any strange overloads on new, etc.?

Share this post


Link to post
Share on other sites
well the code isn''t structure like that, m_cells belongs to the world class, but i wouldn''t think there would be any difference. i can''t seem to figure out what is going on.

Share this post


Link to post
Share on other sites
The only thing I can suggest at this point then, is to try to create a minimal program that actually duplicates your problem. One way to do this is making a backup of your program and start removing what you think are irrelevant bits until it either stops crashing, at which point you''ve found a potential problem, or you don''t think there are any irrelevant bits left, in which case you can try sharing that with us.

Share this post


Link to post
Share on other sites
ok i fixed it, there was some problem because i had copied the project folder and started the second version. I think that everything was not getting rebuilt or something, so i deleted all the obj files, and now it works.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!