pointer to function and dynamic allocation

This is a discussion on pointer to function and dynamic allocation within the C Programming forums, part of the General Programming Boards category; Hi everyone, I am using a pointer to function to return a pointer of integers after certain calculations. This pointer ...

  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
    22,166
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,764
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,764
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,166
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 08:39 AM
  2. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 04: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, 06: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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21