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()?

C program to add two complex numbers , Aim: To implement a program to add ...

Aim: To implement a program to add two complex numbers using constructors. Code:                       class complex {             int real;             int img;

Friend funtion.., Is friend function can be harmfull to make a secure progr...

Is friend function can be harmfull to make a secure program/sofware?

Define passing structure to a function, Define Passing Structure to a Funct...

Define Passing Structure to a Function? A structure is able to be passed as a function argument identical to any other variable. If we are merely interested in one member of a

Board coloring, in this problem you are given a board in which some of the ...

in this problem you are given a board in which some of the elements are placed as shown in below diagram.each element represent a color.fill the other element in the board,such tha

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

Chelo, need some help with finishing a program

need some help with finishing a program

Define bitwise left shift and right shift operators, Define Bitwise Left Sh...

Define Bitwise Left Shift and Right Shift Operators: >? The bitwise shift operators shift their first operand right (>>) or left ( // Illustration of the bitwise right shi

Prepare a mt4 ea to clone mt4 trades to a binary options, Prepare a MT4 EA ...

Prepare a MT4 EA to clone MT4 Trades to a Binary Options Platform Project Description: I want an EA that clones MT4 Trades to Globaltrader365, GT Options and if possible othe

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