To LUGNET HomepageTo LUGNET News HomepageTo LUGNET Guide Homepage
 Help on Searching
 
Post new message to lugnet.robotics.rcxOpen lugnet.robotics.rcx in your NNTP NewsreaderTo LUGNET News Traffic PageSign In (Members)
 Robotics / RCX / 2320
2319  |  2321
Subject: 
Re: Design by Contract (long post)
Newsgroups: 
lugnet.robotics.rcx
Date: 
Thu, 8 Jan 2004 16:52:02 GMT
Viewed: 
3352 times
  
In lugnet.robotics.rcx, Tim McSweeney wrote:
   <delurk>

The first definition of assert is the correct, standard definition. However there is a slight boo boo in the original post in the line:
assert (myptr = malloc (sizeof (mystruct));
Which will delete the malloc when NDEBUG is defined. It’s a classic mistake that bites even experienced programmers now and then.

the easy way round it is to write:
myptr = malloc (sizeof (mystruct));
assert(myptr!=NULL);
thus, the malloc always takes place, but the assert only occurs in debug builds.

D’oH! Thanks, Tim. I was very careful composing the original post, but added the example in as an after thought - Well spotted.

Iain.



Message has 1 Reply:
  Re: Design by Contract (long post)
 
(...) For more fun with asserts: See if your compiler supports the FILE and LINE macros (or something equivilant). #ifdef NDEBUG #define assert(x) ((void)0) #else #define assert(x) if (x) else assertfail(FILE,LINE); #endif your assert_fail function (...) (21 years ago, 9-Jan-04, to lugnet.robotics.rcx, FTX)

Message is in Reply To:
  Re: Design by Contract (long post)
 
(...) <delurk> The first definition of assert is the correct, standard definition. However there is a slight boo boo in the original post in the line: assert (myptr = malloc (sizeof (mystruct)); Which will delete the malloc when NDEBUG is defined. (...) (21 years ago, 8-Jan-04, to lugnet.robotics.rcx, FTX)

7 Messages in This Thread:


Entire Thread on One Page:
Nested:  All | Brief | Compact | Dots
Linear:  All | Brief | Compact

This Message and its Replies on One Page:
Nested:  All | Brief | Compact | Dots
Linear:  All | Brief | Compact
    

Custom Search

©2005 LUGNET. All rights reserved. - hosted by steinbruch.info GbR