function
<memory>

std::declare_no_pointers

void declare_no_pointers (char* p, size_t n);
Declare memory block as containing no pointers
Declares the range of memory starting at p and spanning n bytes as containing no traceable pointer locations.

This formally invalidates any object created with ::operator new whose address in only stored in that range. Notice though that compilers implementing a relaxed pointer safety policy may ignore this.

In garbage-collected implementations, a call to this function can cause objects whose only safely-derived pointer (or its integer representation) resides in the memory range to be collected.

undeclare_no_pointers must be called on the same range before the destruction of the object containing the range. The call to undeclare_no_pointers unregisters the range without restoring any validity to any dynamic object previously invalidated by the call to declare_no_pointers.

Parameters

p
Pointer to the first byte of the range to be declared.
No bytes of the specified range shall have been registered by a previous this function and not released.
n
The length, expressed in bytes, of the range to be declared.
size_t is an unsigned integral type.

Return value

none

This function throws no exceptions.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// declare_no_pointers / undeclare_no_pointers
#include <iostream>
#include <memory>

int main() {
  int * foo = new int(1);
  int * bar = new int(2);
  std::declare_no_pointers (reinterpret_cast<char*>(&bar),sizeof(int*));

  std::cout << "*foo: " << *foo << '\n';

  if (std::get_pointer_safety() != std::pointer_safety::strict)
    std::cout << "*bar: " << *bar << '\n';  // invalid if strict safety

  std::undeclare_no_pointers (reinterpret_cast<char*>(&bar),sizeof(int*));

  delete bar;
  delete foo;
  return 0;
}


Possible output:
*foo: 1
*bar: 2

See also