as new throws exception bad_alloc...does malloc also throws exception of some kind???
SpS
34
Posting Pro
Recommended Answers
Jump to PostNo, malloc returns a null pointer if it fails, just like in C. So you should always test for success like this:
p = malloc ( n * sizeof *p ); if ( p == NULL ) { // Handle malloc failure }
Of course, in C++ …
Jump to Postyes it is standard, but it took a while for some compilers to catch up, most notably msvc6. There is also a nothrow version of new specified by the standard.
Jump to Post>#include<malloc.h>
Get with the times, malloc is declared in cstdlib (or stdlib.h for people who insist on using ancient compilers) and has been for a long time.>n= (int*)malloc(-1);
It's surprising that this actually works, but it's possible (thought not incredibly bright) to allocate that much memory. Remember that …
Jump to Post>it again failed but never reached at if (n==NULL) condition and didn't threw any exception
Oh, I see. You crashed your program by causing the memory manager to panic and you're wondering why your code didn't magically stop it. I can understand your confusion.
All 15 Replies
Narue
5,707
Bad Cop
Team Colleague
SpS
34
Posting Pro
SpS
34
Posting Pro
Stoned_coder
6
Junior Poster
Narue
5,707
Bad Cop
Team Colleague
SpS
34
Posting Pro
Narue
5,707
Bad Cop
Team Colleague
SpS
34
Posting Pro
Narue
5,707
Bad Cop
Team Colleague
Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster
Narue
5,707
Bad Cop
Team Colleague
Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster
Narue
5,707
Bad Cop
Team Colleague
Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster
Narue
5,707
Bad Cop
Team Colleague
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.