![]() |
| | #1 |
| Confused Join Date: Sep 2001 Location: Sweden
Posts: 3,125
| Bad coding habits Code: SomeData* data = new SomeData();
if(data == NULL)
{
//Error
}
It's a bad habit I have, or?
__________________ MagosX.com Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime. |
| Magos is online now | |
| | #2 |
| Banned Join Date: Feb 2003 Location: Australia
Posts: 986
| Well if data is null and you attempt to use it, it should throw an exception anyway, which you could be handling in other ways. Personally, I wouldn't bother. If you do have an out-of-memory exception and the new doesn't work, you have much bigger problems. |
| nickname_changed is offline | |
| | #3 |
| & the hat of GPL slaying Join Date: Sep 2001
Posts: 5,732
| IIRC new is required to throw an exception unless you use the nothrow option. The way you currently have it structured it is pretty pointless. However you can move to a try/catch structure to deal with memory problems. |
| Thantos is offline | |
| | #4 | |
| Registered User Join Date: Aug 2003
Posts: 470
| Quote:
| |
| okinrus is offline | |
| | #5 |
| Registered User Join Date: Aug 2005
Posts: 1,265
| VC++ 6.0 does not throw an exception -- just returns NULL. But that is an old compiler that existed before current standards, so if you use that compiler don't rely on its behavior to be portable to others. And that is just one of the many many problems students come across while trying to learn the language on ancient compilers. and some universities even attempt to teach current c++ with compilers such as Turbo C and Turbo C++. Those two compilers existed long before there was a c++ standard. You wouldn't want an Model-T auto remairman to fix a 2006 car! So why would you want to learn c++ with ancient compilers Last edited by Ancient Dragon; 09-08-2005 at 08:35 PM. |
| Ancient Dragon is offline | |
| | #7 |
| Registered User Join Date: Aug 2005
Posts: 1,265
| That is kind of a two-faced explanation -- first it says that new never returns NULL, then turns around with the But's ... Never say never |
| Ancient Dragon is offline | |
| | #8 |
| Toaster Join Date: Aug 2001
Posts: 2,686
| Checking for NULL isn't worthshile unless you have an old compiler (as stated above). In my opinion, there generally isn't too much point in trying to catch the exception. If you're out of memory, you're rather hosed anyway. Might as well let the program abort.
__________________ The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop. |
| Zach L. is offline | |
| | #9 |
| It's full of stars Join Date: Aug 2001
Posts: 4,833
| >>> Might as well let the program abort. Why? In big programs with a lot of threads running which are allocating and deallocating memory all the time, if you can't grab the memory your thread needs now, why not back off for a few seconds and then try again. Better, control your memory pool with synchronisation objects.
__________________ Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream. |
| adrianxw is offline | |
| | #10 | |
| Banned Join Date: Feb 2003 Location: Australia
Posts: 986
| Quote:
Personally, I think a failed call to new() or malloc should throw an exception anyway, rather than just returning null. If you expect it to return null and handle it gracefully (though that would be hard considering you're out of memory) then wrap it in a try/catch. | |
| nickname_changed is offline | |
| | #11 | |
| & the hat of GPL slaying Join Date: Sep 2001
Posts: 5,732
| Quote:
And letting a program terminate due to an uncaught expection is just shoddy programming. Now it doesn't mean that every new needs to be wrapped in a try catch but it does mean that at some level you need to have a try catch that can deal with the problem. | |
| Thantos is offline | |
| | #12 |
| and the hat of marbles Join Date: May 2002 Location: Lund, Sweden
Posts: 2,041
| Code: int* x = new int; Code: int* x = new(nothrow) int;
__________________ Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling |
| Sang-drax is offline | |
| | #13 | |
| Toaster Join Date: Aug 2001
Posts: 2,686
| Quote:
I was thinking along the lines of your program bogging down the system resources. Assuming that you can't recycle anything, at that point, there isn't much that can be done. Chhers
__________________ The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop. | |
| Zach L. is offline | |
| | #14 | |
| C maniac Join Date: Aug 2004 Location: Cyberwarping to Middle Earth
Posts: 154
| Quote:
But, to get back on topic, I sometimes don't check for NULL, either .Code: SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO); . That much I can boast, because I never use memory allocation .But, in C++, I actually never check. That's because the last time I used C++ with memory allocation was with my Athabasca University assignment (COMP307, TME4, to be exact. I got an A+ on it), and I never use C++ unless I can help it. Java, on the other hand, is a diffrent story, as I am just learning. Anyway. The bottom line of this extra-long post is: try to check for memory allocation that doesn't work. KAWK Last edited by kawk; 09-12-2005 at 05:42 PM. | |
| kawk is offline | |
| | #15 | |
| Frequently Quite Prolix Join Date: Apr 2005 Location: Canada
Posts: 7,698
| Quote:
__________________ dwk Seek and ye shall find. quaere et invenies. "Simplicity does not precede complexity, but follows it." -- Alan Perlis "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra "The only real mistake is the one from which we learn nothing." -- John Powell Other boards: DaniWeb, TPS Unofficial Wiki FAQ: cpwiki.sf.net My website: http://dwks.theprogrammingsite.com/ Projects: codeform, xuni, atlantis, nort, etc. | |
| dwks is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| GLSL lighting. Bad normals? Bad shader? Something else entirely? | psychopath | Game Programming | 6 | 11-12-2005 11:57 AM |
| Poker bad beats | PJYelton | A Brief History of Cprogramming.com | 21 | 01-15-2005 11:42 PM |
| How bad is bad | caroundw5h | A Brief History of Cprogramming.com | 21 | 11-12-2004 09:26 AM |
| good news and bad news | Garfield | A Brief History of Cprogramming.com | 25 | 10-27-2001 07:31 AM |
| Bad code or bad compiler? | musayume | C Programming | 3 | 10-22-2001 09:08 PM |