Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[Question] new/delete operators
Goto page 1, 2  Next
 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
ShurikeN
Advanced Cheater
Reputation: 0

Joined: 09 Jan 2008
Posts: 84

PostPosted: Sat Jan 10, 2009 5:49 am    Post subject: [Question] new/delete operators Reply with quote

I read a book that says when you use new, you clean up with delete. When you use new[] (with brackets), you clean up with delete[] (with brackets).

but what if:
Code:

int main()
{
     player = new Player;
     player = new Player;
     player = new Player;
}

since they are not array i cannot use delete[], how do i actually delete them properly?

_________________
Code:
XXXXXX      XXXXXX
   XXXXX  XXXXX
     XXXXXXXX
    D I R E C T
     XXXXXXXX
   XXXXX  XXXXX
XXXXXX      XXXXXX
      GameDev
Back to top
View user's profile Send private message
Jani
Grandmaster Cheater
Reputation: 2

Joined: 29 Dec 2006
Posts: 804

PostPosted: Sat Jan 10, 2009 5:56 am    Post subject: Reply with quote

Code:

Player p1 = new Player;
Player p2 = new Player;
Player p3 = new Player;

delete p1;
delete p2;
delete p3;
Code:
Player p[3];
for(int i=0; i<3; ++i)
  p[i] = new Player;

for(int i=0; i<3; ++i)
  delete p[i]
Code:
Player *p = new Player[3];

delete [] p;


EDIT: Missed a [/code].
EDIT2: Moar examples.


Last edited by Jani on Sun Jan 11, 2009 6:27 am; edited 3 times in total
Back to top
View user's profile Send private message
Zerith
Master Cheater
Reputation: 1

Joined: 07 Oct 2007
Posts: 468

PostPosted: Sat Jan 10, 2009 7:40 am    Post subject: Reply with quote

Jani, i think he meant something else.

ShurikeN: You should never do a thing like that, it would be impossible to delete those since you are assigning a new portion of memory every time and lose track of the older one.
Back to top
View user's profile Send private message MSN Messenger
&Vage
Grandmaster Cheater Supreme
Reputation: 0

Joined: 25 Jul 2008
Posts: 1053

PostPosted: Sat Jan 10, 2009 4:20 pm    Post subject: Reply with quote

Isn't delete for non array variables and delete[] for arrays?
Back to top
View user's profile Send private message
Noz3001
I'm a spammer
Reputation: 26

Joined: 29 May 2006
Posts: 6220
Location: /dev/null

PostPosted: Sun Jan 11, 2009 5:48 am    Post subject: Reply with quote

You should have 2 pieces of memory allocated which you cant remove.
Back to top
View user's profile Send private message MSN Messenger
Jani
Grandmaster Cheater
Reputation: 2

Joined: 29 Dec 2006
Posts: 804

PostPosted: Sun Jan 11, 2009 6:39 am    Post subject: Reply with quote

Actually.. If you wanted to do it like that, you could build your own garbage collector in the class, similar to Java's one. I mean it'd delete itself it there's no more references to that object.

Let's say something like this:
Code:
class Player
{
private:
   static void* m_p;
public:
   Player();
};

void* Player::m_p=0;

Player::Player()
{
   if( m_p )
      delete m_p;
   m_p = this;
}

int main(int argc, char *argv[])
{
   Player *p;
   p = new Player();
   p = new Player();
   p = new Player();

   delete p;

   return 0;
}


Iono if it works, but you got the idea. Too lazy to test :p

EDIT: if you wanted to keep all the references and just delete them later:
Code:
#include <vector>
#include <iostream>

class Player
{
private:
   static std::vector<void*> *m_v;
public:
   Player() { m_v->push_back(this); }
   ~Player() {
      for(unsigned int i=m_v->size()-1; i>0; --i)
         delete m_v->at(i);
      delete m_v;
   }
   unsigned int Count() { return this->m_v->size(); }
};

std::vector<void*> *Player::m_v = new std::vector<void*>();

int main(int argc, char *argv[])
{
   Player *p;
   p = new Player();
   std::cout << p->Count() << std::endl;
   new Player();
   std::cout << p->Count() << std::endl;
   new Player();
   std::cout << p->Count() << std::endl;

   delete p;

   return 0;
}


Last edited by Jani on Sun Jan 11, 2009 7:00 am; edited 1 time in total
Back to top
View user's profile Send private message
ShurikeN
Advanced Cheater
Reputation: 0

Joined: 09 Jan 2008
Posts: 84

PostPosted: Sun Jan 11, 2009 6:45 am    Post subject: Reply with quote

