473,397 Members | 1,974 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,397 software developers and data experts.

starting an app via "new"

I am using a C++ library to build a GUI app in C++. The lib requires me to start all of my code from an init() and free it all through a shutdown() which are called (respectively) from the constructor and destructor.

I could easily just instantiate a class of mine in init() which starts all of my code and listeners, e.g.:
MyApplicationStuff* appObj;
...
void init(){
MyApplicationStuff appObj = new MyApplicationStuff();
}

But this would place that object in the free store and most of the code executed from within that object (basically my entire app) utilizes automatic objects (stack-based).

My Question:
If my app were designed to utilize the stack, would calling 'new' to start up my app cause everything to be in the free-store... even my automatic/local variables?
Apr 15 '07 #1
3 1316
weaknessforcats
9,208 Expert Mod 8TB
Creating an object on the heap DOES NOT put evferything on the heap. What goes on the heap are the data members of the object. And that's all.

When you call a method, the local variables go on the stack. The location of the call local variables has nothing to do with the location of the object.

Only when you use "new" does data go on the heap.
Apr 15 '07 #2
Creating an object on the heap DOES NOT put evferything on the heap. What goes on the heap are the data members of the object. And that's all.

When you call a method, the local variables go on the stack. The location of the call local variables has nothing to do with the location of the object.

Only when you use "new" does data go on the heap.
Thanks for the reply. I think you answered my question, but I still am not positive...

Given "someObj" is a member contained by value by "anObj" which is a member contained by value by "appObj"... and appObj is instantiated with new, is someObj on the stack or heap? Is anObj on the stack or heap?

I think you meant that someObj would be on the stack (i.e. _not_ free-store) and that anObj would be on the heap. Is this right?
Apr 15 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
Look at what you said:

Given "someObj" is a member contained by value by "anObj" which is a member contained by value by "appObj"... and appObj is instantiated with new, is someObj on the stack or heap? Is anObj on the stack or heap?

This is how it is set up:
1) appObj is allocated on the heap.
2) appObj contains anObj -> so anObj is on the heap
3) an Obj contains someObj -> so someObj is on the heap

That is, everything is on the heap becuse appObj is on the heap.

However, this applies only to the DATA MEMBERS.

Member functions don't have heaps or stacks. They are just functions. When you call one of these functions, the local variables will be on the stack. It makes no difference where the object is located.

A variable is on the heap only when you use "new". So this member function

void appObj::AFunction()
{
char* data = new char[50];
}

has the array on the heap and the pointer data (a local variable) on the stack.
Apr 16 '07 #4

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

Similar topics

6
by: dpr | last post by:
I have come accross a piece of C++ code with the construct: MyClass *c = new class MyClass(); Is there a difference between this and: MyClass *c = new MyClass(); ?
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...
24
by: Rv5 | last post by:
Rookie c++ question, but Ive spent the last 5 years doing Java, where everytime I created an object I used new. In c++ I can create my objects without and its confusing me just a little. I have...
6
by: peter.xiau | last post by:
I found that std::vector<int> v(10) ; will automatically initiallize every elem to 0 in the vector, I check the source code (VS.NET2003), and I found a line of code like this *T = new T() ; ...
37
by: jht5945 | last post by:
For example I wrote a function: function Func() { // do something } we can call it like: var obj = new Func(); // call it as a constructor or var result = Func(); // call it as...
43
by: Bill H | last post by:
25 years ago every computer came with some form of Basic interpreter so you could use yoru computer without having to buy more software. Is Javascript (teamed with HTML) set to become the new...
30
by: Medvedev | last post by:
i see serveral source codes , and i found they almost only use "new" and "delete" keywords to make they object. Why should i do that , and as i know the object is going to be destroy by itself at...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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
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
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...
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.