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=
l
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
class constructor.
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.
For example:
class array
{
public:
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.
For example:
int *a = new int [1000];
if( a == )
exit();
I should use new_handler instead, but I have to use test condition to
find out if a is already allocated.
For example:
int *a = new int [1000];
// Do something
// Are ready to terminate your program. I have to clean up to
deallocate memory.
if( a != )
delete [] a;
// if( yes )
// exit();
// else ( no and contin=
ue )
;
return;
}
}
Prefer initialization rather than assignment. Your constructor should
be
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
TestMemory.
~array()
{
if( m_pArray != 0 )
There is no need for this check.
{
delete [] m_pArray;
m_pArray = 0;
And no need for this assignment.
}
}
bool TestMemory()
{
return m_bOutofMemory;
}
private:
int m_size;
int *m_pArray;
bool m_bOutofMemory;
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...
int main()
{
array *a = new array( 5 );
Why do you want to allocate a dynamically? This is just silly. Use
array a(5);
if( a->TestMemory() == true )
Why do you compare to true - this is just obfuscation. Prefer:
if( a->TestMemory() )
delete a;
// Continue the program after
// exit() function is not invoked.
system( "pause");
return 0;
Here you have a memory leak - because of the dynamic allocation of a.
Here is another good example.
#include <iostream>
using std::cerr;
using std::cout;
using std::endl;
#include <new>
using std::set_new_handler;
#include <cstdlib>
using std::abort;
using std::exit;
class array
{
public:
array( int size ) : m_size( size ), m_pLargeArray( new long long
[ size ] )
{
cout << "array::constructor -- count: " << ++s_ObjectCount << endl;
}
~array()
{
cout << "array::destructor -- count: " << s_ObjectCount-- << endl;
delete [] m_pLargeArray;
}
private:
long long *m_pLargeArray;
int m_size;
static int s_ObjectCount;
};
int array::s_ObjectCount = 0;
void customNewHandler()
{
cerr << "customNewHandler was called.\n";
cerr << "Display Error: Not enough Memory.\n\n";
cerr << "Your C++ project must be terminated." << endl;
// abort();
exit( EXIT_SUCCESS );
}
int main()
{
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;
return 0;
}
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.