Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Leadorn

Alloc dealloc

This topic is 5681 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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;  
  

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
yes man, it''s the right way to do it

Share this post


Link to post
Share on other sites
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 );

Share this post


Link to post
Share on other sites
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 );

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!