article

Read: "Avoiding new operator failure" from my new book-> " 0 error(s) - 0 warning(s)"

Email
Submitted on: 1/9/2015 1:17:00 PM
By: ahmed mohamed alaa (from psc cd)  
Level: Advanced
User Rating: By 2 Users
Compatibility: Microsoft Visual C++
Views: 1928
 
     Avoiding runtime errors caused by the "new" operator using exception handling with simple three different methods

 
				This article is extracted from my new book ; " 0 error(s) - 0 warning(s) " which will be published soon...
"In any programming language, arrays are the most popular and practical way for storing data, however, when allocating memory to be dedicated to an array in a C++ code, you must decide the number of elements in it when defining the array, without postponing this step to runtime.
 One might think of a code such as: 
 cout<<”Enter the size of array”<>array_size;
 int A[array_size]; 
 However, such code is not acceptable by the compiler as it demands a constant (static) memory allocation that can’t be changed in course of program execution… 
 This problem can be solved thanks to the “new” operator , as in most programs the size of the array is not decided at the compile time, we use the “new’’ to capture a whole memory block and dedicate it to the array , such style is called “dynamic memory allocation” …
Now we can write the following code:
 
 cout<<”Enter the size of the array”<>array_size;
 int* array_A ; 
 array_A = new int [array_size]; 
In many occasions, the new operator fails to allocate memory for many reasons that are beyond the scope of this article, we will discuss 3 methods for avoiding this problem, and process new failures.
First method: The classical method!! 
 A classical method is to check whether the pointer is equal to zero after dynamic allocation , the pointer is equal to zero if the new operator fails to allocate memory this can be checked by a very simple if statement after using the new operator.
Second method: Handling the exception! 
 Using exception handling statements, we can write a try – catch statement that catches an exception called bad_alloc defined in the  library this can be illustrated by this code:
 
 #include
 Using std::bad_alloc 
 
 Void main (void)
 {
Double *pointer [100]; 
try
{
for (int i = 0; i < 100; i++)
{
Pointer[i] = new double [1000000];
cout<<”Managed to allocate for”< header and use “set_new_handler” , this function takes a pointer to function as an argument (a function with no arguments and returns void) , once registered , the passed function is called automatically instead of “bad_alloc” , to avoid runtime errors…"
To download the original text in Word format , visit my website AhmedMAlaa.webs.com, the book will be published soon and available at Amazon.cm


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Advanced category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.