Advertisement

Memory allocation problems

Started by October 02, 2000 11:08 AM
2 comments, last by robinei 24 years, 3 months ago
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)
I defined the Cells pointer like this: TCell **Cells;
Advertisement
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:
    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
Thank you man, it worked!

This topic is closed to new replies.

Advertisement