Skip to main content
gamedev.net
🔒 Locked

Alloc dealloc

Started by Leadorn 6 replies 250+ views
Original Post
Leadorn
Leadorn
Ive tried this q in the beginner pool but cant get an answer. If I use this code for my 2d array, is this the correct way to deallocate the array?
  
alloc
int **Map;
Map = new int*[ARRAY_WIDTH];
for (int i = 0; i < ARRAY_WIDTH; i++)
    Map[ i ] = new int[ARRAY_HEIGHT];

dealloc
for (unsigned int i = 0; i < ARRAY_WIDTH; i++)	
{			
delete [] Map[i];
}
delete [] Map;  
  
prgrmmr
prgrmmr
I dont''t like ''new'', but this is correct.


    
//alloc

int **Map;
Map = (INT**)malloc( ARRAY_WIDTH*sizeof(INT*) );
for (int i = 0; i < ARRAY_WIDTH; i++)
Map[i] = (INT*)malloc( ARRAY_WIDTH*sizeof(INT) );

//dealloc

for (unsigned int i = 0; i < ARRAY_WIDTH; i++)
{
free( Map[i] );
}
free( Map );

prgrmmr
prgrmmr
I dont''t like ''new'', but this is correct.


    
//alloc

int **Map;
Map = (INT**)malloc( ARRAY_WIDTH*sizeof(INT*) );
for (int i = 0; i < ARRAY_WIDTH; i++)
Map[i] = (INT*)malloc( ARRAY_WIDTH*sizeof(INT) );

//dealloc

for (unsigned int i = 0; i < ARRAY_WIDTH; i++)
{
free( Map[i] );
}
free( Map );

Kippesoep
Kippesoep
If you don't want to bother with the for-loops, but still want a 2D array, try this:

MATRIX.H

  
template <class T, int columns>
class MATRIX
{
public :
MATRIX (T *data)
{
_data = data;
}

T *operator [] (int row)
{
return _data + (row * columns);
}

protected :
T *_data;
};


and in your code, include MATRIX.H and do something like


  
int *Map1D = new int [ARRAY_WIDTH * ARRAY_HEIGHT];
MATRIX <int, ARRAY_HEIGHT> Map (Map1D);

//Here, you can use Map just as your array of arrays:

Map [10][15] = 1;

//Then, just free it like this:

delete [] Map1D;




[edited by - Kippesoep on January 31, 2003 7:53:33 AM]
Kippesoep
Leadorn
Leadorn
And this reminds my about another problem I have. I've created a class cNode. I’ve done it with templates so I can store any kind of type. It all works good, but when I want to use it in my big project I get link problems.

error LNK2001: unresolved external symbol "public: __thiscall cNode::~cNode(void)" (??1?$cNode@H@@QAE@XZ)

unresolved external symbol "public: __thiscall cNode::cNode(void)" (??0?$cNode@H@@QAE@XZ)

I've tried it out and it works in my test project with only it in it.


This is my first time making a template, any suggestions for changes are appreciated.


    
I use it like this, cNode<int> View;

///Header

template <class Type>
class cNode
{
public:
struct sNode
{
Type *item;
sNode *Next;
};

cNode();
~cNode();
bool Add(Type *item);
bool GetItem(Type **item);
void ResetCount();

private:
//Front of linked list.

sNode *Front;
sNode *End;

sNode *NodePitch;
bool bLocked;
};

//cpp

template <class Type>
cNode<Type>::cNode()
{
End = Front = 0;
bLocked = false;
}


template <class Type>
cNode<Type>::~cNode()
{
sNode *Pitch;
sNode *TempPitch;

Pitch = Front;

while(Pitch != 0)
{

TempPitch = Pitch;
Pitch = Pitch->Next;

delete TempPitch->item;
delete TempPitch;
}//End of while(ChannelPitch != 0)


}




template <class Type>
bool cNode<Type>::Add(Type *item)
{
if(bLocked == true) return false;
if (item == 0) return false;

if(Front == 0)
{
Front = new sNode;
if(Front == 0) return false;
Front->Next = 0;
End = Front;
//Set input.

End->item = item;
NodePitch = Front;

return true;
}
else
{
End->Next = new sNode;
if(End->Next == 0) return false;
End->Next->Next = 0;
End = End->Next;
//Set input.

End->item = item;
return true;
}
return false;
}

template <class Type>
bool cNode<Type>::GetItem(Type **item)
{
//Lock class.

bLocked = true;

if(NodePitch == 0) return false;


*item = NodePitch->item;


NodePitch = NodePitch->Next;
return true;
}

template <class Type>
void cNode<Type>::ResetCount()
{

NodePitch = Front;


}


[edited by - leadorn on January 31, 2003 1:30:10 PM]
Tron3k
Tron3k
I think your problem is that template code always has to be in the header file.
“[The clergy] believe that any portion of power confided to me, will be exerted in opposition to their schemes. And they believe rightly: for I have sworn upon the altar of God, eternal hostility against every form of tyranny over the mind of man” - Thomas Jefferson
Leadorn
Leadorn
aha. so i have to put all the code for the class in the header file. Oki ill try that.

Topic Locked

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

Sign in to reply to this topic.