Skip to main content
gamedev.net
🔒 Locked

Dynamic Memory Allocation Problem

Started by David McCaleb Oct 16, 2000 at 6:08 PM 0 replies 400+ views
Original Post
David McCaleb
David McCaleb
Every time I try to run my program it crashes. The code conserned is here:
    
class Point
{
public:
	Point(unsigned short x = 0, unsigned short y = 0, short value = 1): itsx(x), itsy(y), itsvalue(value)  {}
	~Point() {}
	unsigned short GetItsX() {return itsx;}
	void SetItsX(unsigned short x) {itsx = x;}
	unsigned short GetItsY() {return itsy;}
	void SetItsY(unsigned short y) {itsy = y;}
	short GetItsValue() {return itsvalue;}
	void SetItsValue(short value) {itsvalue = value;}
private:
	unsigned short itsx;
	unsigned short itsy;
	short itsvalue;
};

short FreeSpaces(char * pBoard);

class MediumAI: public AI
{
public:
	MediumAI() {}
	virtual ~MediumAI() {}
	
	virtual void Turn(char * pBoard, short & cx, short & cy, char oc);

	static short i;
};

short MediumAI::i = 0;

void MediumAI::Turn(char * pBoard, short & cx, short & cy, char oc)
{
	srand(time(0));
	static bool * mem = new bool[3 * 3];
	//more declarations here

	short n = FreeSpaces(pBoard);
	static Point * pointlist = new Point[n];

	i++;

	cout << FreeSpaces(pBoard);
	
	//more code here

	
	delete [] pointlist;
	
}
    
Anyone know why this happens? Edited by - David McCaleb on 10/16/00 6:13:29 PM
Liquid
Liquid
One problem I can see is that you declare pointlist as static. By doing this, you allocate it the first time that function is called, and then delete it when the function exits. pointlist will NOT get reallocated the next time you call that function, and if you try and use it, you will get an access violation or something similar.

Hope this Helps,

-Liquid

Topic Locked

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

Sign in to reply to this topic.