By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,853 Members | 2,186 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,853 IT Pros & Developers. It's quick & easy.

Stack and Heap

P: 1
How can we create an object directly on heap instead of getting the same created on stack? Pl anyone can help here on this!!!! Thanks a lot.
Regards,
Sunil
Aug 8 '07 #1
Share this Question
Share on Google+
3 Replies


Meetee
Expert Mod 100+
P: 931
How can we create an object directly on heap instead of getting the same created on stack? Pl anyone can help here on this!!!! Thanks a lot.
Regards,
Sunil
Hi,

(AFAIK) whenever any object is dynamically created it is stored in heap. For eg:

Expand|Select|Wrap|Line Numbers
  1. class A
  2. {
  3. A();
  4. };
  5.  
  6. void fun(A * a)
  7. {
  8. a = new A();
  9. }
  10.  
Here the object a will be allocated in heap.

Regards
Aug 8 '07 #2

P: 93
How can we create an object directly on heap instead of getting the same created on stack? Pl anyone can help here on this!!!! Thanks a lot.
Regards,
Sunil
In C, you can make use of malloc(), calloc(), realloc() functions to
dynamically allocate memory so that they gets stored on heap.
In C++ you can use new to dynamically allocate memory so that they gets stored on heap.

Regards,
Girish.
Aug 8 '07 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
In C++ you can use new to dynamically allocate memory so that they gets stored on heap.
Be more firm about this:

In C++ you must use new to dynamically allocate memory so that they get stored on heap.


malloc() and free() do not call constructors or destructors so you can't use them in C++.
Aug 8 '07 #4

Post your reply

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