This repository has been archived on 2021-11-03. You can view files and clone it, but cannot push or open issues or pull requests.
2019-11-12 10:49:02 +01:00
|
|
|
/*
|
|
|
|
* Deliberately leak memory
|
|
|
|
*
|
|
|
|
* Free memory decreases for a second.
|
|
|
|
* However, because the program quits, the kernel frees the used memory
|
|
|
|
*
|
|
|
|
* If run for some time, my swap space would fill, afterwards my computer is brought to a halt.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#define K 1000
|
|
|
|
#define M K*K
|
|
|
|
#define G K*M
|
|
|
|
|
|
|
|
#define MEMORY_SIZE_IN_CHARS G
|
|
|
|
|
2020-05-06 11:43:09 +02:00
|
|
|
void make_throwaway_ptrs(int leak_size ) {
|
|
|
|
for ( int i = 0; i < leak_size ; i++)
|
|
|
|
{
|
|
|
|
char* throwaway_ptr = new char;
|
|
|
|
}
|
|
|
|
|
|
|
|
// oops, we are not returning the pointers
|
|
|
|
// this means it goes out of scope when the function ends
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-11-12 10:49:02 +01:00
|
|
|
int main() {
|
2020-05-06 11:43:09 +02:00
|
|
|
make_throwaway_ptrs(MEMORY_SIZE_IN_CHARS);
|
2019-11-12 10:49:02 +01:00
|
|
|
|
|
|
|
std::cout << "We leaked " << MEMORY_SIZE_IN_CHARS * sizeof " " << " bits" << std::endl;
|
|
|
|
|
2020-05-06 11:43:09 +02:00
|
|
|
|
|
|
|
std::cout << "Ready to kill the process? (press Enter)" << std::endl;
|
|
|
|
std::cin.ignore();
|
2019-11-12 10:49:02 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|