Skip to main content
gamedev.net
🔒 Locked

Memory allocation problems

Started by Absolution 6 replies 400+ views
Original Post
Absolution
Absolution
I guess I don''t understand memory allocation in C++ as well as I though I did. Here is my problem. I am implementing a tile-based game in DirectX. I have a global map variable consisting of an array of node classes: Cnode *map; In my initialization I allocate memory like this: ... Allocate Surfaces for bitmaps ... map=new Cnode[mapx*mapy]; This was working until I started trying to allocate more surfaces for bitmaps. My program would no longer get past initialization unless I decreased the size of my map. This seemed strange to me since the game has nothing in it right now and the Cnode class consists of 1 variable and a constructor at this point. Also my computer has 256mb of ram so I know I haven''t used it up. Anyway, if I instead declare the map like this: Cnode map[SOME_CONSTANT] I have no problems. I should know this, but why is it giving me trouble when I try and allocate it dynamically during my initialization, but if I declare it with a constant globally everything is fine? Abs p.s. Sorry, I know I have posted this in two different forums, but I didn''t mean to post it in Graphics programming.
RavNaz
RavNaz
try this (not sure if it will work):

#define mapx 10
#define mapy 10
#define map_size mapx * mapy

Cnode *map = NULL;

map = new map[map_size];



CTRL_ALT_DELETE
CTRL_ALT_DELETE
I could be way off, but don''t you have to call a constructor for each CNode object? something like

for(int i = 0; i < mapsize; i++)
map[x] = new CNode();

???
Forgive me if I totally misread your post.
Martee
Martee
I think you are right ...
Doesn''t map = new Cnode[n] only allocate space for n pointers, not the objects themselves?

~~~~~~~~~~
Martee
http://www.csc.uvic.ca/~mdill
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
Qoy
Qoy
No.. It's declared as a pointer to a CNode, so to create the array you should just have to allocate it like:

map = new CNode[numObjects];

Martee: what you suggested is (I believe) the way it works in Java, but not C++.

It looks right to me.. There must be a problem deleting or something. Make sure you only allocate objects once before deleting them, and make sure to only delete them once as well. Also, always set things to NULL (0) after deleting them, as this will help to find bugs.

The program will crash if you delete things more than once.. and it might eventually crash if you allocate things multiple times without deleting them.

What kind of crash are you getting? Are you using the debugger? If so, where is it breaking?

Edited by - Qoy on April 16, 2001 1:50:28 AM
Martee
Martee
Aah!
You are right, of course. CSC 115 really gets to you (thanks Java ).

~~~~~~~~~~
Martee
http://www.csc.uvic.ca/~mdill
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.