Go Down

Topic: Problems with "Exit;" (Read 953 times) previous topic - next topic

LMI_at_work

I tried to exit a loop with exit. Like this.
Code: [Select]
  if (vara>171)
    {
    Dacout0(vara);
    exit;
    }

It did not work. Break works, so there is not a problem but I'm curious. I looked up the word Exit in C++ web references and it is there and there are no errors from compiler either.

pYro_65

use 'break'
'exit' is not part of loops.
http://www.cplusplus.com/reference/clibrary/cstdlib/exit/

dxw00d

There's nowhere to exit to, on a system with no OS.

LMI_at_work

http://www.cplusplus.com that's where I looked it. But I guess I miss understood it. 'Process/function/loop all the same.' But what does exit do in Arduino or GCC, what assembly code it creates. I wonder why does GCC not know that embedded cpu has no OS to exit. (And give an error.)

PaulS

Quote
I wonder why does GCC not know that embedded cpu has no OS to exit. (And give an error.)

The compiler does not know where the object module it is creating is going to be used.

The linker that creates the hex files from the object modules does not know that the object module contains code derived from the exit() function.

Sometimes, it really is necessary for the programmer to know what is reasonable on the platform they arre developing for.

Nick Gammon

Code: [Select]
    exit;


That is not how you call the exit function.

If you called it correctly it would make your program stop executing.
http://www.gammon.com.au/electronics

kg4wsv

exit() is a unix system call (i.e. a call to the unix kernel, as opposed to a simple library call) that terminates the process that calls it.

The arduino doesn't run unix, or any other oeprating system, so it doesn't know about exit() any more than it knows about fork() , exec(), ioctl(), etc...

-j

Nick Gammon

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

Tom Carpenter

The correct function call is:
Code: [Select]
exit(0);
Which is happily recognised by arduino as it is C++. It produces a very simple pair of instructions:
Code: [Select]
0000029a <_exit>:
29a: f8 94        cli

0000029c <__stop_program>:
29c: ff cf        rjmp .-2      ; 0x29c <__stop_program>

which in C++ speak is essentially:
Code: [Select]
cli(); //prevent interrupts
while(1); //Do nothing forever (or at least until reset)
~Tom~

LMI

Thank you all. This was interesting to know

Go Up