Why is the heap (as in where we put objects) called that? [duplicate] - heap

This question already has answers here:
Closed 13 years ago.
Possible Duplicate:
What is the origin of the term “heap” for the free store?
Why is the heap called the heap? I mean the bit of memory which we dynamically allocate bits of.

I don't have a citation for this, but I've always believed it derives from "a place to put stuff; a bit like the stack only less organised".
A stack has strict rules about where you add and remove things, but a heap doesn't.

Related

C++ : Use case / real world application of using the heap memory [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 months ago.
Improve this question
I am trying to code a relatively simple program to understand in which scenarios it would be more efficient and useful to use the heap.
I first read that you better store large objects on the heap.
So I created a std::vector on the heap and filled it with an insane amount of bytes, something like 18Gb. At some point, it threw a std::bad_alloc exception, then my OS (Linux mint) killed the process after the swap was full. But the result was the same with the stack, so I don't understand how it's better on the heap.
Maybe I lack creativity, but I cannot think of a design where I would absolutely need to use the heap, I can always pass a reference of my object on the stack and achieve the same memory usage efficiency. For every program I wrote, it was always more efficient to use the stack speed wise and was the same for memory usage.
So, in what scenario is it useful to use the heap regarding memory usage efficiency or speed?
You use the heap whenever you need more flexibility than the stack provides. If you need to allocate memory to return something from a function, then it can't be on the stack, because it would be freed when the function returned. And it can't be global, because you might call the same function more than once.
In C++, you might not always realize that you are using the heap, because classes like std::vector and std::string use the heap internally. You don't always have to write new yourself to use the heap.

std::vector clear or swap with new vector [duplicate]

This question already has answers here:
Does clearing a vector affect its capacity?
(4 answers)
C++ delete vector, objects, free memory
(7 answers)
Closed 3 years ago.
Sometimes I detect in different c++ projects code like this:
std::vector<DataClass> _dataVec;
...
std::vector<DataClass>().swap(_dataVec);
Is this code more effective than obvious and simple clear call
_dataVec.clear();
or these code samples have some kind of difference?
For what purpose I should prefer first variant?

C++ struct vs Class in terms of memory [duplicate]

This question already has answers here:
C/C++ Struct vs Class
(7 answers)
Closed 6 years ago.
I have recently come across a question in one of my interview.
What is the difference between C++ struct & class in terms of memory ??
I know they are same in all aspects except access specifiers while inheriting & in case of member variables.
Apart from this is there a real difference in terms of memory (may be memory allocation or destruction or memory management whatever) ????
Edit:
I am not pretty sure why did the interviewer asks this question when there is no difference. I have found similar question here see the 2nd comment down to that link, he is asking a same question but no answer. I think there should be a diff.
Thanks In Advance.
In C++, a class and a struct are completly identical except for the facts that structs default to public access and inheritance whereas class defaults to private.
As far as memory layout there is no difference what-so-ever.

Why do we use pointers instead of local variables? [duplicate]

This question already has answers here:
Why use pointers? [closed]
(17 answers)
Closed 9 years ago.
I don't understand the difference between using a pointer and using a normal variable. I'm learning linked lists in class so the use of pointers seems more straightforward since pointers are used to go to the next node in the list, but I don't understand its more basic uses and I'm feeling stressed as it is something I should already understand but don't.
The pointer is in itself just a "normal" variable that just so happens to store a level of indirection to another variable.

Most Common Mistake done/seen in C++ [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
What C++ pitfalls should I avoid ?
What is the most common mistake in C/C++programming that you keep committing or see most of the people do? Being aware of it atleast subconsciously will increase my or anyone's chances of committing it.
Wrong memory management in all kinds of ways - new without delete, delete on the wrong pointer, unclear ownership of a pointer with resulting memory allocation / deallocation problems etc.