Memory allocation problems
In my game I dynamically allocate the maps.
There''s a problem though. It crashes when I try to do it.
Here''s the code I use:
int TMap::Create(short w, short h)
{
int x, y;
// Allocate the map
try
{
*Cells = new TCell[w];
for(x = 0; x < w; x++)
Cells[x] = new TCell[h];
}
catch(std::bad_alloc xa)
{
cerr << "Memory allocation failure...\n";
exit(1);
}
// Set initial cell values
for(y = 0; y < h; y++)
for(x = 0; x < w; x++)
{
Cells[x][y].Tile = 0;
}
// Store the width an height of the map
Width = w;
Height = h;
// Success
return 0;
}
What do I do wrong? I use SDL under Visual C++.
This error message shows up in the stderr.txt file:
Fatal signal: Segmentation Fault (SDL Parachute Deployed)
Cells is an array of pointers, right?
*Cells = new TCell[W];
that creates an array of TCells, not of TCell pointers. also, it''s the same as:
Cells[0] = new TCell[W];
i think what you want to do is this:
------------------------
IUnknown *pUnkOuter
*Cells = new TCell[W];
that creates an array of TCells, not of TCell pointers. also, it''s the same as:
Cells[0] = new TCell[W];
i think what you want to do is this:
Cells = new TCell*[W];for(int cnt=0; cnt<W; cnt++){ Cells[cnt] = new TCell[h];
------------------------
IUnknown *pUnkOuter
------------------------IUnknown *pUnkOuter"Try the best you cantry the best you canthe best you can is good enough" --Radiohead
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement