Last active
December 23, 2015 20:29
-
-
Save arraytools/6689581 to your computer and use it in GitHub Desktop.
memory leak test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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