Thread: pointer to function and dynamic allocation

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    114

    pointer to function and dynamic allocation

    Hi everyone,

    I am using a pointer to function to return a pointer of integers after certain calculations.
    This pointer of integers is a 1D pointer (array), whose memory is allocated INSIDE the function that returns it.

    Now, since I want to return this array to the main.c by a call of the type:

    MYARRAY = function();

    am I supposed to free the allocated memory inside the function or in main, even if in main I am not allocating MYARRAY?

    I report a sample of my main.c and a sample of my function.c below.
    Thank you in advance.

    Best,
    cfd

    main.c
    Code:
    #include ...
    
    int main(int argc, char *argv[])
    {
        int *MYARRAY;
    
        MYARRAY = function();
    
    }
    function.c
    Code:
    int *function(void)
    {
        int n;
        int *ARRAY;
    
        //Allocation
       n = 100;
       ARRAY = calloc(n,sizeof(int *)); 
       
       //Fill ARRAY[]
       for(i=0; i<n; i++)
         ARRAY[i] = i;
    
    
        //Freeing memory
        free(ARRAY);
    
    
    return ARRAY;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cfdprogrammer
    am I supposed to free the allocated memory inside the function or in main, even if in main I am not allocating MYARRAY?
    If you free it in the function, then you would be returning a pointer to freed memory, which is not very useful. Basically, it looks like you want to use the dynamically allocated array and finally free the memory in main.
    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,659
    Well freeing and returning the same is wrong.

    So calling free in main() seems to be right in this instance.
    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
    Mar 2009
    Posts
    114
    Thanks to both for replying.
    In this sense, although I am allocating inside "function", by freeing in main.c instead, do you agree that I am not doing any "trick" to the standard C?

    thank you again
    cfd

  5. #5
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Wouldn't the allocated memory be freed automatically when the function returns?
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Wouldn't the allocated memory be freed automatically when the function returns?
    If it was, malloc would never work!
    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.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cfdprogrammer
    In this sense, although I am allocating inside "function", by freeing in main.c instead, do you agree that I am not doing any "trick" to the standard C?
    As in using calloc in one function and free in another is normal? Yes it is.
    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

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    114
    Thanks for the help;

    Best,
    cfd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. dynamic function execution
    By Sargnagel in forum C Programming
    Replies: 7
    Last Post: 05-07-2003, 05:28 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM

Tags for this Thread