Thread: Dynamic memory allocation by a function within another function

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    2

    Dynamic memory allocation by a function within another function

    Hi All,

    I have been using C for a while, still haven't got around with pointers

    I am defining a pointer (to a pointer) **v; in main.
    Then **v is passed to a function Initialise() to assign values.
    Initialise() calls dmatrix() to allocates memory to v.
    Finally, the function finalise(v) frees the memory.

    PROBLEM: Even though v is passed as a reference (via pointer), its values are not accessible to the main function.The program fails when tries to free the memory.

    I have multiple variables that need to be initialised in this fashion.
    Can anyone offer any clues or workarounds.
    Thanks very much in advance for your help.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    void Initialise(double **v, int row, int column, int value);
    void finalise(double **v);
    double **dmatrix(int row, int column);
    void freedmatrix(double **a);
    
    main(){
             double **v;
             Initialise(v,10,10,3);
             printf("v from main %f\n",v[1][1]);
             finalise(v);
            }
    
    void Initialise(double **v, int row, int column, int value){
            int i,j;
            v=dmatrix(row+1,column+1);
    
            for (i=1;i<=row;i++){
            for (j=1;j<=column;j++){
                    v[i][j]=value;}}
             printf("v from function Initialise %f\n",v[1][1]);
    }
    
    void finalise(double **v){
      freedmatrix(v);}
    
    double **dmatrix(int row, int column){
            int i;
            double **a;
            a=(double **) malloc((row)*sizeof(double *));
            a[0]= (double *) malloc((row)*(column)*sizeof(double));
            
            for(i=1;i<=(row-1);i++){
                    a[i]=a[i-1]+column;
            }
            return(a);
    }
    
    void freedmatrix(double **a){
      free(a[0]);
      free(a);
    }

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    But you're not passing a double ** var by reference, you're passing it by value. Passing it as you do means that any changed to v never get propagated back to the calling function since the callee is just modifying a copy of v. Just like an int - the rules don't change because your variable is a pointer.

    If you want to pass a double ** by reference, the parameter type needs to be double ***, it has to be called as Initialize(&v,...), and modified in the called function using *v = dmatrix(...) and so on.

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    2
    Thanks very much- that makes sense from the results. (I always thought if we declare double **v, then v=&v[0][0]).

    Anyway, I resolved the issue (for now) by declaring v as a global variable.

  4. #4
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by saj View Post
    Anyway, I resolved the issue (for now) by declaring v as a global variable.
    That's not resolving an issue, that's creating future issues.
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by KCfromNC View Post
    But you're not passing a double ** var by reference, you're passing it by value. Passing it as you do means that any changed to v never get propagated back to the calling function since the callee is just modifying a copy of v. Just like an int - the rules don't change because your variable is a pointer.

    If you want to pass a double ** by reference, the parameter type needs to be double ***, it has to be called as Initialize(&v,...), and modified in the called function using *v = dmatrix(...) and so on.
    Just a bit of nit picking really, what you are saying makes sense, but note that there is no passing by reference in C. Everything is passed by value. The fact that a pointer is the address of something else is a completely different subject altogether; the address pointed to by the pointer (i.e. its value) is what gets passed to the function. As far as the compiler is concerned that address is a value just like 42 is.

    C++ on the other hand does support passing by reference, using, well... references. They follow sensibly different rules than pointers though.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic memory allocation for structs passed to function
    By eXcellion in forum C Programming
    Replies: 18
    Last Post: 03-30-2011, 03:30 PM
  2. Dynamic allocation inside function
    By slippy in forum C Programming
    Replies: 4
    Last Post: 10-06-2009, 09:50 PM
  3. pointer to function and dynamic allocation
    By cfdprogrammer in forum C Programming
    Replies: 7
    Last Post: 07-08-2009, 10:36 AM
  4. Dynamic Array Allocation function
    By P4R4N01D in forum C++ Programming
    Replies: 6
    Last Post: 05-15-2009, 02:04 AM
  5. Increasing memory allocation in function
    By Ramses800 in forum C Programming
    Replies: 3
    Last Post: 12-16-2008, 05:30 AM

Tags for this Thread