lundi 26 juin 2017

Does the STD allocator dynamically allocate memory on the heap? does it safely delete memory?

So I am trying to learn about the std::allocator<> in C++ and was confused from the reference sites about a few things. Especially since i read the construct and deconstruct methods of allocator are deprecated in C++17

This is the following code i wrote as an example

// Example with pointers and allocators
#include <iostream>
#include <memory>




int main()
{
  std::allocator<int> nums;
  int * first = nums.allocate(1); //is this on the heap, like with calling new int(4)?
  int * second = nums.allocate(2);
  *first = 7;
  second[0] = 2;
  second[1] = 4;
  std::cout << *first << std::endl;
  std::cout << second[1] << std::endl;
  nums.deallocate(first, 1); //is the int safely deleted from memory?
  nums.deallocate(second, 2);
}

When one calls the allocate method, does the pointer that's returned point to dynamic piece of memory on the heap or is that memory stack allocated?

Additionally, when one calls the deallocate method, is the pointer being deallocated also have it's object deleted from memory? is deallocate equivalent to delete?

Aucun commentaire:

Enregistrer un commentaire