Zerith, thank you, you got my point. Actually i was creating a program for about a week and when i have completed it i did some clean up and i noticed a portion of my code that does exactly what i illustrated above. It wasn't producing any error but i had a feeling that it was WRONG. so thank you for clearing that up.

Thanks also Jani, i appreciate your help.

Edit:
Wow, thanks for your code sample jani. i'll try that.

_________________
Code:
XXXXXX      XXXXXX
   XXXXX  XXXXX
     XXXXXXXX
    D I R E C T
     XXXXXXXX
   XXXXX  XXXXX
XXXXXX      XXXXXX
      GameDev
Back to top
View user's profile Send private message
Jani
Grandmaster Cheater
Reputation: 2

Joined: 29 Dec 2006
Posts: 804

PostPosted: Sun Jan 11, 2009 7:03 am    Post subject: Reply with quote

ShurikeN wrote:
Edit:
Wow, thanks for your code sample jani. i'll try that.
Hehe, np. You might want to create some at(int index) function if you want to access specific Player.

Also, it'd be way more reasonable to create a vector and allocate players there than creating a vector wrapper like this :P
Back to top
View user's profile Send private message
ShurikeN
Advanced Cheater
Reputation: 0

Joined: 09 Jan 2008
Posts: 84

PostPosted: Mon Jan 12, 2009 11:20 pm    Post subject: Reply with quote

like this?
Code:

std::vector<Player*> players;
Player *player;

player = new Player();
players.push_back( player );

_________________
Code:
XXXXXX      XXXXXX
   XXXXX  XXXXX
     XXXXXXXX
    D I R E C T
     XXXXXXXX
   XXXXX  XXXXX
XXXXXX      XXXXXX
      GameDev
Back to top
View user's profile Send private message
Skyance
Cheater
Reputation: 0

Joined: 07 Sep 2007
Posts: 46
Location: Israel

PostPosted: Tue Jan 13, 2009 3:51 am    Post subject: Reply with quote

Yes, but you can simply do:
Code:
players.push_back(new Player());

Then you don't need another temporaty pointer to the new Player object.
Back to top
View user's profile Send private message
Jani
Grandmaster Cheater
Reputation: 2

Joined: 29 Dec 2006
Posts: 804

PostPosted: Tue Jan 13, 2009 1:29 pm    Post subject: Reply with quote

ShurikeN wrote:
like this?
Yup, or just directly push the allocated object:
Code:
std::vector<Player*> players;
players.push_back( new Player() );
Back to top
View user's profile Send private message
&Vage
Grandmaster Cheater Supreme
Reputation: 0

Joined: 25 Jul 2008
Posts: 1053

PostPosted: Tue Jan 13, 2009 4:02 pm    Post subject: Reply with quote

You need to resize the vectors everytime you push_back.
Back to top
View user's profile Send private message
smartz993
I post too much
Reputation: 2

Joined: 20 Jun 2006
Posts: 2013
Location: USA

PostPosted: Tue Jan 13, 2009 4:12 pm    Post subject: Reply with quote

_void_ wrote:
You need to resize the vectors everytime you push_back.




You are scary dumb. Stop trying to help people, you can't.


push_back will resize the vector if it does not have the necessary capacity.
Back to top
View user's profile Send private message
&Vage
Grandmaster Cheater Supreme
Reputation: 0

Joined: 25 Jul 2008
Posts: 1053

PostPosted: Tue Jan 13, 2009 7:21 pm    Post subject: Reply with quote

smartz993 wrote:
_void_ wrote:
You need to resize the vectors everytime you push_back.




You are scary dumb. Stop trying to help people, you can't.


push_back will resize the vector if it does not have the necessary capacity.

I tried it already pussy I always have a capacity error.
Back to top
View user's profile Send private message
smartz993
I post too much
Reputation: 2

Joined: 20 Jun 2006
Posts: 2013
Location: USA

PostPosted: Tue Jan 13, 2009 7:42 pm    Post subject: Reply with quote

Code:
void push_back ( const T& x );




cplusplus.org wrote:
Add element at the end

Adds a new element at the end of the vector, after its current last element. The content of this new element is initialized to a copy of x.

This effectively increases the vector size by one, which causes a reallocation of the internal allocated storage if the vector size was equal to the vector capacity before the call. Reallocations invalidate all previously obtained iterators, references and pointers.


Size != Capacity.

Size is how many elements are in your vector.

The resize function will only increase capacity if you want to resize to larger than the current capacity.

The reserve function will make the capacity to at least(sometimes more) the number in the parameter you give it.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites