Archived

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

*new -vs- new

This topic is 5399 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
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
Unless you''ve taken a reference to it, but that''s still a stupid thing to do :

int& i = *new int;
...
delete &i;



[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

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