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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:40:46+00:00 2026-05-15T23:40:46+00:00

When passing a char* as an argument to a function, should the called function

  • 0

When passing a char* as an argument to a function, should the called function do a free on that string? Otherwise the data would be “lost” right and the program would leak data. Or are char* handled in a special way by the compiler to avoid everyone from having to do free all the time and automatically deletes it one it goes out of scope? I pass “the string” to the function so not an instance to an already existing char*. Or should one use char[] instead? Just feels so dumb to set a fixed limit to the argument input.

  • 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-15T23:40:47+00:00Added an answer on May 15, 2026 at 11:40 pm

    Keep this simple principle in mind: “always free memory at the same level that you allocated it”. In other words a function should never try to free memory that it itself has not allocated. A short example to clarify this:

    #include "graphics.h"
    
    // The graphics API will get a Canvas object for us. This may be newly allocated
    // or one from a pool of pre-allocated objects. 
    Canvas* canvas = graphics_get_canvas (); 
    
    // If draw_image () frees canvas, that violates the above principle.
    // The behavior of the program will be unspecified. So, just draw the image
    // and return.
    draw_image (canvas); 
    
    // This is also a violation.
    // free (canvas) ; 
    
    // The right thing to do is to give back the Canvas object to the graphics API
    // so that it is freed at the same 'level' where it was allocated. 
    graphics_return_canvas (canvas);
    

    Note that the function is not named graphics_free_canvas () or something like that, because the API may choose to free it or reuse it by returning it to a pool. The point is, it is an extremely bad programming practice to assume ownership of a resource that we did not create, unless we are specifically told otherwise.

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

Sidebar

Related Questions

I am having a function: int getparam(char *gotstring) and i am passing a string
passing post data using cURL requires that the name of the input. However, I
Keeping track of how many times a function is called is easy when passing
I'm calling C# method from C++ and passing char** as argument. It has to
I'm having trouble passing a C++0x lambda function as the second argument to makecontext
I'm passing in an argument to a C program: program_name 1234 int main (int
I have a function that will pass a string and manipulate. in the function
I want a simple function that receives a string and returns an array of
ueach is a function that loops through a Unicode string, and running the callback
Passing User defined argument to RPM is possible while installing?. for example: ~>rpm -i

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.