473,403 Members | 2,222 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,403 software developers and data experts.

How to avoid object creation in heap/stack

gpraghuram
1,275 Expert 1GB
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 7822
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 Expert 8TB
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 Expert 1GB
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 Expert 8TB
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
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
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
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...
6
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...
3
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
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...
7
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...
4
by: shaanxxx | last post by:
how can we restrict object creation on heap ? soln1: overload new any other way to do 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...
13
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...
2
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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...

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.