Go Down

Topic: What does abort() do? (Read 6769 times) previous topic - next topic

robitabu

If you do nothing special...

Code: [Select]
#include <assert.h>

void setup( void )
{
 assert( false );
}

void loop( void )
{
}


abort is called if the assertion fails.  stderr does not come into the picture.


That's all clear to me. That's how I already used it after you suggested it to me.
But ... that way, it does no more than calling abort(), right? Why should I even bother with calling assert() then? What's the point if it does'nt add anything new to the picture?

KeithRB

You can disable it with the #define.  Just a few posts up you said that was a good thing. 8^)

johncc

But now that it has been brought up, *is* there any way to redirect stderr to Serial, and then enable with #define __ASSERT_USE_STDERR , like int std c++ you could do

std::cerr.rdbuf(myOStream.rdbuf());

?

PeterH


But now that it has been brought up, *is* there any way to redirect stderr to Serial,


Yes, there is. You can associate stdin, stdout and stderr with the hardware Serial if you want (or any other device) by writing put and get functions for the device and then associating these functions with each stream by calls to fdevopen().
I only provide help via the forum - please do not contact me for private consultancy.

michinyon

That's the most interesting piece of information I have seen all week.

Coding Badly

Why should I even bother with calling assert() then?


Welcome to "Design by Contract" ... http://en.wikipedia.org/wiki/Design_by_contract ... I highly recommend Bertrand Meyer's book.

Nick Gammon


What does actually abort() do in Arduino?


From stdlib.h:

Code: [Select]

/** The abort() function causes abnormal program termination to occur.
    This realization disables interrupts and jumps to _exit() function
    with argument equal to 1. In the limited AVR environment, execution is
    effectively halted by entering an infinite loop. */
extern void abort(void) __ATTR_NORETURN__;


A simple error handler could be:

Code: [Select]

if (somethingBadHappens)
  {
  Serial.println (F("Bad thing XYZ happened."));
  Serial.flush ();
  exit (1);
  }


The flush call will ensure the message appears before you go into the infinite loop. The F macro saves wasting valuable RAM on the message.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Coding Badly


To ensure there is no ambiguity, I strongly agree with @PeterH...
http://arduino.cc/forum/index.php/topic,144756.msg1087549.html#msg1087549

It is never appropriate to use the AVR Libc abort.

Nick Gammon

Actually I got bitten in one of my libraries because exit() isn't always implemented. This might be neater:

Code: [Select]

if (somethingBadHappens)
  {
  Serial.println (F("Bad thing XYZ happened."));
  Serial.flush ();
  noInterrupts ();
  while (true) { }   // give up
  }
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Coding Badly


This should get you started...

Code: [Select]
#define __ASSERT_USE_STDERR
#include <assert.h>

extern void __assert(
    const char *__func,
    const char *__file,
    int __lineno,
    const char *__sexp )
{
  // Put your stuff here.
  // __func is the function name (setup)
  // __file is the module name (the name of your sketch)
  // __lineno is the source code line number of the assert
  // __sexp is the assert expression (as a C string)

  Serial.print( F( "ASSERT FAILURE: " ) );
  Serial.print( __file );
  Serial.print( F( ": " ) );
  Serial.print( __func );
  Serial.print( F( ": #" ) );
  Serial.print( __lineno );
  Serial.println();
  Serial.flush ();
  noInterrupts ();
  while (true) { }   // give up
}

void setup( void )
{
  assert( false );
}

void loop( void )
{
}


Bear in mind that each assert puts up to three string constants in SRAM.  A better version would keep the string constants in Flash.

GoForSmoke

Silly me, I thought I'd need an underlying OS to abort to!
But then when I write code, I expect users to make the occasional error... since 1980.
Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

Nick Gammon

Look on the bright side. Users expect programmers to make the occasional error, too. :)
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

PaulS

Quote
Users expect programmers to make the occasional error, too.

And I never disappoint them.

GoForSmoke

From the OLD (it was old in 85) Programmer's Joke Sheet:

Users should not complain about the code they get. They are lucky to get anything at all.

1980 is when I started writing programs for engineers. Error handling became a must!
Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

robitabu


Users should not complain about the code they get. They are lucky to get anything at all.


Priceless!!! :D

Go Up