473,804 Members | 2,112 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to avoid object creation in heap/stack

gpraghuram
1,275 Recognized Expert Top Contributor
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 throw some light into this

Raghuram
Oct 8 '07 #1
6 7899
ruskalym
65 New Member
Hello,

To create an object in heap, you can allocate it through `new` keyword.

To create an object in stack, you can define it as a local variable, in a function or a method.
Oct 8 '07 #2
JosAH
11,448 Recognized Expert MVP
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 throw some light into this

Raghuram
Ruskalym (see above) already answered your question: if you don't want users
to be able to create objects on the heap, overload the new operator such that
it throws an error (or make it a private operator so that the user can't access it).

If you don't want objects on the stack you could make all constructors private
and add a 'factory method' that builds objects on the heap for the user.

kind regards,

Jos
Oct 8 '07 #3
gpraghuram
1,275 Recognized Expert Top Contributor
Ruskalym (see above) already answered your question: if you don't want users
to be able to create objects on the heap, overload the new operator such that
it throws an error (or make it a private operator so that the user can't access it).

If you don't want objects on the stack you could make all constructors private
and add a 'factory method' that builds objects on the heap for the user.

kind regards,

Jos
Thanks a lot Jos....
I got the answer.
How can you make new operator private?
Did u mean that overloading shuld be made private?

Raghuram
Oct 8 '07 #4
JosAH
11,448 Recognized Expert MVP
Thanks a lot Jos....
I got the answer.
How can you make new operator private?
Erm, you can't; I goofed (I had some strange sort of Java code in my head when
I wrote that (incorrect) remark). It is possible to supply your own implementation
for the global operator new() though; I think that's the way to go if you don't want
any objects on the heap.

Sorry for the mistake and

kind regards,

Jos
Oct 8 '07 #5
duedony
2 New Member
Dear Jos:

>>It is possible to supply your own implementation
>>for the global operator new() though;

If I can implement the global operator new(), I can calculate the total heap usage for C++ object creation on my system.
Oct 10 '07 #6
abnsreddy
1 New Member
In c++ how will you restrict
1)object creation in heap
>>
make private the following functions:
void *operator new(size_t),
void *operator new[](size_t)
2)object creation in stack
>>
make constructor and destructor private
have seperate static functions like create and destroy functions which will internally call constructor and destructor respectively
Jan 2 '12 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

30
3587
by: seesaw | last post by:
Is it right thing to always avoid using "new" to create objects? What if after starting the application, then decide which and how many objects to create? (Seems like under such situation is there no other choice but using "new")
6
2104
by: | last post by:
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.
3
3155
by: balu | last post by:
Hi Gurus I need a help. Im developing a C++ libaray. I don't whether user creates object in stack/heap. Is there a way to find object location? Balu.N
8
1250
by: Lee Jackson | last post by:
Whats the difference between the following in terms of how the CLR allocates and garbage collects? Which is better in terms of memory usage/performance? a) foreach (string strURL in arrLongListOfURLs) { string strCopy = strURL; }
7
2863
by: Arpan | last post by:
The .NET Framework 2.0 documentation states that An Object variable always holds a pointer to the data, never the data itself. Now w.r.t. the following ASP.NET code snippet, can someone please explain me what does the above statement mean? <script runat="server"> Class Clock
4
3307
by: shaanxxx | last post by:
how can we restrict object creation on heap ? soln1: overload new any other way to do this ?
7
22052
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 a; (this type of code and other complex mallc/free etc is used frequently and total approx 450 files, each file is of approx/avg 1000 line,
13
2796
by: orobalage | last post by:
Hi! I was developing some number-crunching algorithms for my university, and I put the processor into a class. While testing, I found a quite *severe performance problem* when the object was created on the stack. I uploaded a test archive here: http://digitus.itk.ppke.hu/~oroba/stack_test.zip Inside you'll find the number cruncher class (CNN in cnn.h and
2
1473
by: Anane | last post by:
When we use NEW word to create object of a class, it always creates object on HEAP memory. How can one create object on stack? If one creates object inside a function, then also object will be created in HEAP. Only object reference will be in stack. When function ends, object reference is destroyed, eventually object will be destroyed by GC. Am I correct with this concept?
0
9712
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10595
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10343
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10089
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9171
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7634
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5530
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4308
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.