Συντονιστής: konnn

migf1 έγραψε:Μπορεί, αλλά το return δεν κάνει cleanup. Το ANSI standard επιβάλει η main() να τερματίζει με exit() γιατί η exit() κάνει cleanup τυχόν αμέλειες του προγραμματιστή πριν επιστρέψει στο OS (π.χ. κλείνει ανοιχτά αρχεία, καθαρίζει μνήμες, κλπ)

simosx έγραψε:migf1 έγραψε:Μπορεί, αλλά το return δεν κάνει cleanup. Το ANSI standard επιβάλει η main() να τερματίζει με exit() γιατί η exit() κάνει cleanup τυχόν αμέλειες του προγραμματιστή πριν επιστρέψει στο OS (π.χ. κλείνει ανοιχτά αρχεία, καθαρίζει μνήμες, κλπ)
Η return είναι που προκαλεί να κλείσουν ανοιχτά αρχεία, και έχει μεγαλύτερο νόημα σε C++ όπου τα αντικείμενα καλούν τον αποδομητή (destructor) στο return.
Αν εκτελέσεις exit(), τότε η εφαρμογή παύει άμεσα να λειτουργεί.
Για τη διαχείριση της μνήμης, ό,τι μνήμη έχει δεσμευθεί σε ένα πρόγραμμα επιστρέφει στο σύστημα κατά τον τερματισμό. Εξαίρεση είναι όταν η εφαρμογή γίνει ζόμπι.
Δες περισσότερα π.χ. στο http://stackoverflow.com/questions/4614 ... it-in-main
Μπορείς να δώσεις σύνδεσμο προς το ANSI C όπου περιγράφει τα παραπάνω; Υποψιάζομαι ότι η συμπεριφορά της exit() που αναφέρεις σχετίζεται με ιδιαιτερότητα συγκεκριμένων μεταγλωττιστών (π.χ. για ενσωματωμένα συστήματα), και όχι για GCC.
Μπορείς να δοκιμάσεις με ένα πρόγραμμα-δείγμα που ανοίγει αρχεία και δεσμεύει μνήμη. Με μεταγλώττιση με gcc -S μπορείς να δεις τον παραγόμενο κώδικα σε γλώσσα μηχανής, και να εντοπίσεις αν πραγματικά η exit() κάνει κάτι περισσότερο.




έγραψε:5.1.2.2.3 Program termination
If the return type of the main function is a type compatible with int, a return from the
initial call to the main function is equivalent to calling the exit function with the value
returned by the main function as its argument;10) reaching the } that terminates the
main function returns a value of 0. If the return type is not compatible with int, the
termination status returned to the host environment is unspecified.
Forward references: definition of terms (7.1.1), the exit function (7.20.4.3).
έγραψε:A process terminates normally when its program signals it is done by calling exit. Returning from main is equivalent to calling exit, and the value that main returns is used as the argument to exit.
έγραψε:void abort();
Terminates program abnormally, by calling raise(SIGABRT).
void exit(int status);
Terminates program normally. Functions installed using atexit are called (in reverse order to that in which installed), open files are flushed, open streams are closed and control is returned to environment. status is returned to environment in implementation-dependent manner. Zero or EXIT_SUCCESS indicates successful termination and EXIT_FAILURE indicates unsuccessful termination. Implementations may define other values.

clepto έγραψε:δεν είναι για σχολή, μόλις τελείωσα την πρώτη λυκείου...
ευχαριστώ θα τα κοιτάξω όλα!

medigeek έγραψε:Υποθέτω είναι το ίδιο:
http://www.open-std.org/jtc1/sc22/WG14/ ... /n1256.pdf
[snip]
http://www.gnu.org/s/hello/manual/libc/ ... ermination
[snip]
http://www.utas.edu.au/infosys/info/doc ... html#abort
[snip]
