Created
August 5, 2013 09:47
-
-
Save airekans/6154711 to your computer and use it in GitHub Desktop.
A simple program used to test new handler.
This file contains hidden or 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> | |
#include <unistd.h> | |
#include <cstring> | |
#include <new> | |
using namespace std; | |
struct ListNode | |
{ | |
ListNode() | |
: next(NULL) | |
{} | |
ListNode* next; | |
char data[1024 * 1024 * 512]; // 512M | |
}; | |
ListNode* front; | |
void NewHandler() | |
{ | |
const char* msg = "NewHandler is called\n"; | |
write(2, msg, strlen(msg)); | |
ListNode* cur = NULL; | |
while (front != NULL) | |
{ | |
cur = front; | |
front = front->next; | |
delete cur; | |
} | |
} | |
int main(int argc, char *argv[]) | |
{ | |
set_new_handler(&NewHandler); | |
const char* msg = "new handler is set\n"; | |
write(2, msg, strlen(msg)); | |
int count = 0; | |
ListNode** node = &front; | |
while (true) | |
{ | |
*node = new ListNode; | |
node = &((*node)->next); | |
++count; | |
if (count % 2 == 0) | |
{ | |
cout << count / 2 << "G memory has been allocated" << endl; | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment