Lost in pointer translation

I am somewhat confused by why this code doesn't maintain the previously assigned value. I assume the pointer is not correctly set in the struct, but why/how? Any explanation or suggestion would be greatly appreciated! Thanks so much!

Example code (should compile as is):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>

using namespace std;

class Test
{
		int size;

	public:

		Test(long sequences)
		{
			size = sequences;
		}

		void setSize(int num)
		{
			size = num;
		}

		int getSize()
		{
			return size;
		}
};

struct Cnt
{
		int somethingElse;
		Test* test;
};

int myFunction(int, Test*);

int main(int argc, char* argv[])
{
	Cnt container;

	int rval = myFunction(4567, container.test);
	cout << "Returned:" << rval << endl;
	cout << "Test:" << container.test->getSize() << endl;

	return 0;
}

int myFunction(int num, Test* test)
{
	test = new Test(num);
	cout << "Set To:" << test->getSize() << endl;
	test->setSize(123);
	cout << "Changed To:" << test->getSize() << endl;
	return test->getSize();
}


Output:

Set To:4567
Changed To:123
Returned:123
Test:-1991643855
.... {Why is that??}
container.test is never initialized. If you think line 48 does this, you're incorrect. You're still passing by value (where you make a copy), it is just in this case you're passing a copy of the memory location of container.test (which is garbage). You immediately overwrite the copy on line 48, but you never do anything to get this new reference back into 'container'.
I made some changes from line 33 down:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int myFunction(int, Cnt&);

int main(int argc, char* argv[])
{
    Cnt container;

    int rval = myFunction(4567, container);
    cout << "Returned:" << rval << endl;
    cout << "Test:" << container.test->getSize() << endl;

    return 0;
}

int myFunction(int num, Cnt& contain)
{
    contain.test = new Test(num);
    cout << "Set To:" << contain.test->getSize() << endl;
    contain.test->setSize(123);
    cout << "Changed To:" << contain.test->getSize() << endl;
    return contain.test->getSize();
}


Now you pass the whole container by reference and set its test pointer inside the function.
Last edited on
If you think line 48 does this, you're incorrect.

Yes, I guess that is exactly what I falsely assumed - or hoped would happen. Clearly container.test remains un-initialized. How would I then pass the pointer correctly to myFunction (which is what I had actually intended)?
Thanks!

Edit: Oh, thanks so much for the solution!! I thought it's either a pointer or by reference, so it didn't occur to me to pass the Test* by reference &.
Last edited on
You could pass the pointer by reference (notice placements of the ampersand [&])...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int myFunction(int, Test*&);

int main(int argc, char* argv[])
{
    Cnt container;

    int rval = myFunction(4567, container.test);
    cout << "Returned:" << rval << endl;
    cout << "Test:" << container.test->getSize() << endl;

    return 0;
}

int myFunction(int num, Test*& test)
{
    test = new Test(num);
    cout << "Set To:" << test->getSize() << endl;
    test->setSize(123);
    cout << "Changed To:" << test->getSize() << endl;
    return test->getSize();
}
Last edited on
int myFunction(int num, Cnt& contain)
Sorry for the noob follow-up question, but is there a way to pass only the pointer (Test* test) by reference instead of the whole Cnt struct contain?
Check my previous response. :)
Awesome! Thanks!!
Topic archived. No new replies allowed.