| View previous topic :: View next topic |
| Author |
Message |
Mike Cox Guest
|
Posted: Thu Jun 23, 2005 9:37 am Post subject: Why is this? |
|
|
Hi.
I have a rather interesting question about the new operator.
When I have the following code it compiles when written as such:
int main()
{
int* p = *new int;
delete p;
return 0;
}
Yet when I do the same with a class, it fails!
class X {};
int main()
{
X* p = *new X;
delete X;
return 0;
}
I get a "cannot convert X* to X" error message from my compiler. Why
doesn't it say the same for the "int* p = *new int;" ?
My fix is to just write the code as:
X* p = new X;
Which compiles just fine!
P.S.
Is the following code correct in modern C++ thinking? I'm just getting
up to speed on modern C++ due to having worked with an older compiler for
legacy code the past few years.
#include <iostream>
#include <vector>
#include <string>
using std::string;
using std::vector;
using std::cout;
using std::cin;
template <class T> class X{
public:
X(){};
X(T var) : myvar(var) {};
void show()
{
cout<< myvar;
};
private:
T myvar;
};
class y{};
int main()
{
try{
X
//throw std::bad_alloc();
pmyclass->show();
delete pmyclass;
}catch(std::bad_alloc &){
cout<<"new threw";
return 1;
}
y* p = new y;
int x = *new int;
return 0;
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Gleb Alexeev Guest
|
Posted: Thu Jun 23, 2005 11:09 am Post subject: Re: Why is this? |
|
|
| Quote: | int main()
{
int* p = *new int;
delete p;
return 0;
}
|
On VC++ 2003 it doesn't compile either (as expected, because there's no
implicit conversion from int to int*).
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Thu Jun 23, 2005 1:29 pm Post subject: Re: Why is this? |
|
|
Mike Cox wrote:
| Quote: | Hi.
I get a "cannot convert X* to X" error message from my compiler. Why
doesn't it say the same for the "int* p = *new int;" ?
It does for me. Both are illegal.
My fix is to just write the code as:
X* p = new X;
|
Why would you do otherwise? new is already defined as returing X*.
Applying * to it makes the value of the expression X.
Are you a Java programmer by any chance?
| Quote: | Which compiles just fine!
P.S.
Is the following code correct in modern C++ thinking? I'm just getting
up to speed on modern C++ due to having worked with an older compiler for
legacy code the past few years.
|
It looks OK, but why bother using new at all if you don't have to.
It's a common error (especially for java-heads) to use new when it's
inappropriate.
| Quote: | int x = *new int;
This is not a good idea. This allocates an int, and then copies the |
value (unfortunately do to C++ stupidity an indeterminate one) into the
int called x. The address of the allocated object is lost (and hence
it is never possible to deallocate that object).
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Clark S. Cox III Guest
|
Posted: Thu Jun 23, 2005 1:30 pm Post subject: Re: Why is this? |
|
|
On 2005-06-23 05:37:00 -0400, Mike Cox <mikecoxlinux (AT) yahoo (DOT) com> said:
| Quote: | Hi.
I have a rather interesting question about the new operator.
When I have the following code it compiles when written as such:
int main()
{
int* p = *new int;
delete p;
return 0;
}
|
If that compiles, then your compiler is broken. That code is wrong, for
the same reasons that the following code is (i.e. you're trying to
implicitly convert a value of type (int) to (int*) ):
int main()
{
int i;
int *p = i;
return 0;
}
| Quote: | P.S.
Is the following code correct in modern C++ thinking? I'm just getting
up to speed on modern C++ due to having worked with an older compiler for
legacy code the past few years.
#include <iostream
#include
#include
using std::string;
using std::vector;
using std::cout;
using std::cin;
template
|
I would prefer "template <typename T>" to "template <class T>", but
that is purely a style issue.
| Quote: | public:
X(){};
X(T var) : myvar(var) {};
void show()
{
cout<< myvar;
};
private:
T myvar;
};
class y{};
int main()
{
try{
X
//throw std::bad_alloc();
pmyclass->show();
delete pmyclass;
}catch(std::bad_alloc &){
cout<<"new threw";
return 1;
}
y* p = new y;
|
There's a problem with this line:
You're allocating an int via new, assigning the contents of that int to
a new int variable, and promptly forgetting about the pointer. There is
now no way that you can ever use or delete that int; you've just leaked
memory.
--
Clark S. Cox, III
[email]clarkcox3 (AT) gmail (DOT) com[/email]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
tesfaye Guest
|
Posted: Thu Jun 23, 2005 11:05 pm Post subject: Re: Why is this? |
|
|
| Quote: | y* p = new y;
int x = *new int;
|
The first one is correct usage of the new operator.
But in the second one, what is your intention there? What do you want x
to contain?
The new operator always returns a pointer to the lowest byte address of
the newly allocated storage.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Old Wolf Guest
|
Posted: Fri Jun 24, 2005 7:22 am Post subject: Re: Why is this? |
|
|
Mike Cox wrote:
| Quote: | Hi.
I have a rather interesting question about the new operator.
When I have the following code it compiles when written as such:
int main()
{
int* p = *new int;
delete p;
return 0;
}
|
That code is illegal. If it works for you, I can only guess
that the compiler has decided that *new int is 0, and is creating
p as a null pointer. (Try displaying p and see what you get).
What compiler do you have?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|