Pointers and parameter passing modes

I'm trying to become more familiar with pointers and parameter passing modes. I have this example, but can't reach the right solution even with the memory diagrams...
Can somebody explain me how this program works step by step?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void mix(int *one, int &two, int * &three) {
    *one = two;
    one = three;
    *one = 2;
    three = new int(4);
    two = 6;
}

void testIt() { 
    int first = 1;
    int *second = new int(3);
    int *third = new int(5);
    int **fourth = &second;
    mix(&first, *third, *fourth);
    cout << first << " " << *second << " " << *third << " " << **fourth << endl;
}
I could explain this to you line by line, but I'd rather see you try and work through it.
Why don't you post what you think each step does and we'll help where you have problems. Start with lines 11-13, then work your way through the call to the mix function.

10. int first = 1; - initializes first at call stack to be 1, in first is 1

11. int *second = new int(3); - dynamically allocating heap memory for 3 integers, second is address of the first int, in *second is ? (not initialized yet)

12. int *third = new int(5); - dynamically allocating heap memory for 5 integers, third is address of the first int, *third is ? (not initialized yet)

13. int **fourth = &second; - fourth is a pointer to pointer to second, **fourth points at *second so in **fourth is the same as *second ( ? ), *fourth is address of the first int (as in line 11)

14. calls function mix with parameter passing as below
- &first -> *one – the same as *one = &first – so *one now points to first, so *one is 1
- *third -> &two
- *fourth -> * &three

The last few I’m not sure at all, but I’m not sure about the others though…

Thank you for any help…
Last edited on
10. Correct
11. Incorrect. Allocates a single integer giving it the value 3. There is a difference between new int[3] (allocate array) and new int(3) (allocate single and initialize).
12 Incorrect. Allocates a single integer giving it the value 5.
13. I'd state it a little differently. forth is a pointer to a pointer. It is initialized with the address of second. So yes, **forth points indirectly to what *second points to.
14a, Correct
14b. third points to the int containing 5. That int is passed by reference.
14c. forth is dereferenced once, which is the pointer second. The third arg is an int pointer passed by reference, so we are in effect passing second by reference.

Now to work through mix.
2) What one points at (first) is replaced by the contents of the second arg (5).
3) The pointer one is replaced by what was passed in the third parameter (second).
4) What one (second) points to is replaced by 2.
5) A new int containg 4 is allocated. Since the third argument was passed by reference, we're changing the location passed in the third arg which was effectively second, so now (*second and **forth) both point to the value 4.
6) The second argument (*third) is now set to 6.

If we got this right the values printed should be:
first = 5, *second = 4, *third = 6, **forth = 4

And points off to the instructor for creating a memory leak. The memory allocated at line 11 was lost.
Thank you, it helped a lot!
Last edited on
Topic archived. No new replies allowed.