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 511143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:11:11+00:00 2026-05-13T07:11:11+00:00

In a response elsewhere, I found the following snippet: In general it is nicer

  • 0

In a response elsewhere, I found the following snippet:

In general it is nicer in C to have
the caller allocate memory, not the
callee – hence why strcpy is a “nicer”
function, in my opinion, than strdup.

I can see how this is a valid pattern, but why might it be considered nicer? Are there advantages to following this pattern? Or not?

example

Recently I’ve written a fair amount of code that looks something like:

struct foo *a = foo_create();
// do something with a
foo_destroy(a);

If foo is a anything more than a flat structure, then I figured I could put all my initialization in one step. Also, assume the struct should be on the heap. Why might it be better form to do something like:

struct foo *a = malloc(sizeof(foo));
foo_init(a);
// do something with a
foo_destroy(a)
  • 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-05-13T07:11:11+00:00Added an answer on May 13, 2026 at 7:11 am

    Whenever you want an opaque structure and don’t want to expose its internals in the header file. Your foo_create() example illustrates this.

    Another example is the Windows API. E.g. CreateWindow gives you a HWND. You have no idea what the actual WND structure looks like and can’t touch its fields.

    Same with kernel object handles. E.g. CreateEvent gives a HANDLE. You can only manipulate it with the well-defined API, and close it with CloseHandle().

    Re:

    struct foo *a = malloc(sizeof(foo));
    

    This requires you to define struct foo in a header, and hence expose its internals. If you want to change it down the track, you risk breaking existing code that (incorrectly) relied on its members directly.

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

Sidebar

Related Questions

This may have been answered elsewhere but I could not find a suitable response.
I have a json response like: { total: 2, success: true, rows: [ {a1_id:7847TK10,
I'm not really sure why my code is throwing a EXC_BAD_ACCESS, I have followed
I have the following code which popualtes a ListView with photos from Flickr private
In response to the helpful comments, I have edited the original question (where I
(Following up on this question: Getting raw XML response from Java web service client
Not sure if this question has been asked elsewhere... For a website project that
I have some code which waits for a server response & uses a lambda
This code snippet isn't working, I'm getting an Authentication Failed. response from the server.
I've found some similar questions on SO and elsewhere, but nothing that quite does

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.