2

I belong to a newly setup college, and have teachers who are not so talented. I personally have spotted them making mistakes many times. This is because they themselves have false knowledge. Hence I teach programming and networking to myself. As I cannot rely on them.

Now since this is the scenario, I have fallen into a big doubt.

One of my teachers of the subject Analysis Of Algorithms, had said, "when int main() returns 0, all the buffers are closed, all the resources utilized are cleared off, and space is created for other programs. Hence it is a good practise to return 0". To which I argued, "mam, we always knew that it is only to indicate that the program has completed exexution successfully" To which she said, "you are partly correct".

Which clearly means she meant returning a non zero doesnt release resources.

Now who is correct, I or she? Should I ask for a money back?

Aditya Singh
  • 127
  • 8
  • 1
    So her allegation is, if you return 1, then resources aren't released? – dsolimano Jun 12 '14 at 13:46
  • @dsolimano she did not say that. But probably she meant that. – Aditya Singh Jun 12 '14 at 13:47
  • 13
    create even more free resources by returning -1! – risingDarkness Jun 12 '14 at 13:48
  • @risingDarkness sorry didnt get you – Aditya Singh Jun 12 '14 at 13:49
  • Nice way to start a question. Can you elaborate on that college of yours, so as to warn the others? – barak manos Jun 12 '14 at 13:49
  • @barakmanos lol. I dont think thats important. That was just to explain the scenario. Lol – Aditya Singh Jun 12 '14 at 13:50
  • 1
    Just ask the teacher what happens when you return a value different to `0`. But note that there is a bunch of stuff that can still happen in POSIX systems after `main` returns, before resources are returned to the OS. – juanchopanza Jun 12 '14 at 13:51
  • @juanchopanza Probably, she will leave teaching. By the way may I know what does the OS do with those returns? – Aditya Singh Jun 12 '14 at 13:52
  • IMHO, typical mainstream OSes will typically free the resources owned by the program *once the program ends*... but this cannot be an universal expectation and different environments (e.g. embedded?) can have different results. It seems funny that the OS behaviour towards cleaning up or not will depend on the return value. – jsantander Jun 12 '14 at 13:53

5 Answers5

17

The value returned by main is given to the operating system. Nothing in the C or C++ standards state that different things happen based on that value.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • 2
    This is the correct answer. The C/C++ standard does not care what happens when the abstract machine gets `0` or any other returned value. – Shoe Jun 12 '14 at 13:53
  • It is true that some clean up is performed, such as flushing buffers, right? – Fiddling Bits Jun 12 '14 at 13:53
  • 1
    @Jefffrey but what doesthe OS do with those integers? How differently does it behave when program is successful(return 0) or unsuccessful(return non zero)? – Aditya Singh Jun 12 '14 at 13:57
  • @user3182942 It depends on the OS, not on the programming language. But I would expect the OS to do the same, regardless of return status. – juanchopanza Jun 12 '14 at 13:58
  • 1
    The OS can do anything it wants to do. A shell might show the return code only if it's non-zero. An even more intelligent shell might interpret it depending on the program, and display a human-readable error message (see f.i. [here](http://linux.die.net/man/8/e2fsck)). The point is that the ***C language specification*** does not specify what should happen, from its point of view returning `0` or any other integer has exactly the same meaning. – peppe Jun 12 '14 at 13:58
  • 1
    @peppe ok. So you mean, regardless of returning any integer, positive, negative or 0, the OS will do what it has to do? – Aditya Singh Jun 12 '14 at 14:03
  • @user3182942 What it ***wants*** to do. – Shoe Jun 12 '14 at 14:04
  • @Jefffrey ok. So is the behavior different for different shells we use? And how does OS X, linux and windows behave with those returns? Any idea? – Aditya Singh Jun 12 '14 at 14:06
  • @user3182942 You'll have to take a look at the OS manual or specification. – Shoe Jun 12 '14 at 14:07
  • @Jefffrey they dont have anything regarding my questions. I am asking this after a long unsuccessful search. – Aditya Singh Jun 12 '14 at 14:10
  • @user3182942 yes, they do. Because it's their job to document this behavior. – Shoe Jun 12 '14 at 14:14
  • @Jefffrey can you please give me the link? Specifically with a thread with C/C++ returning 0 – Aditya Singh Jun 12 '14 at 14:20
3

Your teacher is 100% wrong.

You got it right.
You should ask for your money back.

MIT has nice online courses which would be more accurate.

abelenky
  • 63,815
  • 23
  • 109
  • 159
2

When main function returns open files are closed and output streams are flushed. C does NOT specify if memory allocated by malloc is freed.

