new of an object without assigning it to any variable

A

ajay

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

new A;

???
tx
 
S

Siemel Naran

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

Alf P. Steinbach

* "Corno said:
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();'.
 
K

Kevin Goodsell

ajay said:
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
 
S

Serge Paccalin

Le mardi 13 avril 2004 à 09:05, Siemel Naran a écrit dans
comp.lang.c++ :
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
 
A

Allan Bruce

ajay said:
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
 
R

Richard Herring

Buster said:
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.
 
B

Bill Seurer

Richard said:
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.
 
L

Leor Zolman

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
 
A

Alf P. Steinbach

* Leor Zolman said:
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.
 
N

Nils Petter Vaskinn

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")
 
L

Leor Zolman

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

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



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



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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top