Help with passing by reference

Hello, I'm trying to pass a pointer variable but when I return from the function call, the variable is empty even though it gets modified during the function. Any ideas on how I can get the modified value? Following is the code:

1
2
3
4
5
6
7
8
9
			empInfo = new EmpInfo ();
			if (!IsEmpInMemory(strOwner, fOwnerInvalid, empInfo ))
			{
				if (ValidateEmpExists(strOwner, fOwnerInvalid, empInfo ))
				{
					g_cEmpList.AddTail(empInfo );
				}
			}
			strBoss = empInfo ->GetBoss();


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
BOOL IsEmpInMemory(const CString& strEmp, BOOL& fEmpInvalid, EmpInfo* empInfo )
{
	POSITION				cCurrentPosition;

	for (cCurrentPosition = g_cEmpList.GetHeadPosition(); NULL != cCurrentPosition;)
	{
		empInfo = (EmpInfo*) g_cEmpList.GetNext(cCurrentPosition);
		if (0 == strEmp.CompareNoCase(empInfo ->GetEmpName()))
		{
			if (empInfo ->IsEmpInvalid())
			{
				fEmpInvalid = TRUE;
			}
			return (TRUE);
		}
	}
	return (FALSE);
}
I think the issue is in Line 7 of the second block of code. Why would "g_cEmpList" need to be cast to an 'EmpInfo' pointer for the assignment to work?
I inherited this code and did not catch that issue, thanks. Although it is unnecessary, it isn't the reason that this problem is occurring, I just removed the cast and still same issue.
Last edited on
Where is "empInfo" getting assigned data? Is it in Line 7 of the second code block? Because that's the only place I see it occuring. Also, unless "cCurrentPosition" is a pointer, I don't see where it is getting incremented.

This is difficult to determine without the rest of the code. But at the same time I have a feeling that the code is a little long.

If I had to venture a guess, I'd say that the "GetNext()" member function isn't returning any data. This may be because of several things, none of which I would be able to verify with what is here.

Is the for loop even executing? I noticed that the condition is that cCurrentPosition not be equal to NULL, but if it isn't being assigned anything by "g_cEmpList.GetHeadPosition()" then it wouldn't execute. Then again you don't initialize cCurrentPosition to anything so if it contains garbage data it might not evaluate as NULL.
"g_cEmpList.GetNext(cCurrentPosition)"

This call not only gets the next member in list but also sets cCurrentPosition (effectively incrementing it). I know this logic makes little sense but I didn't code this.

I know this function works properly without the requirement of letting the calling function access the updated empInfo. I've debugged through it and basically what happens is that the empInfo gets properly set but as soon as I leave the function and return to the call, it's returned to being NULL.

It must be a problem with how I'm passing the parameter (or initializing it?) with pointers and such, I'm not too good with pointers, heh.
"empInfo" seems to be a pointer, if it is being set to point at a variable that is only alive during that function then this could cause the problem you are seeing. Try declaring "empInfo" as a non-pointer variable and passing it into the function by reference.
Topic archived. No new replies allowed.