By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,238 Members | 1,746 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,238 IT Pros & Developers. It's quick & easy.

avoid stack creation?

P: n/a
I have a class with overloading operator new. (Because, if an identical object exists, return a pointer to existed object instead of
a new pointer)
It has no sense (it is dangerous) to allocate an object of that class in stack.

So, is there a trick to avoid stack creation of an object of this class?
I mean, to take compile-time error instead of runtime.
I want to create this class object only in heap.
Thanks.
Jul 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a

"<- Chameleon ->" <ch******@hotmail.NOSPAM.com> wrote in message
news:c7**********@nic.grnet.gr...
I have a class with overloading operator new. (Because, if an identical object exists, return a pointer to existed object instead of a new pointer)
It has no sense (it is dangerous) to allocate an object of that class in stack.
So, is there a trick to avoid stack creation of an object of this class?
I mean, to take compile-time error instead of runtime.
I want to create this class object only in heap.
Thanks.


Simple trick is to make all the constructors private and create with a
friend function

class X
{
friend X* new_x();
private:
X();
public:
...
};

X* new_x() { return new X(); }

john
Jul 22 '05 #2

P: n/a
> > I have a class with overloading operator new. (Because, if an identical
object exists, return a pointer to existed object instead of
a new pointer)
It has no sense (it is dangerous) to allocate an object of that class in

stack.

So, is there a trick to avoid stack creation of an object of this class?
I mean, to take compile-time error instead of runtime.
I want to create this class object only in heap.


Simple trick is to make all the constructors private and create with a
friend function

class X
{
friend X* new_x();
private:
X();
public:
...
};

X* new_x() { return new X(); }

this method has a problem:
destructor never run!
Jul 22 '05 #3

P: n/a

"<- Chameleon ->" <ch******@hotmail.NOSPAM.com> wrote in message
news:c7**********@nic.grnet.gr...
I have a class with overloading operator new. (Because, if an identical
object exists, return a pointer to existed object instead of
a new pointer)
It has no sense (it is dangerous) to allocate an object of that class
in stack.

So, is there a trick to avoid stack creation of an object of this

class? I mean, to take compile-time error instead of runtime.
I want to create this class object only in heap.


Simple trick is to make all the constructors private and create with a
friend function

class X
{
friend X* new_x();
private:
X();
public:
...
};

X* new_x() { return new X(); }

this method has a problem:
destructor never run!


That will be because you never called delete.

X * xp = new_x();
....
delete xp; // destructor runs here

john
Jul 22 '05 #4

P: n/a

From your posting, its not clear to me what the exact problem is.

If you only want one object to exist and all clients share it, use the
singleton design pattern - see the book.

If the "object is too big to be stack allocated", then design the class so
that you have a wrapper which is ok to be on the stack and the guts which
are
dynamically allocated. In this way, you'll get the benefits of clean up.

If the object should always be allocate do the heap, make the constructor
private, but provide a public new() or a static class member to make one for
you. This works because new() can access the private constructor member ok
in
constructing the object, but the client can't.

Overloading the new() operator in the way you says sounds kind of dangerous,
if I do a new() I expect to put in a delete() somewhere, what would the
semantics
be, no-op, or actually destroy the object, this brings us back to the
singleton pattern.

dt.

----- Original Message -----
From: "<- Chameleon ->" <ch******@hotmail.NOSPAM.com>
Newsgroups: comp.lang.c++
Sent: Wednesday, May 12, 2004 2:14 AM
Subject: avoid stack creation?

I have a class with overloading operator new. (Because, if an identical object exists, return a pointer to existed object instead of a new pointer)
It has no sense (it is dangerous) to allocate an object of that class in stack.
So, is there a trick to avoid stack creation of an object of this class?
I mean, to take compile-time error instead of runtime.
I want to create this class object only in heap.
Thanks.

"<- Chameleon ->" <ch******@hotmail.NOSPAM.com> wrote in message
news:c7**********@nic.grnet.gr... I have a class with overloading operator new. (Because, if an identical object exists, return a pointer to existed object instead of a new pointer)
It has no sense (it is dangerous) to allocate an object of that class in stack.
So, is there a trick to avoid stack creation of an object of this class?
I mean, to take compile-time error instead of runtime.
I want to create this class object only in heap.
Thanks.

Jul 22 '05 #5

P: n/a
"<- Chameleon ->" <ch******@hotmail.NOSPAM.com> wrote in message news:<c7**********@nic.grnet.gr>...
I have a class with overloading operator new. (Because, if an
identical object exists, return a pointer to existed object
instead of a new pointer)
It has no sense (it is dangerous) to allocate an object of
that class in stack.


This is a very very bad idea. If I call new twice, I *will* call
delete twice. This will break your operator new, because it didn't
allocate memory twice.

The correct implementation is not to use pointers at all, here.
Instead, create a X_handle class. This class can be freely copied,
created, etcetera. Internally, they hold a pointer to the real
X object. Each X object holds an X_handle_count member. This count
is controlled by the X_handle ctors and dtors. When the count
reaches 0, the last X_handle dtor deletes the X.

Because the X_handle has sizeof(X*), it can be copied around,
stack allocated, etcetera. The real X is always on the free store.
X doesn't have public ctors or dtors, but X_handle is a friend.

Regards,
Michiel Salters
Jul 22 '05 #6

P: n/a
> > I have a class with overloading operator new. (Because, if an
identical object exists, return a pointer to existed object
instead of a new pointer)
It has no sense (it is dangerous) to allocate an object of
that class in stack.


This is a very very bad idea. If I call new twice, I *will* call
delete twice. This will break your operator new, because it didn't
allocate memory twice.

The correct implementation is not to use pointers at all, here.
Instead, create a X_handle class. This class can be freely copied,
created, etcetera. Internally, they hold a pointer to the real
X object. Each X object holds an X_handle_count member. This count
is controlled by the X_handle ctors and dtors. When the count
reaches 0, the last X_handle dtor deletes the X.

Because the X_handle has sizeof(X*), it can be copied around,
stack allocated, etcetera. The real X is always on the free store.
X doesn't have public ctors or dtors, but X_handle is a friend.

I use this method earlier with a template class:
class SharedObject<C>
which contains a class (any class).
Now I use a class with private constructor (so, I cannot run "new" or stack allocation)
and it have a public method
Texture2D *Texture2D::LoadTexture(..............);
which maybe allocate a new object or maybe return a pointer to a previous allocated object (increase counter).

And delete:

void Texture2D::operator delete(void *obj)
{
Texture2D *tex = (Texture2D*) obj;
if (--tex->count == 0) free(obj);
}
Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.