Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8101193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:55:29+00:00 2026-06-05T22:55:29+00:00

A while ago I asked myself, when I write something like this: char* first(int

  • 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];
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T22:55:30+00:00Added an answer on June 5, 2026 at 10:55 pm

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While I realize that this question has been asked once or twice ago but
This question was asked quite some time ago, and while it covers possible solutions
UPDATE 10/19/2010 I know I asked this question a while ago, but the workarounds
Okay so I know I asked a similar question a while ago, but this
This question is related to this initial question asked a little while ago. Now,
A while ago I asked this question about how to defer updates to an
This was asked quite a while ago in SO but there isn't a definitive
A while ago I had asked how to make a collapsible comment box like
I asked this question a while ago, and the answer provided was satisfactory to
This is similar to a question I asked a while ago, however this didn't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.