Skip to content

Instantly share code, notes, and snippets.

@arraytools
Last active December 23, 2015 20:29
Show Gist options
  • Save arraytools/6689581 to your computer and use it in GitHub Desktop.
Save arraytools/6689581 to your computer and use it in GitHub Desktop.
memory leak test
#include <iostream>
using namespace std;
void foo(unsigned long long n)
{
double *ptr = new (std::nothrow) double[n];
if (!ptr) {
cout << "Failed to allocate double[n]" << endl;
} else {
ptr[1] = 1.0;
ptr[n-1] = 2.0;
cout << "double[n] is allocated successfully!" << endl;
}
}
int main()
{
cout << "The program is used to test memory leak. \n";
cout << "Do not worry. It won't crash your computer.\n";
cout << "The source code is available on https://gist.github.com/arraytools/6689581 \n" << endl;
unsigned long long n;
int testChoice;
cout << "Enter your choice 1=main, 2=sub: ";
cin >> testChoice;
cout << "\nNext enter the array size (<= 18,446,744,073,709,551,615) : ";
cout << "\nSome common scenarios";
cout << "\n268,435,456-1 = 2GB";
cout << "\n536,870,912-1 = 4GB";
cout << "\n1,073,741,824-1 = 8GB";
cout << "\n2,147,483,648-1 = 16GB";
cout << "\n4,294,967,296-1 = 32GB";
cout << "\n8,589,934,592-1 = 64GB";
cout << "\n";
cin >> n;
if (testChoice == 1)
{
double *ptr = new (std::nothrow) double[n];
if (!ptr)
{
cout << "Failed to allocate double[n]" << endl;
} else {
ptr[1] = 1.0;
ptr[n-1] = 2.0;
cout << "double[n] is allocated successfully!" << endl;
}
} else {
foo(n);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment