0
When new is used apart from control to deallocate memory
Hi I had opinion that new i.e. dynamic memory is used when we want control of that memory... It means it should be accessible outside function scope until we don't free it using delete Is there any other need to use new ? Is it like we opt for new when object size is very large ? Any other occasions when new usage is must ?
2 Réponses
+ 4
Memory dynamically allocated with new and freed with delete is allocated on the “heap” while automatic memory allocation happens on the “stack.”
Yes. Because the heap is much larger than the stack, we may choose to allocate there if an object/collection is very large.
One other reason may be speed. Generally, objects allocated to the heap are slower to find, access, and recall than those on the stack. By storing things on the heap that are low priority (infrequently accessed), we might see gains in efficiency because the occupied memory in the stack is reduced and thus we may retrieve data from there even more quickly (enhancing stack efficiency through data prioritization).
+ 1
Thanks Jeremy Miller