-4

On creating a class object with 'new' keyword,why is an object heap created instead of stack?Would stack not be a better option since it is faster,performs contiguous allocation and does not need manual deletion(free/delete)? If objects of a particular class have the same template then why do we need dynamic allocation?(justification for heap) [×××Please note that my doubt is not about which option a programmer must choose,but instead about why the compiler behaves the way it does in the above case.×××]

  • You're asking what the purpose of the heap is. Well, it's certainly possible to write applications without using the heap, but it's limiting as well; you generally have to know ahead of time every allocation you need to make. It's a trade-off between flexibility and performance. – Cameron Sep 03 '15 at 19:19
  • 2
    Stack is very limited and costful. full answer: http://stackoverflow.com/questions/7123936/why-is-there-a-stack-and-a-heap – CyberGuy Sep 03 '15 at 19:19
  • Wow. Even the comments are duplicates. @CyberGuy wouldn't mind knowing what you mean by "costful". Stack is usually the cheaper option. – user4581301 Sep 03 '15 at 19:22
  • @user4581301 see image below. By costful i mean expensive in dollars. – CyberGuy Sep 03 '15 at 19:23
  • Why is it anymore costful than the heap? Maybe we are just misinterpreting your comment. – David Zech Sep 03 '15 at 19:23
  • In order to understand the stack i guess you need to learn a little bit of assembly and __cdecl calling convension otherwise nothing people say will make any sense to you anyways. The stack is limited, there are rules that needs to be followed in order to have some sense of flow and order. The heap, stack, data segment or whatever you want to call it is all in ram its just how allocation and deallocation is handled. –  Sep 03 '15 at 19:29
  • 1
    The `new` gets memory from the free-store. Nothing stops an implementation from using the stack as the free-store. – PaulMcKenzie Sep 03 '15 at 19:31
  • Stack is usually more efficient because it requires adjusting a pointer. Allocating from the heap usually involves find a block that is appropriate size; which takes more time than adjusting a pointer. – Thomas Matthews Sep 03 '15 at 19:34
  • By the way, it's not one or the other. Many implementations give you a stack and heap to use (as well as global variable space). A rule of thumb is that large items (objects) use the heap. Also, the heap is used for objects that have a longer lifetime than inside a function or inside a statement block. – Thomas Matthews Sep 03 '15 at 19:37
  • On a side note, if you wanted to allocate off the stack (with auto-free on function exit), you could use _alloca(): object * pobject = (object *) _alloca(sizeof(object)); . – rcgldr Sep 03 '15 at 19:45
  • 1
    Strongly object to the premature closure of this question. It is not a duplicate of the question about what distinguishes stack vs heap, it is a question about why you would use one or the other. A thoughtful answer could have been given. When a question is closed this quickly though it can be answered, you waste the time of dozens of visitors and worsen this site and its rep. – cardiff space man Sep 03 '15 at 19:54
  • I don't see how this question is a repetition of stack vs heap.I simply want to know why we use heaps when we always have the option of going with stacks for c++ objects. – Ankit Peshin Sep 03 '15 at 20:33
  • Very related: http://stackoverflow.com/a/3889495/214671 – Matteo Italia Sep 03 '15 at 21:35

1 Answers1

3

Basically, stack-created objects cannot have a lifetime outside of the function they're created in. That's the most obvious rationale for a heap.

Object * foo()
{
  Object o;
  return &o;  // undefined behaviour, because 'o' ceases to exist once the function returns.
}
Destructor
  • 13,235
  • 8
  • 48
  • 108
Roddy
  • 63,052
  • 38
  • 156
  • 264
  • Note that string literals like `"Hello World"` are an exception to this rule – VoidStar Sep 03 '15 at 19:51
  • 5
    @Machtl, The 'string literal' isn't a stack-created object (or a heap-created one). The *pointer* to it is, though. – Roddy Sep 03 '15 at 19:54