Skip to content

Instantly share code, notes, and snippets.

@jmftrindade
Created June 3, 2021 21:30
Show Gist options
  • Save jmftrindade/045d34df3aff88cb956c11890c47cb24 to your computer and use it in GitHub Desktop.
Save jmftrindade/045d34df3aff88cb956c11890c47cb24 to your computer and use it in GitHub Desktop.
Instrumenting operator new to keep track of mem allocations
struct A {
	static std::size_t allocated;
  
  	static void* operator new(std::size_t size) {
    	allocated += size / sizeof(A);
      	return ::operator new(size);
    }
  
  	static void* operator new[](std::size_t size) {
    	allocated += size / sizeof(A);
      	return ::operator new(size);
    }
}

std::size_t A::allocated = 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment