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

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 ...).

 

 

Posted Date: 3/19/2013 8:39:01 AM | Location : United States







Related Discussions:- Do i have to check for null after p = new fred()?, Assignment Help, Ask Question on Do i have to check for null after p = new fred()?, Get Answer, Expert's Help, Do i have to check for null after p = new fred()? Discussions

Write discussion on Do i have to check for null after p = new fred()?
Your posts are moderated
Related Questions
Program is to define a class as employee: Write a program to define a class as employee and collect information about them by using classes and object class employee   {

//Create a custom calculator program capable of reading the input stream of an expression //and make basic computations to provide answer. //Your program will ask the user to


Hello I''m new to programming, and I''m making now my 1st program. My question is how to put substr in textbox that question mark should be at the end of sentence? And 2nd question

Depreciation to a salvage value of 0. For tax purposes an item may be depreciated over a period of several years, n. With the straight line method of depreciati

railway reservation programming

Write a program to create a binary file and store the following data "hello", 0x0030,'1', 1.234  Using visual studio examine the binary file and note how the day is stored   Answe

Rules of Operator Overloading It is a function defined to an operator with new term or meaning. It cannot produce new operator. It cannot modified the meaning of th

void main() { int *p, *q, i ; p=(int*)100; q=(int*)200; i=q-p; printf("%d",i); } }

getting a palindrome using minimum replacement