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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:41:07+00:00 2026-05-26T16:41:07+00:00

Defined in h file: char *copiedText; Defined in c file: char *copyText(char *text){ if

  • 0

Defined in h file:

char *copiedText;

Defined in c file:

char *copyText(char *text){
  if (copiedText != 0) free(copiedText);
  copiedText = (char *)calloc((strlen(text) + 1) * sizeof(char), sizeof(char));
  strcpy(copiedText, text);
  return copiedText;
}

First off, this isn’t about how to copy text, I just chose that as an example. My question is about the free() before calloc().

First question I hear you ask is why not just free() at the appropriate time – i.e. when copiedText is no longer needed?

Long story short, I’m making part of a program and I can not trust the users of my function to properly free() copiedText, so I want to contain as much code inside my function as possible. All they do is include h and c file and call the function copyText.

I am aware there will be a (minor) memory leak between last call to copyText and program termination, but this is a tradeoff that I am willing to accept, because I only malloc() small amounts of data.

The question is simply, will the free() actually free up the memory allocated by my calloc() when coded like this?

  • 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-26T16:41:07+00:00Added an answer on May 26, 2026 at 4:41 pm

    The flip side is that now:

    1. the pointer returned by copyText() is only valid until the next call to copyText() — this is not at all obvious to the caller and is probably worth documenting as prominently as you can;
    2. the function is not thread-safe.

    A better approach might be to require an explicit free() (perhaps wrapping it in a function called freeText(), as a counterpart to copyText()).

    Another alternative is for the caller to provide their own buffer along with its size (so that you can avoid buffer overruns).

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

Sidebar

Related Questions

I have a web page with swf file defined this way <object width=600 height=400
I have a template class defined in a header file like this. Here I
I have got a map defined inside a class in header file like this:
My professor defined this in the .h file void list_map(INTLIST* list, void (*f)(void *));
I have a file structure like this: file1.h extern const char *build_info[][3]; file1.cpp #include
I've got this here class defined in a header file: class E_IndexList { public:
I have unsigned char[] that contains binary text. I need to convert this binary
I'd like to define a constant char* in my header file for my .cpp
I have an aidl file defined as follows: package com.erbedo.callalert; interface RemoteCallAlert { void
I am using the following to search for a file defined as a macro

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.