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

Input data analysis, Create a class Word, representing a word. Two words sh...

Create a class Word, representing a word. Two words should be considered equal if they consist of the same sequence of letters and we consider upper case and lower case as equal. F

Where are longjmp and setjmp used in c++, Where are longjmp and setjmp used...

Where are longjmp and setjmp used in C++? -Setjmp and longjmp must not be used in C++. - Longjmp jumps out of the function without unwinding stack. This means that local obj

Write a program to sum any pair of numbers, Write a C++ program that reads ...

Write a C++ program that reads from keyboard 3 integers, with proper input prompt, and then displays the maximum sum of any pair of numbers from these three. If the 3 numbers are 5

Student, Ask4. Write a query to display the columns listed below. The query...

Ask4. Write a query to display the columns listed below. The query should list each customer in which the video rental is overdue. The Days_Overdue column should calculate the numb

Program for implementation of a data storage system, Introduction:  This...

Introduction:  This assignment requires a knowledge of variables (integers, char types), loops, conditionals, switch, functions, char arrays, string arrays, number arrays, struc

Inheritance, example of program to ad two numbers

example of program to ad two numbers

#titletrees.., #question.conversion of tree into binary tree.

#question.conversion of tree into binary tree.

Define some features of register storage class in c program, Define some fe...

Define some features of register storage class in c program? The feature of variable defined to be of register type all as follows: Storage - CPU registers Default initia

Arrays and pointers, i have an array of structs, and am trying to make a po...

i have an array of structs, and am trying to make a pointer that can point to a certain struct within the array

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