Do i have to check for null after p = new fred()?, C/C++ Programming

Assignment Help:

A: No. (Excluding if you have an old compiler, you might have to force the new operator to throw an exception if it runs out of memory.)

This turns out to be a real pain to always write down explicit NULL tests after each new allocation. Code like the following is extremely tedious:

Fred* p = new Fred();

if (p == NULL)

throw std::bad_alloc();

If your compiler doesn't support (or if you reject to use) exceptions, your code may be even more tedious:

Fred* p = new Fred();

if (p == NULL) {

std::cerr << "Couldn't allocate memory for Fred" << std::endl;

abort();

}

In C++, if the runtime system can't allocate sizeof(Fred) bytes of memory throughout p

= new Fred(), a std::bad_alloc exception will thrown. Unlike malloc(), new never returns

NULL!

Then you should simply write:

Fred* p = new Fred(); // No require to check if p is NULL

Though, if your compiler is old, it might not yet support this. Determine by checking your compiler's documentation under "new". If you hold an old compiler, you might have to force the compiler to have this behavior.

 

Note: If you are using Microsoft Visual C++, to obtain new to throw an exception while it fails you have to #include some standard header in at least one of your .cpp files. For instance, you could

#include (or or or ...).

 

 


Related Discussions:- Do i have to check for null after p = new fred()?

Program for stack over flow vulnerability., Imagine that the server program...

Imagine that the server program is a setuid program owned by the root, then after you penetrate the program with a shell running, you become the ROOT and you can remove the whole f

Define the logical operators in c language, Define the Logical Operators in...

Define the Logical Operators in c language? The Logical operators deals with the ways the relationships are able to connected. C has the following three logical operators

Roman code python help , In general, Roman numerals can be converted mathem...

In general, Roman numerals can be converted mathematically by simply assigning a numerical value to each letter, according to the chart below, and calculating a total: M=1000 | D=5

Padovan sequence, write a program that counts the number of occurrences of ...

write a program that counts the number of occurrences of the string in the n-th Padovan string P(n)

Is it possible to create your own header files, Is it possible to create yo...

Is it possible to create your own header files? - Yes, it's possible to create a customized header file. - To do this, you just need to include function prototypes that you

Write a c program to input a floating point number, Step 1 Define the start...

Step 1 Define the start of the program    It should be noted that within C all commands should end in a semi-colon. For most of your programs the definition of a program header as

Explain the following object oriented methodologies, Question 1 Explain th...

Question 1 Explain the following Object Oriented Concepts with suitable examples for each Question 2 Explain the following Object Oriented Methodologies Question 3 Descr

I want im messenger, I would like to get you to collaborate in my project. ...

I would like to get you to collaborate in my project. We want a C programmer to Develop applications for Windows, the first application may be an Instant messenger encrypted in a w

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd