Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > new of an object without assigning it to any variable

Reply
Thread Tools

new of an object without assigning it to any variable

 
 
ajay
Guest
Posts: n/a
 
      04-13-2004
Why would a new of object be created without assigning it to any of variable?

new A;

???
tx
 
Reply With Quote
 
 
 
 
Buster
Guest
Posts: n/a
 
      04-13-2004
ajay wrote:

> Why would a new of object be created without assigning it to any of variable?
>
> new A;
>
> ???
> tx


For side effects.

--
Regards,
Buster.
 
Reply With Quote
 
 
 
 
Siemel Naran
Guest
Posts: n/a
 
      04-13-2004
"Buster" <(E-Mail Removed)> wrote in message
news:c5g2ac$2ld$(E-Mail Removed)...

> > Why would a new of object be created without assigning it to any of

variable?
> >
> > new A;
> >
> > ???
> > tx

>
> For side effects.


It's a memory leak in C++.


 
Reply With Quote
 
Corno
Guest
Posts: n/a
 
      04-13-2004
> > >
> > > new A;
> > >

> It's a memory leak in C++.
>

It is, but this could very well not be:

new A(b);

Corno


 
Reply With Quote
 
Buster
Guest
Posts: n/a
 
      04-13-2004
Siemel Naran wrote:

> "Buster" wrote
>
>>>Why would a new of object be created without assigning it to
>>>any of variable?
>>>
>>>new A;
>>>
>>>???
>>>tx

>>
>>For side effects.

>
> It's a memory leak in C++.


Not necessarily.

--
Regards,
Buster.
 
Reply With Quote
 
Siemel Naran
Guest
Posts: n/a
 
      04-13-2004
"Buster" <(E-Mail Removed)> wrote in message
news:c5g38k$rj4$(E-Mail Removed)...
> Siemel Naran wrote:


> >>>new A;


> > It's a memory leak in C++.

>
> Not necessarily.


If you overload global operator new, etc?


 
Reply With Quote
 
Siemel Naran
Guest
Posts: n/a
 
      04-13-2004
"Corno" <Corno@dds%FAKE%.nl> wrote in message
news:c5g38i$uc$(E-Mail Removed)...

> > > > new A;
> > > >

> > It's a memory leak in C++.
> >

> It is, but this could very well not be:
>
> new A(b);


Perhaps so. But we still ought not to be writing code like this in C++. OK
in Java and other garbage collected languages.


 
Reply With Quote
 
Buster
Guest
Posts: n/a
 
      04-13-2004
Siemel Naran wrote:
> "Buster" wrote
>>Siemel Naran wrote:

>
>>>>> new A;
>>>
>>> It's a memory leak in C++.

>>
>> Not necessarily.

>
> If you overload global operator new, etc?


Or if an exception will be thrown during construction.

--
Regards,
Buster.
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      04-13-2004
* "Corno" <Corno@dds%FAKE%.nl> schriebt:
> > > >
> > > > new A;
> > > >

> > It's a memory leak in C++.
> >

> It is, but this could very well not be:
>
> new A(b);


There is nothing in the latter expression that makes it less likely
to be a memory leak.

Not that a new-expression by itself is _necessarily_ a leak.

But you have to use extremely contorted mechanisms to avoid a leak, and
in that case it's much easier to use e.g. 'std::auto_ptr<A>( new A );',
or, if class A does not depend on dynamic allocation, simply 'A();'.

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
Buster
Guest
Posts: n/a
 
      04-13-2004
Corno wrote:

>>>>new A;

>>
>>It's a memory leak in C++.

>
> It is, but this could very well not be:
>
> new A(b);


Do you mean "new (b) A;"?

--
Regards,
Buster.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
501 PIX "deny any any" "allow any any" Any Anybody? Networking Student Cisco 4 11-16-2006 10:40 PM
Assigning methods to objects, and assigning onreadystatechange to an XMLHttpRequest -- an inconsistency? weston Javascript 1 09-22-2006 09:33 AM
if instance variable get initialize after assigning some values or after constructor then when does static variable get initialize Tony Morris Java 3 02-04-2006 08:39 AM
Slice returned hash or access to its elements without assigning to a variable Andrew Perl Misc 9 06-14-2005 05:09 PM
javascript - Assigning an object to Session variable RA ASP .Net 4 11-15-2004 07:59 PM



Advertisments