Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

memory management & std::allocator

In reviewing my code I see some "ugly" structure I use, in a class (called "map") I have a vector which contains a "data" class:

std::vector<PointerToHUGEClass> vector;

Where PointerToHUGEClass is just like the name describes. (though the object pointed too is also owned by the map class, and created with the "new" parameter in the constructor). This works all good (at the moment). However I still feel it is more of a work-around.

The only reason I am using a "PointerToHUGEClass" instead of just "HUGEClass", is because I wanted to make sure the object is not declared from the stack. This was made however before I understood allocaters. Now I feel it is more or less the task of the allocator to ensure the memory isn't declared from the stack.

My questions:

  • Am I correct in assuming the allocator is responsible for the memory management from the items? (And making sure it is declared from stack/freestore/heap/whatever)
  • What does the std::allocator do? - Does it declare from the stack, or from the heap?
  • (follow up from previous question): if I copy an item declared in the stack to the datastructure is it still declared in the heap?

Thanks again, paul23

like image 993
paul23 Avatar asked Nov 23 '10 10:11

paul23


People also ask

What is memory management explain?

Memory management is the process of controlling and coordinating a computer's main memory. It ensures that blocks of memory space are properly managed and allocated so the operating system (OS), applications and other running processes have the memory they need to carry out their operations.

What is memory management and its types?

Memory management is the functionality of an operating system which handles or manages primary memory and moves processes back and forth between main memory and disk during execution. Memory management keeps track of each and every memory location, regardless of either it is allocated to some process or it is free.

What are the four types of memory management?

It keeps track of every memory location(if its is free or occupied). Paging, and swapping,segmentation and compaction are modern computers' four main memory management techniques. Swapping is the best technique for memory management because it provides the most efficient use of system resources.


1 Answers

  • Am I correct in assuming the allocator is responsible for the memory management from the items? (And making sure it is declared from stack/freestore/heap/whatever)

No you are not. The allocator is just sugar coating over new and delete and in general responsible of deciding where the memory will be allocated. The responsibility of calling allocate, deallocate, construct and destruct is to its users (which means here, std::vector). From your point of view, it'll be automatic, which is what matters here after all.

  • What does the std::allocator do? - Does it declare from the stack, or from the heap?

std::allocator is mandated to allocate using ::operator new(size_t), thus it depends on the definition of the global new operator. Generally, this means the heap. The stack is for object with automatic storage duration.

  • (follow up from previous question): if I copy an item declared in the stack to the datastructure is it still declared in the heap?

If you copy an item, then a copy is allocated where you copy it. Here it means copying an item from the stack to the heap. You then have two copies of the object, meaning that changes on one copy are not reflected on the other.

Beware though, that we are talking about the default copying mode, that is a shallow copy. If you copy an object, it'll get copied all fine; if you copy a pointer, only the pointer will be copied, not the data pointed to.

like image 173
Matthieu M. Avatar answered Oct 16 '22 14:10

Matthieu M.