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

  • Home
  • SEARCH
  • 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

Ask A Question

Stats

  • Questions 524k
  • Answers 524k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Found it. JSCEP has a class called "PKIMessage" that does… May 16, 2026 at 9:56 pm
  • Editorial Team
    Editorial Team added an answer Not unless you checked their IP, but then again that's… May 16, 2026 at 9:56 pm
  • Editorial Team
    Editorial Team added an answer There is several things at play in your example. Here… May 16, 2026 at 9:56 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I am trying to write a function to get a string from uart. Its
I'm writing a function in C that takes a variable number of arguments. size_t
I've coded a program in C that sends messages to the stdout using printf
I am using Linux. I am trying to write a program in c that
When compiling a program containing this particular function, /* * Function read_integer * *
I'm writing a program for a school project that is supposed to emulate the
What I understand that hexadecimal numbers can be placed into string using \x .
I'm trying to create a function: template <typename T> void doIt( T*& p )
I made a function similar to numpy's array . It converts lists to arrays,
I have a large C structure (about 40 members, int and char[]) which 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.