Returning value to pointer argument

I have a question about pointers in C++. I am currently making functions that use pointers to (optionally) return extra information that cannot be contained in the single return variable. However, I am nervous about whether this method is kosher or not because I assume the memory address attached to the local variable inside the function is kind of in limbo once the function goes out of scope.

Here's a simplified example of what I mean:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int getTwoValues(int* pValue2 = NULL)
{
  int funcValue1 = 1;
  int funcValue2 = 2;
  
  if(pValue2 != NULL) {
    *pValue2 = funcValue2;
  }

  return funcValue1;
}


int main()
{
  int value1;
  int value2;

  value1 = getTwoValues(&value2);

  std::cout << value1 << " " << value2 << std::endl;
  
  return 0;
}  



this code compiles fine, and outputs to the command prompt, as expected: 1 2

Question 1:
Is the memory block associated with value2 in any danger because it is associated with a variable that has gone out of scope?

Question 1 Addendum:
If there is no danger here, is it generally better to write *pValue2 = funcValue2; OR pValue2 = &funcValue2; ? OR does it absolutely not matter either way?

Question 2:
Is there a better way to do what I am trying to do without using a structure / std::pair as a return type? I want the second return value to be completely optional and invisible to the user. In other words, if they don't want the second value, they shouldn't need to go out of their way to extract only the first value. Just ignore the optional argument and go about their day.

Thanks!
closed account (D80DSL3A)
Your method is safe. It doesn't utilize the address of the local variable funcValue2 , it's just using a pointer to copy a value to the value2 variable in main().

is it generally better to write *pValue2 = funcValue2;

That's all that will work given that you have passed a pointer by value (not by reference).

OR pValue2 = &funcValue2; ?

Have you tried that yet? It copies the address of funcvalue2, but to a local copy of the pointer which was passed by value. This won't change the value of value2 in main.

You would have to pass the pointer by reference in order to retrieve the address of funcValue2 from the function. Doing this could cause the trouble you're asking about. This would require passing arguments differently, destroying the "optional retrieval" method that you want.

You could also retrieve the address of funcValue2 using a pointer to a pointer. This could be passed by value which permits giving a default value once again. The argument could be omitted in the function call, as per your approach using a pointer passed by value.

Here's some code demonstrating all of this:
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
54
#include<iostream>

int getTwoValues(int*& p_int)// passing pointer by reference
{
  int funcValue1 = 1;
  int funcValue2 = 2;

    p_int = &funcValue2;// copy address of local variable

  return funcValue1;
}

// overloaded function
int getTwoValues(int** pp_int = NULL)// passing pointer to pointer by value
{
  int funcValue1 = 1;
  int funcValue2 = 2;

    if( pp_int != NULL )
        *pp_int = &funcValue2;// copy address of local variable to pointer pointed to by ppInt

  return funcValue1;
}

int main()
{
    int value1 = 5;// giving initial values here
    int value2 = 5;
    int *pInt = &value2;// an actual pointer will be needed for the 1st function call below.

    // show address where value2 is stored
    std::cout << "value2 is stored at: " << &value2 << std::endl;
    // show address pointed to by pInt. Should be same as above
    std::cout << "pInt points to: " << pInt << std::endl;

    // passing a pointer by reference
    value1 = getTwoValues(pInt);// must pass an "actual" pointer, not just an address value.

    // note how the address pointed to by pInt has changed
    std::cout << "now pInt points to: " << pInt << std::endl;
    std::cout << value1 << " " << value2 << std::endl;// value2 unaffected

    // now for the pointer to pointer approach
    // make pInt point to value2 again
    pInt = &value2;
    int **ppInt = &pInt;// make ppInt point to pInt

    // retrieve address of local variable via pointer to pointer
    value1 = getTwoValues(ppInt);
    // the address pointed to by pInt has been changed
    std::cout << "now pInt points to: " << pInt << std::endl;

    return 0;
}

Output on my machine at the present time:

value2 is stored at: 0x23fe2c
pInt points to: 0x23fe2c
now pInt points to: 0x23fde8
1 5
now pInt points to: 0x23fde8

The last 2 addresses shown are the address of funcValue2 from the function.
Trouble would result if the address of funcValue2 was used in main, other than to just display it as was done here.
Last edited on
So much sense! You are the man! Thank you!
Topic archived. No new replies allowed.