- LPINT lpInt = NULL;
- lpInt = (LPINT)malloc(sizeof(INT));
- *lpInt = 4;
- printf("Value : %d\r\n", *lpInt);
- free(lpInt);
- LPINT lpInt = NULL;
- lpInt = (LPINT)malloc(sizeof(INT));
- if (lpInt == NULL)
- {
- printf("Fail to alloc memory!!\r\n");
- return FALSE;
- }
- *lpInt = 4;
- printf("Value : %d\r\n", *lpInt);
- free(lpInt);
- LPINT lpInt = NULL;
- INT nTotalBytes = 0, nAllocByte = 10000000;
- while(TRUE)
- {
- lpInt = NULL;
- lpInt = new INT[nAllocByte];
- nTotalBytes += nAllocByte * sizeof(INT);
- if (lpInt == NULL)
- {
- printf("Fail to alloc memory!!\r\n");
- break;
- }
- printf("Alloc Success!! (%d)\r\n", nTotalBytes);
- }
- // blah~ blah~
Beginning in Visual C++ .NET 2002, the CRT's new function (in libc.lib, libcd.lib, libcmt.lib, libcmtd.lib, msvcrt.lib, and msvcrtd.lib) will continue to return NULL if memory allocation fails. However, the new function in the Standard C++ Library (in libcp.lib, libcpd.lib, libcpmt.lib, libcpmtd.lib, msvcprt.lib, and msvcprtd.lib) will support the behavior specified in the C++ standard, which is to throw a std::bad_alloc exception if the memory allocation fails.
- try
- {
- LPINT lpInt = NULL;
- INT nTotalBytes = 0, nAllocByte = 10000000;
- while(TRUE)
- {
- lpInt = NULL;
- lpInt = new INT[nAllocByte];
- nTotalBytes += nAllocByte * sizeof(INT);
- printf("Alloc Success!! (%d)\r\n", nTotalBytes);
- }
- }
- catch ( exception e )
- {
- printf("%s\r\n", e.what( ));
- }
If the attempt is successful, the function returns a pointer to the allocated storage. Otherwise, the function calls the designated new handler. If the called function returns, the loop repeats. The loop terminates when an attempt to allocate the requested storage is successful or when a called function does not return.
The required behavior of a new handler is to perform one of the following operations:
Make more storage available for allocation and then return.
Call either abort or exit(int).
Throw an object of type bad_alloc.
The default behavior of a new handler is to throw an object of type bad_alloc. A null pointer designates the default new handler.
The order and contiguity of storage allocated by successive calls to operator new(size_t) is unspecified, as are the initial values stored there.
- void __cdecl MyNewHandler( )
- {
- printf("Fail to Alloc Memory \r\n");
- // throw bad_alloc();
- ExitProcess(FALSE);
- return;
- }
- int _tmain()
- {
- set_new_handler (MyNewHandler);
- LPINT lpInt = NULL;
- INT nTotalBytes = 0, nAllocByte = 10000000;
- while(TRUE)
- {
- lpInt = NULL;
- lpInt = new INT[nAllocByte];
- nTotalBytes += nAllocByte * sizeof(INT);
- printf("Alloc Success!! (%d)\r\n", nTotalBytes);
- }
- // blah~ blah~
- }
'C++' 카테고리의 다른 글
STL의 원소로 배열(Array)을 사용할 수 없는 이유 ( 'initializing' : cannot convert from ...) (0) | 2009.10.25 |
---|---|
delete와 delete[]를 구분해서 사용해야 하는 이유 (0) | 2009.07.30 |
TIME_WAIT를 남기지 않는 세션종료 (Graceful Shutdown) (13) | 2009.04.19 |
멀티스레드 프로그래밍(Multithread Programming)에 관한 고찰 (2) (10) | 2009.04.11 |
멀티스레드 프로그래밍(Multithread Programming)에 관한 고찰 (1) (4) | 2009.03.24 |