473,399 Members | 3,302 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,399 software developers and data experts.

Preventing stack instances

I know that making new protected or private will (generally) prevent
instances from being created on the heap, but I was wondering about
preventing them on the stack.

I saw in another post a hint about protecting the destructor. As the
objects in question are all managed through a single smart pointer type
I suspect that something like the following should work:

class MyObjectPtr;

class MyObject { // There are manu sub-classes of this type
protected:
virtual ~MyObject();
friend MyObjectPtr;
};

class MyObjectPtr {
public:
~MyObjectPtr() {
delete object;
}
private:
MyObject *object;
};

[Example is really pseudo-code with anything not relevant just skipped]

Is this sufficient? Are there any 'gotchas' associated with this?

TIA
Kirit

Jun 17 '06 #1
3 1888
* Kirit Sælensminde:
I know that making new protected or private will (generally) prevent
instances from being created on the heap, but I was wondering about
preventing them on the stack.

I saw in another post a hint about protecting the destructor. As the
objects in question are all managed through a single smart pointer type
I suspect that something like the following should work:

class MyObjectPtr;

class MyObject { // There are manu sub-classes of this type
protected:
virtual ~MyObject();
friend MyObjectPtr;
};

class MyObjectPtr {
public:
~MyObjectPtr() {
delete object;
}
private:
MyObject *object;
};

[Example is really pseudo-code with anything not relevant just skipped]

Is this sufficient?
For the purpose of ensuring dynamic allocation, yes.

Are there any 'gotchas' associated with this?


Yes. A restriction to dynamic allocation is not very meaningful unless
it's exploited some way, which typically means that the objects will
participate in some linked data structure. When you restrict a class to
dynamic allocation you should therefore in general also take charge of
(or prevent) copying.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jun 17 '06 #2
On 16 Jun 2006 22:46:35 -0700, "=?iso-8859-1?q?Kirit_S=E6lensminde?="
<ki****************@gmail.com> wrote:
I know that making new protected or private will (generally) prevent
instances from being created on the heap, but I was wondering about
preventing them on the stack.

I saw in another post a hint about protecting the destructor.
.... and the constructor(s)
As the
objects in question are all managed through a single smart pointer type
I suspect that something like the following should work:

class MyObjectPtr;

class MyObject { // There are manu sub-classes of this type
protected:
virtual ~MyObject();
friend MyObjectPtr;
};
you can create derived objects on the stack, e.g.
class MyObjectEx : public MyObject {};
class MyObjectPtr {
public:
~MyObjectPtr() {
delete object;
}
private:
MyObject *object;
};

[Example is really pseudo-code with anything not relevant just skipped]
Is this sufficient? Are there any 'gotchas' associated with this?


In contrast to Java, which is a heap-oriented language, C++ works best
as stack-oriented language. Even in your example the heap allocated
objects are managed by a stack-based "smart" pointer. You should avoid
heap objects as much as possible. They produce management and
performance overhead.

Best wishes,
Roland Pibinger
Jun 17 '06 #3

Alf P. Steinbach wrote:
Are there any 'gotchas' associated with this?
Yes. A restriction to dynamic allocation is not very meaningful unless
it's exploited some way, which typically means that the objects will
participate in some linked data structure. When you restrict a class to
dynamic allocation you should therefore in general also take charge of
(or prevent) copying.


Of course. Thanks. The class already derives from boost::noncopyable so
that's covered.

Roland Pibinger wrote: you can create derived objects on the stack, e.g.
class MyObjectEx : public MyObject {};
Thanks. It means that the person doing it is specifically circumventing
the protection though. As such I guess they will get everything they
deserve :-)
In contrast to Java, which is a heap-oriented language, C++ works best
as stack-oriented language. Even in your example the heap allocated
objects are managed by a stack-based "smart" pointer. You should avoid
heap objects as much as possible. They produce management and
performance overhead.


C++ is not heap oriented? Ummm, Ok. That seems a peculiar view to take.

Of course on the matter of performance you cannot say, in general, that
stack or heap will be faster without measuring each. For some classes
heap will be faster, for others it will be faster on the stack. It all
depends on what the class is doing; how it is managing whatever
ancilliary memory/objects it controls; its own access pattern and its
create/destroy pattern.

Of course the 'management overhead' is exactly what I was asking about.
K

Jun 17 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Andrew | last post by:
Last night I was reading about implementing my own stack. The example given pushes items on and off the stack at the start and end of each procedure (ie. in a std module). What's not so clear is...
20
by: Sushil | last post by:
Hi gurus I was reading FAQ "alloca cannot be written portably, and is difficult to implement on machines without a conventional stack." I understand that the standard does not mandate...
2
by: Nick McCamy | last post by:
I have a question related to allocating on the stack. In this program below, are my following assumptions true? - variable a is allocated on the heap since it's static - variable b is...
7
by: benben | last post by:
How can I convert a stack to array? I have a Stack of System.Drawing.Point's and I would like to invoke Graphics.DrawCurve, which takes a Point array as parameter. I tried Stack.ToArray but it...
13
by: gmccallum | last post by:
General Info: A struct is stored on the stack and a class on the heap. A struct is a value type while a class is a reference type. Question: What if a struct contains a string...
9
by: shine | last post by:
what is the difference between a heap and a stack?
3
by: Boni | last post by:
Dear all, I create a big number of a class instances of some class. Sometimes this instances must be collected (all pointers are deleted, but instance is not explicitly disposed).But sometimes I...
3
by: Martin Manns | last post by:
Hi, Calling methods of other object instances seems quite expensive on the stack (see example below). Is there a better way of traversing through methods of instances that are connected in a...
9
by: Tarique | last post by:
Hello all.I am trying to implement a stack which can store either integer or float values. The code is given below: #include<stdio.h> #include<stdlib.h> #include<string.h> #define...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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,...

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.