Jump to content
  • Advertisement

Archived

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

sab3156

*new -vs- new

This topic is 5586 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

what is the difference between
*new 
and
new 
?? i understand by using the
new 
will be used as pointer allocation, but is it essentially the same as
*new 
??

Share this post


Link to post
Share on other sites
Advertisement
I imagine that *new would dereference the pointer new returns, and in the process create a memory leak since we have allocated memory with no pointer to delete it with later.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Not if you use a garbage collector.

Share this post


Link to post
Share on other sites
I guess you''ve seen this somewhere otherwise you wouldn''t be asking. What was the context you saw it in?


  
#include <iostream>

int main() {
using std::cout;
using std::endl;

int* sheep = new int(5);

cout << "address of sheep " << sheep << endl;
cout << "number of sheep " << *sheep << endl;

// int dog = *new int(1);// memory leak

int* dog = new int(1);
int spit = *dog;

cout << "address of dog " << dog << endl;
cout << "number of dog " << *dog << endl;

cout << "number of spit " << spit << endl;
cout << "address of spit " << &spit << endl;

delete sheep;
delete dog;
}

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!