• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 301
  • Last Modified:

NULL

When the new fails, do I get NULL or its a dead code. See below.

CAStorageSubSystemSettings*storageSubSystemSettings=NULL;
storageSubSystemSettings = new CAStorageSubSystemSettings();

            if(storageSubSystemSettings == NULL)                                    // Is This is a dead code. Control  will never reach here.????

            {

                       
                        return CACSS_FALSE;

            }
0
pb_india
Asked:
pb_india
  • 8
  • 5
  • 3
  • +3
2 Solutions
 
Harisha M GCommented:
Hi pb_india,
    If the free store (heap) is exhausted and there is no memory available, then the "new" operator will return NULL, otherwise it will return the address of the object created

Bye
---
Harish
0
 
pb_indiaAuthor Commented:
What happens when new fails?
0
 
Harisha M GCommented:
> What happens when new fails?

new fails only when there is no memory.. then it will return NULL
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
pb_indiaAuthor Commented:
But “new” will not return NULL on failure.  Rather it will throws a bad_alloc exception. So the we need to “catch” bad_alloc exception. No?

0
 
Harisha M GCommented:
This might be helpful...

http://support.microsoft.com/kb/q156808/
0
 
Harisha M GCommented:
Here is a nice description to catch bad_alloc...
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcstdlib/html/vclrf_new_badalloc_class.asp

Here is a good description for various versions of new
http://www.roguewave.com/support/docs/sourcepro/stdlibref/operatornew.html

One more thing..

HAVE YOU #INCLUDED <NEW> ??
0
 
pb_indiaAuthor Commented:
So it does throw exceptiona and not return null (like you previously mentioned)

"The first operator new will attempt to allocate memory and if it fails, will throw an exception."
0
 
Harisha M GCommented:
If #include <new> is absent, it WILL return NULL
otherwise, it will return exception object
0
 
pb_indiaAuthor Commented:
Why?
0
 
Harisha M GCommented:
The new operator is overloaded in that...
0
 
pb_indiaAuthor Commented:
I have not included <new>. So do u think it will go to NULL when allocation fails or it will throw bad exception?
0
 
Harisha M GCommented:
I think it throws NULL since it doesn't know anything about bad_alloc. Once you include that, it will be overloaded and is capable of throwing various types of errors.
0
 
efnCommented:
The default behavior is for it to throw an exception when allocation fails.  Some older compilers may not do this.

If you want it to return a null pointer instead of throwing an exception, you can make that happen by coding like this:

storageSubSystemSettings = new (std::nothrow) CAStorageSubSystemSettings;

See also:

http://www.gotw.ca/publications/mill15.htm

http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_11987379.html
0
 
PaulCaswellCommented:
But for safety and portability it is better to handle both situations.

1. Always check for NULL.
2. Catch all exceptions.
3. Always check for NULL.

Paul
0
 
furqanchandioCommented:
hi

visual C++ 6 doent return null but throws an exception

ANSI standard states that a compiler should return NULL not throw an exception
0
 
waysideCommented:
No comment has been added to this question in more than 21 days, so it is now classified as abandoned..
I will leave the following recommendation for this question in the Cleanup topic area:

Split between mgh_mgharish and efn

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

wayside
EE Cleanup Volunteer
0
 
efnCommented:
I object.  Most of mgh_mgharish's comments were incorrect, at least for standard C++.
0
 
Harisha M GCommented:
>> Most of mgh_mgharish's comments were incorrect, at least for standard C++.

Standard C++ doesn't define anything about exception. If you include <new> then the exception will be raised; otherwise NULL will be returned as it was the case at the beginning.

Remember: VC++ is NOT Standard C++

OK, even if raises the exception, what will be 'returned' to the variable (lvalue) ? It is NULL
0
 
efnCommented:
The international standard for C++ (ISO/IEC 14882:2003) says that operator new is implicitly declared at global scope like this:

void* operator new(std::size_t) throw(std::bad_alloc);

(section 3.7.3, clause 2)

Note that it declares that the function may throw a std::bad_alloc exception.  The standard goes on to say that unlike the new operator, std::bad_alloc is not implicitly declared.  Consequently, if you want to catch the exception, you must include the <new> header to get the type declared.

The standard does not say anything about the functionality of the new operator changing as a consequence of including the <new> header.  If that makes a difference in mgh_mgharish's environment, it is probably because the implicitly declared new operator is nonstandard and including the header causes the compiler to use a different operator function that is standard.

Section 18.4.1.1 specifies the behavior of the new operator.  Clause 3 says:

"Required behavior: Return a non-null pointer to suitably aligned storage (3.7.3), or else throw a bad_alloc exception. This requirement is binding on a replacement version of this function."

If the new operator raises an exception, it does not return any value, so the variable that would receive the returned value is unchanged.  If it was initialized to zero, it is still zero; if it was uninitialized garbage, it is still garbage.

See also:

C++ Memory and Resource Management by Stephen Dewhurst.
http://www.informit.com/articles/article.asp?p=30642&seqNum=2

To New, Perchance To Throw, Part 1 by Herb Sutter (cited above)
http://www.gotw.ca/publications/mill15.htm

Dynamic Memory by Juan Soulie
http://www.cplusplus.com/doc/tutorial/dynamic.html
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 8
  • 5
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now