Re: Test Dynamic Memory
On Dec 13, 3:16 pm, peter koch <peter.koch.lar...@gmail.com> wrote:
On 13 Dec., 21:10, Immortal Nephi <Immortal_Ne...@hotmail.com> wrote:
I notice that some codes do not have to test dynamic memory to
determine if dynamic memory is available or not. It is said in Deite=
How to Program C++ book.
If it says so, it is a bad book.
Is it necessary to test dynamic memory if memory is very small like
less than 256 bytes? The test should fail and prevent you from doing
If new fails, standard behaviour is to throw std::bad_alloc. Some very
old compilers got that wrong, noticeably VC6.0. If you use that
compiler and can't change, you can change a setting to make it
standards-compliant in that respect.
array class is already constructed before dynamic memory is tested.
If it fails, error message is displayed before exit() function
terminates the program. If I decide to allow the program running
after dynamic memory reported out of memory, then array class should
use destructor to clean up all data members and release array class
from the memory.
You do realize, that you are just creating a dumped-down version of
std::vector? So your class is useless if not as an exercise. Also, you
are missing a lot of stuff - an assignment operator and a copy
constructor needs to be written.
I build my own class. I don't need to use vector because I don't need
all the features such as search or insert element / remove element. I
suppose to put an assignment operator and a copy constructor to
private if I don't need to use them.
array( int size )
m_pArray = new int[ size ];
m_size = size;
if( m_pArray == 0 )
// Display Error Messag=
e -- out of memory
ion and try again or
// use exit() function
This behaviour is better delegated into the new_handler function.
Are you saying? I create some classes to build different objects and
put them into memory. For example, 5 different classes are
constructed successfully and are stored in memory. They will be
destructed after the program exits successfully. If one class
attempts to allocate more memory, then bad allocation must be returned
before new_handler exception is thrown. Correct?
I do not need to test to find out if new keyword returns a pointer
with bad allocation.
int *a = new int ;
if( a == )
I should use new_handler instead, but I have to use test condition to
find out if a is already allocated.
int *a = new int ;
// Do something
// Are ready to terminate your program. I have to clean up to
if( a != )
delete  a;
// if( yes )
// else ( no and contin=
Prefer initialization rather than assignment. Your constructor should
array( int size ): m_size(size),m_pArray(new int[ size ])=
Also notice that m_bOutofMemory only gets initialised in case there is
a memory leak, so you are likely to run into undefined behaviour in
if( m_pArray != 0 )
There is no need for this check.
delete  m_pArray;
m_pArray = 0;
And no need for this assignment.
That hungarian notation really creeps me out.
Please explain why you dislike hungarian notation. It provides you
more information when you need to know because there may be hundreds
or thosuands of variables. Anyway...
array *a = new array( 5 );
Why do you want to allocate a dynamically? This is just silly. Use
if( a->TestMemory() == true )
Why do you compare to true - this is just obfuscation. Prefer:
if( a->TestMemory() )
// Continue the program after
// exit() function is not invoked.
Here you have a memory leak - because of the dynamic allocation of a.
Here is another good example.
array( int size ) : m_size( size ), m_pLargeArray( new long long
[ size ] )
cout << "array::constructor -- count: " << ++s_ObjectCount << endl;
cout << "array::destructor -- count: " << s_ObjectCount-- << endl;
delete  m_pLargeArray;
long long *m_pLargeArray;
static int s_ObjectCount;
int array::s_ObjectCount = 0;
cerr << "customNewHandler was called.\n";
cerr << "Display Error: Not enough Memory.\n\n";
cerr << "Your C++ project must be terminated." << endl;
exit( EXIT_SUCCESS );
set_new_handler( customNewHandler );
array a1( 50000000 ); // Success
array a2( 50000000 ); // Success
array a3( 50000000 ); // Success
array a4( 50000000 ); // Success
array a5( 50000000 ); // Failed -- bad memory allocation.
cout << "C++ project runs successful." << endl;
If bad memory allocation is detected, then will exit() function does
its own responsibility to deallocate a1, a2, a3, and a4 from the
memory before program is terminated?
Do you suggest me to put new_handler exception in main.cpp only? All
other headers and source codes are included in main.cpp. new_handler
exception takes care to clean up the rest.