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

  • SEARCH
  • Home
  • 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 8392619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:33:36+00:00 2026-06-09T19:33:36+00:00

Possible Duplicate: pointer as second argument instead of returning pointer? I see this a

  • 0

Possible Duplicate:
pointer as second argument instead of returning pointer?

I see this a lot:

Monkey* test = malloc(sizeof(Monkey));
Monkey_New(test);

Wouldn’t it be cleaner to have the init function return the pointer?

Monkey* test = Monkey_New();

Why is it frequently done in the first way?

  • 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-09T19:33:38+00:00Added an answer on June 9, 2026 at 7:33 pm

    The two designs are functionally extremely different. The one you’re asking about puts the burden of allocation on the caller. This has a huge advantage that dynamic storage (malloc) can be avoided if the object does not have to out-live the caller; it can be stored in an automatic variable. This also means that, assuming the init function does not have to obtain further resources, you can have a failure-free API where the caller does not have to check for failure. On the other hand, it requires that the definition (at least the size) of the structure be exposed to the calling module, which means the caller must be recompiled if the library code is updated to use an object of a larger size; thus it can be negative for shared libraries intended to have a long-term stable ABI.

    In the alternative design you pointed out, the burden of obtaining storage is on the callee (library function), and it must use dynamic storage (malloc) since automatic storage would not survive the return to the caller. This is more abstract and cleaner,

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

Sidebar

Related Questions

Possible Duplicate: How to find the sizeof(a pointer pointing to an array) Sizeof array
Possible Duplicate: Why ‘this’ is a pointer and not a reference? Is there any
Possible Duplicate: When should I make explicit use of the this pointer? I'm wondering
Possible Duplicate: How to find the sizeof(a pointer pointing to an array) Sizeof an
Possible Duplicate: Difference between pointer variable and reference variable in C++ I saw this
Possible Duplicate: Test for void pointer in C++ before deleting Is code snippet 1
Possible Duplicate: How to find the sizeof( a pointer pointing to an array )
Possible Duplicate: what does malloc(0) return ? Does it return zero pointer? Is the
Possible Duplicate: Different answers from strlen and sizeof for Pointer & Array based init
Possible Duplicate: Modifying value of char pointer in c produces segfault This is a

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.