Thread: Is it necessary to free an allocated variable inside a local function?.

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

    Is it necessary to free an allocated variable inside a local function?.

    Do I need to free variable 'var' inside of local function func()?.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    void func()
    {
            int* var = (int*)malloc(sizeof(*var));
    }
    
    
    int main()
    {
        func();
        return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Generally, you should free what you malloc. It may not be necessary in a modern OS if the process is going to be terminated very soon, but if you're not certain that that will be the case, then there's no harm just freeing anyway.

    As for your example: unfortunately it is so pointless that you don't need malloc either, so it's not very useful to comment on whether you need to free anything. Arguably, you don't need the program in the first place
    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
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Do I need to free variable 'var' inside of local function func()?.
    You either need to free it in func, or return the pointer to the caller so they can free it.

    You need to call free at some point, your only choice is where and when.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    int *iota(size_t size)
    {
        int *v = malloc(size * sizeof(*v));
        if (v)
            for (size_t i = 0; i < size; ++i) v[i] = i;
        return v;
    }
     
    void print(const int *a, size_t size) {
        for (size_t i = 0; i < size; ++i) printf("%d ", a[i]);
        putchar('\n');
    }
     
    int main()
    {
        const size_t size = 20;
        int *a = iota(size);
        if (a)
        {
             print(a, size);
             free(a);
        }
        return 0;
    }
    Last edited by john.c; 12-16-2020 at 07:47 AM.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  5. #5
    Registered User
    Join Date
    Oct 2019
    Posts
    43
    Thanks Amanda for your comments.

  6. #6
    Registered User
    Join Date
    Oct 2019
    Posts
    43
    Thanks.

  7. #7
    Registered User
    Join Date
    Oct 2019
    Posts
    43
    Thanks Salem and John too for comments.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 10-21-2019, 03:18 PM
  2. Replies: 4
    Last Post: 01-14-2019, 01:36 PM
  3. Local variable inside a loop
    By krkr in forum C Programming
    Replies: 17
    Last Post: 11-28-2014, 11:39 AM
  4. Replies: 3
    Last Post: 01-15-2011, 01:47 PM
  5. Replies: 20
    Last Post: 11-12-2005, 03:10 PM

Tags for this Thread