C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Why is this?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Mike Cox
Guest





PostPosted: Thu Jun 23, 2005 9:37 am    Post subject: Why is this? Reply with 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;
}


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





PostPosted: Thu Jun 23, 2005 11:09 am    Post subject: Re: Why is this? Reply with quote



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





PostPosted: Thu Jun 23, 2005 1:29 pm    Post subject: Re: Why is this? Reply with quote



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





PostPosted: Thu Jun 23, 2005 1:30 pm    Post subject: Re: Why is this? Reply with quote

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:

Quote:
int x = *new int;

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.

Quote:
return 0;
}


--
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





PostPosted: Thu Jun 23, 2005 11:05 pm    Post subject: Re: Why is this? Reply with quote

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





PostPosted: Fri Jun 24, 2005 7:22 am    Post subject: Re: Why is this? Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.