new of an object without assigning it to any variable

Discussion in 'C++' started by ajay, Apr 13, 2004.

  1. ajay

    ajay Guest

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

    new A;

    ???
    tx
     
    ajay, Apr 13, 2004
    #1
    1. Advertising

  2. ajay

    Buster Guest

    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.
     
    Buster, Apr 13, 2004
    #2
    1. Advertising

  3. ajay

    Siemel Naran Guest

    "Buster" <> wrote in message
    news:c5g2ac$2ld$...

    > > 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++.
     
    Siemel Naran, Apr 13, 2004
    #3
  4. ajay

    Corno Guest

    > > >
    > > > new A;
    > > >

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

    It is, but this could very well not be:

    new A(b);

    Corno
     
    Corno, Apr 13, 2004
    #4
  5. ajay

    Buster Guest

    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.
     
    Buster, Apr 13, 2004
    #5
  6. ajay

    Siemel Naran Guest

    "Buster" <> wrote in message
    news:c5g38k$rj4$...
    > Siemel Naran wrote:


    > >>>new A;


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

    >
    > Not necessarily.


    If you overload global operator new, etc?
     
    Siemel Naran, Apr 13, 2004
    #6
  7. ajay

    Siemel Naran Guest

    "Corno" <Corno@dds%FAKE%.nl> wrote in message
    news:c5g38i$uc$...

    > > > > 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.
     
    Siemel Naran, Apr 13, 2004
    #7
  8. ajay

    Buster Guest

    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.
     
    Buster, Apr 13, 2004
    #8
  9. * "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?
     
    Alf P. Steinbach, Apr 13, 2004
    #9
  10. ajay

    Buster Guest

    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.
     
    Buster, Apr 13, 2004
    #10
  11. ajay wrote:

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


    It wouldn't, in my code.

    One exception -- placement new:

    new (some_address) A;

    No need to save the address in this case, because you already know it.

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
     
    Kevin Goodsell, Apr 13, 2004
    #11
  12. Le mardi 13 avril 2004 à 09:05, Siemel Naran a écrit dans
    comp.lang.c++ :

    >>>>>new A;

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

    >>
    >> Not necessarily.

    >
    > If you overload global operator new, etc?


    "A" could be a singleton class with a static member to store the address
    of the constructed instance. Something like this:

    // Untested code below

    class A
    {
    private:
    static A *pA;

    public:
    A()
    {
    if (NULL == pA)
    {
    pA = this;
    }

    // (re)set pA->members...
    }

    ~A()
    {
    if (this == pA)
    {
    // clean pA->members...
    pA = NULL;
    }
    }

    A *instance()
    {
    return pA;
    }
    };

    A *A::pA = NULL;

    int main()
    {
    new A;

    // do stuff

    delete A::instance();

    return 0;
    }


    --
    ___________ 2004-04-13 10:36:56
    _/ _ \_`_`_`_) Serge PACCALIN -- sp ad mailclub.net
    \ \_L_) Il faut donc que les hommes commencent
    -'(__) par n'être pas fanatiques pour mériter
    _/___(_) la tolérance. -- Voltaire, 1763
     
    Serge Paccalin, Apr 13, 2004
    #12
  13. ajay

    ajay Guest

    Tx Serge,
    ur answer seems to be reasonable.
     
    ajay, Apr 13, 2004
    #13
  14. ajay

    Allan Bruce Guest

    "ajay" <> wrote in message
    news:...
    > Why would a new of object be created without assigning it to any of

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


    I use it when adding items to a hashtable that I've made. The hastable
    requires a class that inherits MyNullClass. To add to the hastable I use
    this:

    bool Put(int xiKey, MyNullClass *xiIn);

    For storing a class of type FOO, then i use something like

    class FooDataItem : public MyNullClass
    {
    public:
    FooDataItem(Foo *xiIn){mFooItem = xiIn;}
    virtual ~FooDataItem(){}
    protected:
    Foo *mFooItem;
    };

    Now, when I add an item to the HT, I dont want to store the FooDataItem, so
    I add like this:

    int key;
    Foo *ABC;
    // allocate mem and do some stuff with foo
    MyHt->Put(key, new FooDataItem(ABC));

    The last line calls new but doesnt assign it to any variable (it actually
    doeswithin the hash table, but it doesnt appear to).
    The line could be re-written as:

    FooDataItem *lFDI = new FooDataItem(ABC);
    MyHt->Put(key, lFDI);

    Which is a bit messier in my opinion. I guess this comes from writing
    programs in java.
    HTH
    Allan
     
    Allan Bruce, Apr 13, 2004
    #14
  15. In message <c5g3mc$rj4$>, Buster
    <> writes
    >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.
    >

    Or if its constructor registers it with some other object which takes
    responsibility for deleting it.

    --
    Richard Herring
     
    Richard Herring, Apr 13, 2004
    #15
  16. ajay

    Bill Seurer Guest

    Richard Herring wrote:

    > In message <c5g3mc$rj4$>, Buster
    > <> writes
    >
    >> 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.
    >>

    > Or if its constructor registers it with some other object which takes
    > responsibility for deleting it.


    Or if it just needs to hang around until the program completes for some
    reason.
     
    Bill Seurer, Apr 13, 2004
    #16
  17. ajay

    Leor Zolman Guest

    On Tue, 13 Apr 2004 07:13:26 GMT, (Alf P. Steinbach) wrote:

    >* "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();'.


    I've been trying to think of a way that any /statement/ of the form:

    new A(anything-or-nothing);

    (that would include /both/ Siemel and Corno's examples) would not be a leak
    (assuming it doesn't fail or somehow get optimized away), and haven't been
    able to. Note that in these cases, the expression stands alone as an
    expression statement; there's no additional chicanery. And note also that
    this does not contradict what you said above; I'm just contrasting your
    comments with the language of the OP and those early responses in order to
    try to plug a possible point of confusion.

    Even if class A has no base classes or non-static data, aren't objects
    obliged to have size >= 1? So no matter what you do, isn't at least that
    one byte going to hanging over you up to program termination?

    Consider this program (output follows, showing results of the assigned
    "new" operations with and without the commented out raw "new" in between
    them):

    #include <iostream>
    using namespace std;

    class A {};

    int main()
    {
    cout << "Without a new in between: " << endl;
    A *p1 = new A();
    // new A();
    A *p2 = new A();

    cout << "p1's at: " << p1 << ", and p2's at: " << p2 << endl;
    return 0;
    }

    d:\src\learn>size
    With a new in between:
    p1's at: 3293648, and p2's at: 3293696

    d:\src\learn>size
    Without a new in between:
    p1's at: 3293648, and p2's at: 3293672

    Of course there are no "guarantees" about where memory comes from and in
    what order using "new", but I think this illustrates the point.
    -leor

    --
    Leor Zolman --- BD Software --- www.bdsoft.com
    On-Site Training in C/C++, Java, Perl and Unix
    C++ users: download BD Software's free STL Error Message Decryptor at:
    www.bdsoft.com/tools/stlfilt.html
     
    Leor Zolman, Apr 13, 2004
    #17
  18. * Leor Zolman <> schriebt:
    > On Tue, 13 Apr 2004 07:13:26 GMT, (Alf P. Steinbach) wrote:
    >
    > >* "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();'.

    >
    > I've been trying to think of a way that any /statement/ of the form:
    >
    > new A(anything-or-nothing);
    >
    > (that would include /both/ Siemel and Corno's examples) would not be a leak
    > (assuming it doesn't fail or somehow get optimized away), and haven't been
    > able to.


    Well the simplest one has already been mentioned by others: if A::A() throws.

    But that wasn't what I meant by contorted.

    Here's a contorted scheme:


    class A;

    class Cleanup
    {
    public:
    ~Cleanup(){ set( 0 ); }
    static void set( A* pObj ){ delete myP; myP = pObj; }
    private:
    static A* myP;
    };

    A* Cleanup::myP = 0;


    class A
    {
    public:
    A(){ Cleanup::set( this ); }
    };


    int main()
    {
    Cleanup aCleaner;
    new A;
    }


    Here class Cleanup could conceivably be one of those dreaded "Manager"
    singleton classes or some such, where objects install themselves --
    which I think is very bad design, but it happens.


    > Note that in these cases, the expression stands alone as an
    > expression statement; there's no additional chicanery.


    What I meant by extremely contorted mechanism was chicanery like the
    above.


    > And note also that
    > this does not contradict what you said above; I'm just contrasting your
    > comments with the language of the OP and those early responses in order to
    > try to plug a possible point of confusion.


    Heh. I'm often wrong. I like to boast that I'm wrong in less than 50% of
    cases, but.


    > Even if class A has no base classes or non-static data, aren't objects
    > obliged to have size >= 1? So no matter what you do, isn't at least that
    > one byte going to hanging over you up to program termination?


    If there is no cleanup mechanism, yes, in the sense that one address from
    the address space continues to be reserved. Not necessarily in the sense
    that that address could have been used for a non-zero size object. But I
    think that amounts to the same thing.

    --
    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?
     
    Alf P. Steinbach, Apr 13, 2004
    #18
  19. On Tue, 13 Apr 2004 13:11:50 +0000, Leor Zolman wrote:


    > I've been trying to think of a way that any /statement/ of the form:
    >
    > new A(anything-or-nothing);
    >
    > would not be a leak


    The constructor of A _may_ store the address of the new object somewhere
    for later deletion. But in that case I would write the code as:

    new A; /* Not a leak, A's constructor registers the address with <xyz> so
    it will be deleted automatically when <zxy> */

    Atleast if someone else migt look at (or maintain) the code.

    Wether doing this is good design is a different debate. (My answer would
    be, "it depends")

    --
    NPV

    "the large print giveth, and the small print taketh away"
    Tom Waits - Step right up
     
    Nils Petter Vaskinn, Apr 13, 2004
    #19
  20. ajay

    Leor Zolman Guest

    On Tue, 13 Apr 2004 13:29:59 GMT, (Alf P. Steinbach) wrote:

    >>
    >> I've been trying to think of a way that any /statement/ of the form:
    >>
    >> new A(anything-or-nothing);
    >>
    >> (that would include /both/ Siemel and Corno's examples) would not be a leak
    >> (assuming it doesn't fail or somehow get optimized away), and haven't been
    >> able to.

    >
    >Well the simplest one has already been mentioned by others: if A::A() throws.


    Well, that's kinda what I meant by "doesn't fail" above ;-)

    >
    >But that wasn't what I meant by contorted.
    >
    >Here's a contorted scheme:


    [ not-that-unreasonable-of-a-contorted-scheme snipped ]

    Oh yeah, that's kinda cool actually...

    >
    >Here class Cleanup could conceivably be one of those dreaded "Manager"
    >singleton classes or some such, where objects install themselves --
    >which I think is very bad design, but it happens.


    Okay, perhaps /not/ so cool...

    >
    >
    >> Note that in these cases, the expression stands alone as an
    >> expression statement; there's no additional chicanery.


    Right you are.

    >
    >What I meant by extremely contorted mechanism was chicanery like the
    >above.
    >
    >
    >> And note also that
    >> this does not contradict what you said above; I'm just contrasting your
    >> comments with the language of the OP and those early responses in order to
    >> try to plug a possible point of confusion.

    >
    >Heh. I'm often wrong. I like to boast that I'm wrong in less than 50% of
    >cases, but.
    >
    >
    >> Even if class A has no base classes or non-static data, aren't objects
    >> obliged to have size >= 1? So no matter what you do, isn't at least that
    >> one byte going to hanging over you up to program termination?

    >
    >If there is no cleanup mechanism, yes, in the sense that one address from
    >the address space continues to be reserved. Not necessarily in the sense
    >that that address could have been used for a non-zero size object. But I
    >think that amounts to the same thing.


    Not sure what you're trying to say exactly in those last two sentences, but
    I'm not particularly worried about it. Thanks for the insight,
    -leor


    --
    Leor Zolman --- BD Software --- www.bdsoft.com
    On-Site Training in C/C++, Java, Perl and Unix
    C++ users: download BD Software's free STL Error Message Decryptor at:
    www.bdsoft.com/tools/stlfilt.html
     
    Leor Zolman, Apr 13, 2004
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. RA
    Replies:
    4
    Views:
    4,699
  2. , India
    Replies:
    1
    Views:
    700
    Pascal J. Bourguignon
    Apr 22, 2008
  3. Richard Lionheart
    Replies:
    27
    Views:
    439
    Jean-Hugues ROBERT
    May 4, 2004
  4. Andrew
    Replies:
    9
    Views:
    141
    Brian McCauley
    Jun 14, 2005
  5. weston
    Replies:
    1
    Views:
    278
    Richard Cornford
    Sep 22, 2006
Loading...

Share This Page