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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:14:34+00:00 2026-05-27T17:14:34+00:00

I would like to actually use this wrapper, but the problem is i don’t

  • 0

I would like to actually use this wrapper, but the problem is i don’t know if it’s very safe, yet.

I have a few simple questions regarding using malloc(), calloc(), and realloc(). Here’s what I have so far:

string.h

typedef struct str str; // pointer for encapsulation

string.c

struct str
{
    char *buf;
    size_t len;
}

Say i have a helper function that simply does this:

    str *NEW_STRING()
    {
        str *temp = calloc(1, sizeof (struct str));
        temp->len = 0;
        temp->buf = (char *) malloc(1);
        return temp;
    }

Is this safe? If it is, what would happen if i did something like this:

str *A_STRING = NEW_STRING();
A_STRING = NEW_STRING();

It would call malloc and calloc twice, is that bad? Would an initializer be better?

void str_init(str *A_STRING)
{
    if (A_STRING)
    {
        free(A_STRING);
    }

    if (A_STRING->buf)
    {
        free(A_STRING->buf);
    }

    A_STRING = calloc(1, sizeof (struct str));
    A_STRING->buf = (char *) malloc(1);
    A_STRING->len = 0;
}

Finally, is this a good way to free memory?

    void free_string(str *A_STRING)
    {
        if (A_STRING->buf)
        {
            free(A_STRING->buf);
        }
        else
        {
            A_STRING->buf = NULL;
        }

        if (A_STRING)
        {
            free(A_STRING);
        }
        else
        {
            A_STRING = NULL;
        }

        A_STRING->len = 0;
    }

Any additional information would be great if included. I don’t want to release anything to the public as if it were a good library, because i am primarily doing this for learning purposes.

  • 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-27T17:14:35+00:00Added an answer on May 27, 2026 at 5:14 pm

    Lots of errors:

    is this safe? if it is, what would happen if i did something like this:

    No.

    str *NEW_STRING()
    {
        str *temp = calloc(1, sizeof (struct str));
    
        // If calloc fails and returns NULL all the code below is invalid and blows the code up.
    

    Next:

    it would call malloc and calloc twice, is that bad? would an initializer be better?

    You leak memory.
    The second call basically generates a new object the old object is lost and leaks.

    Problems in str_init

    void str_init(str *A_STRING)
    {
    

    Is this the first time that his method is called?
    If so then A_STRING contains a random value (that you are about to FREE).
    This will blow the code up.

        if (A_STRING)
        {
            free(A_STRING);
        }
    

    A_STRING is freed (you can now no longer accesses it).
    Any code that does so is bad.

        if (A_STRING->buf)   // Bang blow up code.
        {
            free(A_STRING->buf);
        }
    
        A_STRING = calloc(1, sizeof (struct str));
    

    No checking the result of calloc.

        A_STRING->buf = (char *) malloc(1);
        A_STRING->len = 0;
    }
    

    is this a good way to free memory?

    void free_string(str *A_STRING)
    {
        if (A_STRING->buf)
        {
            free(A_STRING->buf);
        }
        else
        {
            A_STRING->buf = NULL;  // Its already NULL pointless work
        }
    
        if (A_STRING)
        {
            free(A_STRING);
        }
        else
        {
            A_STRING = NULL;  // ITs already NULL pointless work
        }
    
        // BANG you just blew up the space shuttle.
        A_STRING->len = 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to know whether this is actually possible in Android? When Android
Would like to know the c# code to actually retrieve the IP type: Static
I would like to place my div below the list, but actually it is
Would like to know what a programmer should know to become a good at
We all know you can't do things like this: int a = 7; new
I have an object, o, and a type, T. I'd like to use reflection
Tried to find answer here but, no luck so, here goes; I would like
First of all, I have to use and create this DOM element in a
I have raised this question on the WP Answers as well. But since this
Would like to get a list of advantages and disadvantages of using Stored Procedures.

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.