Click here to Skip to main content
15,894,539 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralYou are welcome. Pin
CPallini29-Sep-08 21:49
mveCPallini29-Sep-08 21:49 
Questiondynamic allocation of arrays [modified] Pin
Sauce!28-Sep-08 19:14
Sauce!28-Sep-08 19:14 
AnswerRe: dynamic allocation of arrays Pin
Saurabh.Garg28-Sep-08 19:23
Saurabh.Garg28-Sep-08 19:23 
GeneralRe: dynamic allocation of arrays Pin
Sauce!28-Sep-08 19:28
Sauce!28-Sep-08 19:28 
GeneralRe: dynamic allocation of arrays [modified] Pin
Sauce!29-Sep-08 21:01
Sauce!29-Sep-08 21:01 
GeneralRe: dynamic allocation of arrays Pin
Iain Clarke, Warrior Programmer30-Sep-08 0:20
Iain Clarke, Warrior Programmer30-Sep-08 0:20 
GeneralRe: dynamic allocation of arrays Pin
Sauce!30-Sep-08 0:55
Sauce!30-Sep-08 0:55 
GeneralRe: dynamic allocation of arrays Pin
Iain Clarke, Warrior Programmer30-Sep-08 1:25
Iain Clarke, Warrior Programmer30-Sep-08 1:25 
On the code above, I'm quite surprised at the delete [] failing, unless there is a problem inside the destructor for Tile.

Have you tried making a test app, and tried Tile as a very simple class (ie, just an int), and implicit constructor / destructor?

Also put a breakpoint on your allocation and deallocation part, and make sure you haven't messed up the address of p_TileArray.

Lastly, I can see one other bug, which is in place many times.

for(int i = 0; i < (sizeof(p_TileArray) / sizeof(p_TileArray[0])); i++)


As m_pTileArray is a pointer, sizeof(p_TileArray) will be 4 on a 32 bit system, and 8 on a 64 one. I'm assuming sizeof (Tile) is larger, so your loops will never work. When you weren't dynamically allocating Tiles, and had Tile p_TileArray[4]; (I'm guessing), then the sizeof trick would work nicely.

You;'ll need a separate member variable to tell the loops how many you have.

eg:
p_TileArray = NULL;
n_TileArray = 4;
p_TileArray = new Tile[n_TileArray];


You may also want to use array structures to hold the tiles, which will clear things up.

In block.h
    CArray<Tile, Tile &> m_Tiles;

In block.cpp
  constructor
   m_Tiles.SetSize (4);
   m_Tiles [0].Set....

...
void Block::Render()
{
	for(int i = 0; i < m_Tiles.GetSize (); i++)
	{
		m_Tiles[i].Render();
	}	
}


If you prefer, you could use std::vector instead. Either choice frees you from needing to delete the dynamic "array", and may help you.


Good luck,

Iain.
GeneralRe: dynamic allocation of arrays Pin
Sauce!30-Sep-08 1:34
Sauce!30-Sep-08 1:34 
GeneralRe: dynamic allocation of arrays Pin
Iain Clarke, Warrior Programmer30-Sep-08 1:48
Iain Clarke, Warrior Programmer30-Sep-08 1:48 
GeneralRe: dynamic allocation of arrays Pin
Sauce!30-Sep-08 2:16
Sauce!30-Sep-08 2:16 
GeneralRe: dynamic allocation of arrays Pin
Iain Clarke, Warrior Programmer30-Sep-08 3:21
Iain Clarke, Warrior Programmer30-Sep-08 3:21 
GeneralRe: dynamic allocation of arrays [modified] Pin
Sauce!30-Sep-08 5:01
Sauce!30-Sep-08 5:01 
GeneralRe: dynamic allocation of arrays Pin
Iain Clarke, Warrior Programmer30-Sep-08 5:31
Iain Clarke, Warrior Programmer30-Sep-08 5:31 
GeneralRe: dynamic allocation of arrays Pin
Sauce!30-Sep-08 5:43
Sauce!30-Sep-08 5:43 
GeneralRe: dynamic allocation of arrays Pin
Iain Clarke, Warrior Programmer30-Sep-08 6:02
Iain Clarke, Warrior Programmer30-Sep-08 6:02 
GeneralRe: dynamic allocation of arrays Pin
Sauce!2-Oct-08 5:05
Sauce!2-Oct-08 5:05 
GeneralRe: dynamic allocation of arrays [modified] Pin
Sauce!4-Oct-08 2:42
Sauce!4-Oct-08 2:42 
GeneralRe: dynamic allocation of arrays Pin
Sauce!4-Oct-08 5:19
Sauce!4-Oct-08 5:19 
GeneralRe: dynamic allocation of arrays [modified] Pin
Sauce!4-Oct-08 5:34
Sauce!4-Oct-08 5:34 
GeneralRe: dynamic allocation of arrays [modified] Pin
Sauce!4-Oct-08 12:53
Sauce!4-Oct-08 12:53 
GeneralRe: dynamic allocation of arrays Pin
Sauce!17-Oct-08 20:58
Sauce!17-Oct-08 20:58 
Questionno WM_MOUSEHOVER in an edit Pin
followait28-Sep-08 18:58
followait28-Sep-08 18:58 
AnswerRe: no WM_MOUSEHOVER in an edit Pin
Iain Clarke, Warrior Programmer30-Sep-08 0:25
Iain Clarke, Warrior Programmer30-Sep-08 0:25 
QuestionCListCtrl Problem Pin
Dhiraj kumar Saini28-Sep-08 18:53
Dhiraj kumar Saini28-Sep-08 18:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.