473,406 Members | 2,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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.
Jul 22 '05 #1
6 2082

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

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

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
"<- 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
> > 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Dan Elliott | last post by:
Hello all, I am writing a program which needs to run as quickly as possible, but holds a lot of data in memory (around 1GB for a usual run). Is this too much memory to even consider putting...
4
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's...
24
by: arcticool | last post by:
I had an interview today and I got destroyed :( The question was why have a stack and a heap? I could answer all the practical stuff like value types live on the stack, enums are on the stack, as...
2
by: robert | last post by:
When employing complex UI libs (wx, win32ui, ..) and other extension libs, nice "only Python stack traces" remain a myth. Currently I'm hunting again a rare C-level crash bug of a Python based...
16
by: sarathy | last post by:
Hi all, I need a few clarifications regarding memory allocaion in C++. I apologize for the lengthy explanation. 1. In C++, Objects are allocated in heap. What does heap refer to? Is it an area...
5
by: rover8898 | last post by:
Hello all, I have a programming problem/challenge. Let explain my scenario: I have a C program (distributed accross many files and functions) that is responsible for handling hardware. This...
7
by: amit.atray | last post by:
Environement : Sun OS + gnu tools + sun studio (dbx etc) having some Old C-Code (ansi + KR Style) and code inspection shows some big size variable (auto) allocated (on stack) say for ex. char...
6
by: roguefeebo | last post by:
Hello everyone, thanks for looking. I'll get right to the point: I'm having problems trying to figure out a hierarchy of menus in C; command-line style. Say, for example the program analyzes a...
6
gpraghuram
by: gpraghuram | last post by:
HI All, Someone asked me the following question and i am not able to get an answer for this. In c++ how will you restrict 1)object creation in heap 2)object creation in stack Can anyoine...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.