#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    132
    Rep Power
    2

    Question about passing pointer to function


    i'm reading about memory allocation, and i saw an example of a wrong way and a right way to allocate memory from functions.

    i have a question about it:

    why is this wrong:
    first way:
    Code:
    void func(int *p)
    {
    	p = (int *) malloc(3*sizeof(int));
    }
    
    void main()
    {
    	int *pm = NULL;
    	func(pm);
    }

    and this is right:
    second way:
    Code:
    void func(int **p)
    {
    	*p = (int *) malloc(3*sizeof(int));
    }
    
    void main()
    {
    	int *pm = NULL;
    	func(&pm);
    }
    why isn't the allocated memory using the first way accessible?
    doesn't p hold pm's address?

    thanks in advanced!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Posts
    152
    Rep Power
    5
    When you declare local variables in a function, there are two actions performed by the compiler. One is declaring the variable of that type, second one is initializing the value for the variable.

    In your example, the first program can't send any values to the function ie (pm) has no value. However, in the second case the address of the pointer is passed to the function and double declared here is initialized.
  4. #3
  5. Come play with me!
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,759
    Rep Power
    9397
    But in the first example pm does have a value. NULL is a value, not the absence of one.

    In the first example pm is a number (currently 0). It represents a location in memory (currently an invalid location) but it is still a number. And the normal rules when calling a function with parameters still apply: each value is a copy of the original. pm in main() will be copied to p in func. Modifying p is useless because you're modifying the copy.

    The second example has func() receiving a copy of &pm, but that's fine because that copy has the same value and it's the value we actually care about: a location in memory. The function can then go off to that location and do whatever it wants (such as treat it as a pointer and modify the value being pointed to). There you're not actually modifying the copy itself.

    ASCII art.
    Code:
    First:
    
           +-----------+
    main   | pm = NULL |
           +-----------+
    ------------ V --------
           +-----------+
    func   | p  = NULL |
           +-----------+
    
    
    Second:
    
           +-----+    +-----------+
    main   | &pm | -> | pm = NULL |
           +-----+    +------------
    --------- V ------- ^ ------------
           +-----+      |
    func   |  p  | -----+ *p
           +-----+
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    132
    Rep Power
    2
    great explanation.
    thanks!
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    132
    Rep Power
    2
    and just want to make sure i got it right:



    is that pretty much the different?

IMN logo majestic logo threadwatch logo seochat tools logo