Memory management and std :: allocator

When looking at my code, I see some ugly structure that I use in the class (called "map") I have a vector that contains the "data" class:

std::vector<PointerToHUGEClass> vector; 

Where PointerToHUGEClass is exactly the same as the name describes. (although the specified object also belongs to the map class and is created using the "new" parameter in the constructor). This works well (for now). However, I still feel that this is more of a job.

The reason is only I use "PointerToHUGEClass" instead of "HUGEClass" because I wanted to make sure that the object was not declared from the stack. This was done before I understood the assistants. Now I feel that the task of the allocator is to ensure that the memory is not declared from the stack.

My questions:

  • Do I correctly assume that the allocator is responsible for managing memory from the elements? (And make sure it is declared from stack / freestore / heap / whatever)
  • What does std :: allocator do? - Is it declared from the stack or from the heap?
  • (follow the previous question): if I copy an item declared on the stack to the data structure, is it still declared on the heap?

Thanks again, paul23

+5
c ++ memory-management stl
Nov 23 '10 at 10:01
source share
2 answers
  • Do I correctly assume that the allocator is responsible for managing memory from the elements? (And make sure it is declared from stack / freestore / heap / whatever)

No no. A allocator is simply a sugar coating on top of new and delete and is generally responsible for deciding where the memory will be allocated. Responsibility for calling allocate , deallocate , construct and destruct belongs to its users (which means std::vector ). From your point of view, it will be automatic, which is important here in the end.

  • What does std :: allocator do? - Is it declared from the stack or from the heap?

std::allocator is assigned using ::operator new(size_t) , so this depends on the definition of the global operator new . This usually means a bunch. The stack is intended for an object with automatic storage duration.

  • (follow the previous question): if I copy an item declared on the stack to the data structure, is it still declared on the heap?

If you copy an element, the copy is highlighted where you copy it. Here, this means copying an item from the stack to the heap. Then you have two copies of the object, which means that the changes on one copy are not reflected on the other.

Beware, however, that this is the default copy mode, which is a shallow copy. If you copy an object, it will be fully copied; if you copy the pointer, only the pointer will be copied, not the data it points to.

+8
Nov 23 '10 at 10:19
source share

Your answers:

  • Yes you are right. In any case, if the container template type is a pointer, as in your case, the container will allocate / free memory for the pointer, and not for the object that the pointer points to!

  • Prior to implementation, the $ 20.2.5 standard describes valve requirements.

  • Since you keep pointers, I'm afraid the answer to this question will confuse you. If you copy a pointer to an object allocated on the stack into the container, your object will remain on the stack. Once you exit the scope, it will be destroyed and you will have the wrong pointer in the container.

NTN

0
Nov 23 '10 at 10:14
source share



All Articles