Thread: Why does the statement if(arr) free(arr); still executing?

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    38

    Why does the statement if(arr) free(arr); still executing?

    Hello everyone, I have a heap allocated array arr. The allocation in arr is freed by using a pointer ptr. Therefore, if(arr) statement should return a false value. But practically it returns a true value and go ahead to call the free(arr) to free up the allocated space again. Resultantly, the program crashed.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    int main()
    {
        int* arr = (int*)malloc(3 * sizeof(*arr));
        arr[0] = 12; arr[1] = 61; arr[2] = 40;
        int* ptr = arr;
        free(ptr);
        ptr = nullptr;
    
    
        if(arr)
            free(arr);
        arr = nullptr;
        return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,157
    You set ptr to be nullptr, but forgot to set arr to be nullptr, so it still pointed to memory that was deallocated.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2019
    Posts
    38
    Thank you very much for your answer. Appreciated. However, wondering part is, why is the if(arr) expression returning a true value!. It should not return a true value after executing free(ptr). Sorry to be such a critical.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,157
    free(ptr) only frees the memory. It does not change the value of ptr or arr. That's why you need to set them to be null pointers if you intend to check them later.

    By the way, I assumed that you were testing out some C constructs in C++, but you posted in the C programming forum. You should be aware that nullptr is a C++ construct that would be invalid in C unless you defined it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2019
    Posts
    38
    Alright.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "IF" statement is not executing
    By vknehra10 in forum C Programming
    Replies: 1
    Last Post: 10-05-2016, 11:21 PM
  2. Replies: 16
    Last Post: 12-28-2012, 04:07 PM
  3. Replies: 40
    Last Post: 09-01-2006, 12:09 AM
  4. executing a statement
    By devil@work in forum C++ Programming
    Replies: 3
    Last Post: 03-30-2003, 08:04 AM
  5. coredump problem when executing delete statement using Pro*C
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 05-13-2002, 10:48 PM

Tags for this Thread