Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

NULL

Posted on 2005-05-10
20
Medium Priority
?
300 Views
Last Modified: 2010-04-01
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
Comment
Question by:pb_india
  • 8
  • 5
  • 3
  • +3
19 Comments
 
LVL 37

Accepted Solution

by:
Harisha M G earned 100 total points
ID: 13971495
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
 
LVL 2

Author Comment

by:pb_india
ID: 13971577
What happens when new fails?
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 13971628
> What happens when new fails?

new fails only when there is no memory.. then it will return NULL
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:pb_india
ID: 13971743
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
 
LVL 37

Expert Comment

by:Harisha M G
ID: 13971820
This might be helpful...

http://support.microsoft.com/kb/q156808/
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 13971878
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
 
LVL 2

Author Comment

by:pb_india
ID: 13971888
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
 
LVL 37

Expert Comment

by:Harisha M G
ID: 13971904
If #include <new> is absent, it WILL return NULL
otherwise, it will return exception object
0
 
LVL 2

Author Comment

by:pb_india
ID: 13972128
Why?
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 13972230
The new operator is overloaded in that...
0
 
LVL 2

Author Comment

by:pb_india
ID: 13972280
I have not included <new>. So do u think it will go to NULL when allocation fails or it will throw bad exception?
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 13972352
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
 
LVL 15

Assisted Solution

by:efn
efn earned 100 total points
ID: 13974618
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
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 13984985
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
 
LVL 4

Expert Comment

by:furqanchandio
ID: 14051720
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
 
LVL 14

Expert Comment

by:wayside
ID: 15623179
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
 
LVL 15

Expert Comment

by:efn
ID: 15626044
I object.  Most of mgh_mgharish's comments were incorrect, at least for standard C++.
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 15640385
>> 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
 
LVL 15

Expert Comment

by:efn
ID: 15640646
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question