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

Decodethecode.h, decodes the sending smuggler string from the number format...

decodes the sending smuggler string from the number format into plain text

Sorted linked list , Node *orderedInsert(Node *p, int newval); /* Allocates...

Node *orderedInsert(Node *p, int newval); /* Allocates a new Node with data value newval and inserts into the ordered list with first node pointer p in such a way that the data va

What does it mean to declare a destructor as static, d) a "static destructo...

d) a "static destructor" is a static member function of the class which accepts one argument - a pointer to the object of that class to be cracked. It is probably utilized along wi

Webcam driver static library or dll required for windows, Webcam Driver sta...

Webcam Driver static library or DLL required for Windows I want a .dll or .lib file using which i will take a snapshot from the webcam and it can't turn on the webcam LED while

C program to reverse the elements of array, C program to reverse the elemen...

C program to reverse the elements of array: #define rows 3 #define cols 3 void main() {                 int i=0,j=0;                 int arr[rows][cols];

Program to calculate average of marks, Program for calculate average of tot...

Program for calculate average of total marks: #include using namespace std; void print(int marks_arr[],int cnt) { int ind[cnt]; int i=0; int j=0; int k=0;

Find the largest and smallest average, Use the above problem and have ...

Use the above problem and have the program print the biggest and smallest sales for each employee for everyday of the month. Program should also find the largest and

Boardcoloring., Smugglers are becoming very smart day by day. Now they have...

Smugglers are becoming very smart day by day. Now they have developed a new technique of sending their messages from one smuggler to another. In their new technology, they are send

Program is to perform all the functions of super bazaar, Program is to perf...

Program is to perform all the functions that are performed in a super bazaar: class stock   {   private:     int itno;     char itname[20];     char brname[20];

Palindrome, A palindrome is a string that reads the same from both the ends...

A palindrome is a string that reads the same from both the ends. Given a string S convert it to a palindrome by doing character replacement. Your task is to convert S to palindrome

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