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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:53:20+00:00 2026-05-25T21:53:20+00:00

void getFree(void *ptr) { if(ptr != NULL) { free(ptr); ptr = NULL; } return;

  • 0
void getFree(void *ptr)
{
    if(ptr != NULL)
    {
        free(ptr);
        ptr = NULL;
    }
    return;
}
int main()
{
char *a;
a=malloc(10);
getFree(a);
if(a==NULL)
    printf("it is null");
else
    printf("not null");
}

Why is the output of this program not NULL?

  • 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-25T21:53:21+00:00Added an answer on May 25, 2026 at 9:53 pm

    Because the pointer is copied by value to your function. You are assigning NULL to the local copy of the variable (ptr). This does not assign it to the original copy.

    The memory will still be freed, so you can no longer safely access it, but your original pointer will not be NULL.

    This the same as if you were passing an int to a function instead. You wouldn’t expect the original int to be edited by that function, unless you were passing a pointer to it.

    void setInt(int someValue) {
        someValue = 5;
    }
    
    int main() {
        int someOtherValue = 7;
        setInt(someOtherValue);
        printf("%i\n", someOtherValue); // You'd expect this to print 7, not 5...
        return 0;
    }
    

    If you want to null the original pointer, you’ll have to pass a pointer-to-pointer:

    void getFree(void** ptr) {
        /* Note we are dereferencing the outer pointer,
        so we're directly editing the original pointer */
    
        if (*ptr != NULL) {
            /* The C standard guarantees that free() safely handles NULL,
               but I'm leaving the NULL check to make the example more clear.
               Remove the "if" check above, in your own code */
            free(*ptr);
            *ptr = NULL;
        }
    
        return;
    }
    
    int main() {
        char *a;
        a = malloc(10);
    
        getFree(&a); /* Pass a pointer-to-pointer */
    
        if (a == NULL) {
            printf("it is null");
        } else {
            printf("not null");
        }
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

void main(){ int i,k; char* p; int j; printf(address of i is %d \naddress
void* GetData() { return reinterpret_cast<unsigned char*>(this); } Is there a case of automatic type
void foo(char *p) { int i; int len = strlen(p); p = malloc(sizeof(char)*len+2); p[0]
void (int a[]) { a[5] = 3; // this is wrong? } Can I
void foo(void **Pointer); int main () { int *IntPtr; foo(&((void*)IntPtr)); } Why do I
void turtle (int gtot) { int msg; fcntl(gtot,F_SETFL,O_NONBLOCK); read(gtot,&msg,4); gotoxy(12, 21); printf(The value of
void main() { const int * a; *a = 5; } gcc error :
void max_min(sqlite3 *db) { //call back********* int i, ncols; sqlite3_stmt *stmt; char *sql; const
void reverse(char *str){ int i,j; char temp; for(i=0,j=strlen(str)-1; i<j; i++, j--){ temp = *(str
void distinct (void) { char sent; int n1, n2, n3, n4, n5, n6, n7;

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.