ouah
  • 142,963
  • 15
  • 272
  • 331
  • 1
    I have experimented with that. For example,if you write in a file, the writing is successful only if you close the file. If you leave it unclosed, the writing remains in the buffer and flushes. File writing doesnt take place if you dont close it. So,in my 1st test run, when i did not close the file, even though i returned 0, the writing did not take place. And in my 2nd test run of the program,i closed the file and returned 89. But file writing was successful. So i concluded that what you said was wrong. M sorry but thats true. – Aditya Singh Jun 12 '14 at 14:18
  • 1
    @user3182942 Probably the write buffer is not flushed to file. But that wasn't the question. Instead it's in question if the buffer is released. – harper Jun 12 '14 at 14:37
  • @harper but if its not flushed, thats the case of "sometimes". But this "always" happens. – Aditya Singh Jun 12 '14 at 14:39
  • @user3182942, big question would be, what platform you used for your test. A conforming C implementation must close files. Some non-conforming implementations might not. (Closing of files is not done implicitly if you `abort`, but that is a different story.) – Jens Gustedt Jun 12 '14 at 14:49
  • @user3182942 *So i concluded that what you said was wrong.* C guarantees it and it is nearly word to word what is written in the Standard: *C99, 7.19.3p5) "If the main function returns to its original caller, or if the exit function is called, all open files are closed (hence all output streams are flushed) before program termination."* – ouah Jun 13 '14 at 10:00
0

We can think of the question at two level.

  1. What happens when the C/C++ ends (with a return code)
  2. What happens after the C/C++ ends (and how the OS deals with the resources and the return code).

For 1), C/C++ does not do any resource management and it will simply provide the return code back to the OS, regardless of 0 or a different value.

One exception I can think of are complex library resources (e.g streams)that can get destroyed as going out of scope... and that can have resource cleanup behaviours associated to that (e.g. filebuf contained in ofstream will have the file closed in the destructor)

But the above will be a case by case basis.

For 2)... the simple answer it just depends on the OS. Typical mainstream OS will take care of resource cleanup after the program. Fringe OS (e.g. embedded systems) might not.... or even in the case of maintream OS, different resources will get treated differently or take more time to be recovered (found: OS; resources automatically clean up (for Windows) and Answer to When is a C++ terminate handler the Right Thing(TM)?)

Finally what the OS does with the return value, again depends on the OS, I doubt the resource handling will change with different return values... but who knows for a particular case.

Community
  • 1
  • 1
jsantander
  • 4,972
  • 16
  • 27
  • Thankyou sir. So you mean, all the files, if left unclosed by the program, are closed by the OS? And a linked list is also destroyed even if a destructor isn't used? – Aditya Singh Jun 12 '14 at 14:23
  • @user3182942 No, I mean that there are C++ libraries that have builtin mechanisms to release resources on destruction.... But the destructor might not be called... Take that as a first line of defense within C++ libraries. If that fails it falls to the OS to release (or not release) the resources. – jsantander Jun 12 '14 at 14:25
  • but os never releases any resource. Not atleast OS X maverixs, windows 8, or 14.04LTS ubuntu. I experimented with it a lot, sir. I did it once again today. It did not close the file when I did not use the fclose() function in my C program. It doesnt even close a database connection if not closed explicitly. And my teachers even say that OS's behave differently if a nonzero is returned. But I never saw anything like that on the internet. This thing confuses me. So exactly what kind of reaources are released? Please enlighten me. – Aditya Singh Jun 12 '14 at 14:31
  • and is it also possible that a destructor is never called?? Never heard that before. – Aditya Singh Jun 12 '14 at 14:33
  • 1
    @user3182942 yes it is called memory leak... :D (example you do new but never delete) – jsantander Jun 12 '14 at 14:34
  • thankyou.so under what scenarios does that happen? And exactly what kind of resources are released? – Aditya Singh Jun 12 '14 at 14:35
  • @user3182942 typically is a software bug. Think of it this way: Your program takes resources from the OS. Your code or the libraries you use might have code in place to try help you in handling those resources. That might work or might not, depending on how much you help or mistakes or bad usage. Once your program is done, there might be still resources allocated to the program, then it is up to the OS to recover those resources. That simply depends on the OS features. – jsantander Jun 12 '14 at 14:40
  • Thankyou. But when the things come under OS responsibilities, does the OS release the resource, or the resources keep floating in the memory like an asteroids in space? – Aditya Singh Jun 13 '14 at 05:18
0

Final conclusion from the above answers... the OS does what it is programmed to do. Returning any integer just means to the OS that the program has terminated, nothing else. OS doesnt care if it terminated successfully or unsuccessfully. When we return any integer from main(), the integer is nothing but garbage for the OS. No matter you return 0 or -556 or any integer. Everything is the same for the OS. OS doesnt clear any resources. Infact when it comes to C/C++, its the programmers responsibility to clear off resources. The OS does nothing. We return 0 just coz it is a standard convension.

Aditya Singh
  • 127
  • 8