Pages: [1]   Go Down
Author Topic: Problems with "Exit;"  (Read 893 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 31
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I tried to exit a loop with exit. Like this.
Code:
  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.
Logged

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 70
Posts: 2171
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 31
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.)
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49343
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18792
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
    exit;

That is not how you call the exit function.

If you called it correctly it would make your program stop executing.
Logged


0
Offline Offline
Faraday Member
**
Karma: 8
Posts: 2526
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18792
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

(And give an error.)

What error would that be?
Logged


Leeds, UK
Offline Offline
Edison Member
*
Karma: 80
Posts: 1726
Once the magic blue smoke is released, it won't go back in!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0000029c <__stop_program>:
 29c: ff cf        rjmp .-2      ; 0x29c <__stop_program>
which in C++ speak is essentially:
Code:
cli(); //prevent interrupts
while(1); //Do nothing forever (or at least until reset)
Logged

~Tom~

Turku
Offline Offline
Full Member
***
Karma: 1
Posts: 224
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you all. This was interesting to know
Logged

Pages: [1]   Go Up
Jump to: