Which way is better?



 DEVELOP > c-Plus-Plus > Which way is better?

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: ""
Date: 11 Jan 2008 12:16:28 AM
Object: Which way is better?
int n;
int *p1;
p1 = &n;
or
int *p, *p1;
p = new int;
p1 = p;
The above two ways to assign pointer p1, which one is better?
Jack
.

User: "Alf P. Steinbach"

Title: Re: Which way is better? 11 Jan 2008 12:48:10 AM
* junw2000@gmail.com:

int n;
int *p1;
p1 = &n;

or

int *p, *p1;
p = new int;
p1 = p;

The above two ways to assign pointer p1, which one is better?

I only see one way demonstrated above, using "=".
Additionally, without requirements the phrase "better" is meaningless.
Cheers, & hth.,
- Alf
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
.

User: "Daniel T."

Title: Re: Which way is better? 11 Jan 2008 09:37:21 PM
wrote:

int n;
int *p1;
p1 = &n;

or

int *p, *p1;
p = new int;
p1 = p;

The above two ways to assign pointer p1, which one is better?

You should use new when: the lifetime of the object isn't known at
compile time, the sub-type of the object isn't known at compile time, or
(in the case of arrays) the number of objects needed aren't known at
compile time. Otherwise, don't use new. (I think that's all of them...)
.

User: "Howard"

Title: Re: Which way is better? 11 Jan 2008 07:56:37 PM

int n;
int *p1;
p1 = &n;

or

int *p, *p1;
p = new int;
p1 = p;

I suspect most people here would prefer the first, since you don't have to
manage the memory yourself. There's rarely a reason to "new" a single
integer. But even with a more complex object, it's much easier to work with
automatic memory than with dynamic allocation. Unless you really need to
use new, why bother?
One other thing. It's also usually better to initialize an object at the
point of declaration, when possible, like this:
X* p = new X;
X* p1 = p;
-Howard
.

User: ""

Title: Re: Which way is better? 11 Jan 2008 12:52:30 AM
On Jan 10, 10:16 pm,
wrote:

int n;
int *p1;
p1 = &n;

In the case above, there is an automatic object called 'n', whose
lifetime is controlled by the compiler. That object's life ends
"automatically." This might happen at the end of the scope where the
object is defined.
p1 cannot be used after n dies; but as written above, p1's life ends
before n anyway. (Automatic objects are destroyed in the reverse order
of their construction.)

int *p, *p1;
p = new int;
p1 = p;

In the case above, the lifetime of the object is determined by the
programmer. That object (what p points to) will live until 'delete' is
called on it.
In this case, *p may live longer that p1.

The above two ways to assign pointer p1, which one is better?

If automatic destruction is fine, then the first one is better;
because the lifetime is handled for us. If the object must live longer
for any reason, then the second one is a must.
Ali
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER