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 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.
Jump to Post>but since u told me that malloc throws null on failure
malloc doesn't throw null, it returns NULL. There's sufficient difference that you should avoid mixing the two.>this is what i wanted to say/ask...
Well, you didn't specify how it didn't work, but on one of my compilers, …
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.