0

A while ago I asked myself, when I write something like this:

   char* first(int howMany){
      return (char*)malloc(howMany);
   } 

   int main(){
      char*t;
      int one=20;
      t=first(20);  
   }

this code crashes at codepad.org but in Mepis Linux 11.04 it just provokes some warnings.

But if I write something like this for C++:

char* first(int howMany){
    return new char [howMany];
   }

int main(){
    char*t;
    int one=20;
    t=first(20);  
}

this code works fine.

My question is:

why doesn't the C-code work and what happens, when I try to

return (char*)malloc(howMany)?

P.S: and what happens if i trying to use in Objective-C this function?

(NSArray*) first(){
    return [NSArray array];
}
4

2 に答える 2

8

I doesn't crash in codepad.org, it runs, but the exit status of the program indicates an error. The exit status is undefined and just what happens to be on the stack by chance, and the exit value (it happens to be 120 on codepad.org) comes from your program not returning anything properly from main(), like it should do.

Traditionally in both C and C++, you had to explicitly return something from main(). In later C and C++ standards, this was changed, so that if you exited main() without a return statement, it would automatically return 0. (0 means success.)

What seems to have happened is that you have a C++ compiler which respects this standard, (standard C++ has had this rule for a longer time than standard C), that if main() is not returning anything, it returns 0 by default.

In contrast, the C compiler on codepad.org seems to follow an older standard, where main() must have an explicit return statement for the program to be valid. So, one could argue that your program fails because codepad.org does not have an up to date C compiler, or one could argue that your program should be more conservative and not assume a modern compiler, and instead return explicitly from main().

That is why the compiler on Mepis Linux warns you. It is probably not configured to follow the latest standard and thus warns you that you should end your main() function with a return statement.

So if you want to write conservative code which works everywhere, your main() function should always end with a return statement. Your very question is proof that it may be dangerous to rely on features of the newer standards if you want your code to work in the "real world".

Changing to Objective C may change something else, but the main problem (pun intended) is still that you are not explicitly returning a value from main() and what standard the C part of Objective C follows in a particular compiler.

That you are not freeing the memory, could well cause a memory leak, but is not an error in the program itself, and is not affecting the issues you observed. (Indeed, on Linux, exiting the program automatically frees all memory, which is perhaps sloppy programming, but not incorrect.)

于 2012-06-14T09:23:51.520 に答える
3

One important difference between C and C++ in this case is that in C++, the main function returns 0 if there is no explicit return. This is the case in your code sample, so it is seen as exiting with success in C++, regardless of any code errors.

于 2012-06-14T09:41:04.477 に答える