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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:37:42+00:00 2026-06-01T13:37:42+00:00

So, I’m a C# guy trying my hand at learning C. As a first

  • 0

So, I’m a C# guy trying my hand at learning C. As a first (personal) project I am attempting to write a basic coordinate geometry library.

Question: Is it again best C programming practices to allocate memory on the heap behind the scenes instead of letting the programmer who is targeting the library do it?

For example, my ‘point’ struct & related methods:

point.h

/* A basic point type. */
typedef struct point
{
    float x;
    float y;
    float z;
    char *note;
}point;

/* Initializes a basic point type. [Free with free_point method] */
point *create_point(float pos_x, float pos_y, float pos_z, char *_note);
/* Frees a point type. */
void free_point(point *_point);
/* Finds the midpoint between two points. */
point *midpoint(point *pt1, point *pt2);

point.c

#include "point.h"

/* Initializes a basic point type. [Free with free_point method] */
point *create_point(float pos_x, float pos_y, float pos_z, char *_note)
{
    point *p;
    size_t notelen = strlen(_note);

    p = (point*)malloc(sizeof(point));
    p->x = pos_x;
    p->y = pos_y;
    p->z = pos_z;

    p->note = (char*)calloc(notelen + 1, sizeof(char));
    strcpy_s(p->note, notelen + 1, _note);

    return p;

}
/* Frees a point type. */
void free_point(point *_point)
{
    free (_point->note);
    free (_point);
}

/* Creates a midpoint between two points. */
point *midpoint(point *pt1, point *pt2)
{
    float mid_x = (pt1->x + pt2->x) * 0.5f;
    float mid_y = (pt1->y + pt2->y) * 0.5f;
    float mid_z = (pt1->z + pt2->z) * 0.5f;

    point *p = create_point(mid_x, mid_y, mid_z, "Midpoint");
    return p;
}

Notice that I creating the struct ‘point’ on the heap FOR whoever implements/uses my lib (in all honesty, this project is just for me and for learning, nevertheless…) via the create_point() method. Is this poor practice? It feels like I am forcing the user into programming a certain way. Same goes with the midpoint() method. Again, you must use pointers to a ‘point’ struct.

I was unable to find exact questions regarding C library design on SO, but please point me in the right direction if applicable.

Thanks.

  • 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-01T13:37:44+00:00Added an answer on June 1, 2026 at 1:37 pm

    It’s preference, really. I usually subscribe to letting the user allocate the memory for the object however they wish, and then initializing the members for them.

    /* here a non-zero return value might indicate if for example
     * we failed to allocate memory for note */
    int point_init(struct point* p, int x, int y, char* note)
    {
      /* ... */
    }
    
    /* usage: */
    struct point p;
    if (point_init(&p, 1, 2, "hello")) {
      /* error */
    }
    

    This gives the user the option to do things like allocate an array of points that are adjacent in memory, instead of keeping track of an array of pointers to points:

    struct point mypoints[NUM_POINTS];
    
    for(size_t i = 0; i < NUM_POINTS; ++i) {
      point_init(&mypoints[i], ...);
    }
    

    EDIT:

    If you want to use the Opaque Pointer strategy to hide the members of your structure from the library user, you will need to allocate the memory for them using the method you described. That’s certainly a huge benefit for allocating the structure in your library.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post

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.