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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:57:46+00:00 2026-06-16T05:57:46+00:00

It is known that if we pass a pointer by value to a function,

  • 0

It is known that if we pass a pointer by value to a function, it cannot be freed inside the function, like so:

void func(int *p)
{
    free(p);
    p = NULL;
}

p holds a copy of a (presumably valid) address, so free(p) tries to, well, free it. But since it is a copy, it cannot really free it. How does the call to free() know that it cannot really free it ?

The code above does not produce an error. Does that mean free() just fails silently, “somehow” knowing that address passed in as argument cannot be worked upon ?

  • 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-16T05:57:47+00:00Added an answer on June 16, 2026 at 5:57 am

    p holds a copy of a (presumably valid) address, so free(p) tries to, well, free it. But since it is a copy, it cannot really free it.

    It’s not true. free() can work just fine if p is a valid address returned by malloc() (or NULL).

    In fact, this is a common pattern for implementing custom “destructor” functions (when writing OO-style code in C).

    What you probably mean is that p won’t change to NULL after this – but that’s natural, since you’re passing it by value. If you want to free() and null out the pointer, then pass it by pointer (“byref”):

    void func(int **p)
    {
        if (p != NULL) {
            free(*p);
            *p = NULL;
        }
    }
    

    and use this like

    int *p = someConstructor();
    func(&p);
    // here 'p' will actually be NULL
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function that I pass an array into and an int into
I have a function that takes pointer to pointer to struct. struct ABC; void
I want to pass an ImageIcon instance by value as we know that by
It is known that the halting problem cannot have a definite solution, one that
After a while, I was finally able to pass a pointer to a function
I have a pure C++ class that needs to retrieve an int value from
I know that I can pass a string as the second parameter to the
I know that it's a common convention to pass the length of dynamically allocated
I know from here that I can pass an ignore option in the jQuery.validate
I known that with subversion you can attach arbitrary properties to each files. In

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.