News for hackers. Stuff that matters.
Re:Range-Checks and Recklessness by fexl on Sunday March 23, 2014 @04:16PM (#7454933)
Assertion failure messages are sent to stderr. Production code can capture that as it wishes.
Re:Range-Checks and Recklessness by dllthomas on Sunday March 23, 2014 @04:43PM (#7455062)
The assert from assert.h is a macro that completely elides the check if built with NDEBUG. Assuming you're not using some other assert(), and are building your production build with NDEBUG (which is typically part of what's meant by "a production build", though you can certainly disagree with the practice), production code cannot capture it as no messages will be generated.
Re:Range-Checks and Recklessness by fexl on Monday March 24, 2014 @10:38AM (#7458430)
Right now, in my code, assert(0==1) produces this output to stderr:

  run: run.c:422: main: Assertion `0==1' failed.
  Aborted
That is what I want.

You are correct. If I build with -DNDEBUG=1, then the assert is completely ignored: no error message, and no abort. That is not what I want. Therefore I will not build with -DNDEBUG=1.

I will certainly not introduce "logging to a file" as a concept inside buf.c. There is no need for the pure data manipulation code in buf.c to know anything about files or stdio, or a specific name of a log file.

If the assert from assert.h does not do what I want, then I'll make a version that does. However, that is a moot point, since right now it does what I want.

Re:Range-Checks and Recklessness by dllthomas on Monday March 24, 2014 @10:50AM (#7458491)
I had no objection to the approach, just the lack of a note about assumptions it relied upon.
Re:Range-Checks and Recklessness by fexl on Monday March 24, 2014 @12:30PM (#7459160)
No it's a good point actually, and it pays to know your context. Frankly, looking at the 118 lines in assert.h, it does considerably more than I really need.

Instead of this:

  assert(buf->pos < buf->str->len);
I could just do this instead:

  if (buf->pos >= buf->str->len) die("bad pos");
If the die message is unique, I don't really need to include __FILE__, __LINE__, and the expression itself in it.

I could even do this, though it's probably overkill for something that should never happen anyway:

  if (buf->pos >= buf->str->len)
      die("bad pos %d %d", buf->pos, buf->str->len);
Re:Range-Checks and Recklessness by dllthomas on Monday March 24, 2014 @12:32PM (#7459179)
Absolutely, though I tend to throw __FILE__ and __LINE__ in everything, so I can step through the sources of messages by just pulling messages into my vim quickfix buffer. Again, totally depends on context, though.
Re:Range-Checks and Recklessness by Roboprog on Sunday March 23, 2014 @05:56PM (#7455380)
For in-house code in a manufacturing company, we usually left all the debug symbol and assertions in our C code. For writing a product that shipped to others at a dev tools company, we usually turned off the debugging, turned on NDEBUG and full optimization. (although the product had checking for things built into its logic, and a lot of testing -- C being used as an assembler surrogate -- as we were writing developer tools, rather than "enterprise" whack-it-together-on-no-time-budget pile-age)

"Production build" varies by environment, TMTOWTDI.

Re:Range-Checks and Recklessness by dllthomas on Sunday March 23, 2014 @06:18PM (#7455474)
Absolutely the case. I just think it's wrong to assume - without stating it - that NDEBUG will be off "in production".