*** Quick Pointer Question ***

Hello so I have bumped into a small dilemma today.

I have one example.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>
#include <string>
#include <iostream>
using namespace std;

void SomeFunction(int* pInt){

  int myInt = 25;
  pInt = &myInt;

  cout << *pInt << endl;
  
}

int main(){
  
  int* pInt;
  
  SomeFunction(pInt);

  cout <<  *pInt << endl;
}


If I do that, I get, as I expect, a segmentation fault (core dumped) on the second print.

Correct me if I am wrong, this is because I have declared a local var in another function, given it a value, pointed a pointer at it, then exited the function. As a result, the var is deleted, and the pointer is pointing to nothing real, so I get a segmentation fault.

My next example (the one I really dont get).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>
#include <string>
#include <iostream>
using namespace std;

int* SomeFunction(){

  int myInt = 25;
  int* pInt = &myInt;
  
  cout << "pInt: " << *pInt << endl;
  
  return &myInt;
  
}

int main(){

  int* pReceivedInt = SomeFunction();

  cout << *pReceivedInt<< endl;

}


I get

test2.cxx:8: warning: address of local variable ‘myInt’ returned

But when I run, I get desired output.

pInt: 25
25

My question is - why do I get that second 25 on the output? Should't I get a segmentation fault as in the first program? What is going on here? Is the function not being deleted because I am returning a pointer? I have a situation where I am returning a 4 byte? pointer, which is fine, its data as well, but its pointing to some other local thing that SHOULD be deleted once I exit function.

Please explain!
Correct me if I am wrong, this is because I have declared a local var in another function, given it a value, pointed a pointer at it, then exited the function. As a result, the var is deleted, and the pointer is pointing to nothing real, so I get a segmentation fault.


You're sort of on the right track.

The real problem is that you've passed the variable (pInt) by value to the function. main() does not see the
change of the pointer value made by the function, and pInt is uninitialized in main().

[EDIT: If you fix that, say, to pass pInt by reference to the function, then main() will be accessing a variable that
was on the stack and is no longer there. I can assure you that that won't crash, and it may even by chance print
out the right value, but it is still not OK to do that.]
Last edited on
Wait, so pInt (the pointer) is uninitialized in the first program? I thought the problem is that the variable just gets deleted with the rest of the function upon exit, so the pointer (which is still there... I did cout for its address) just points to nothing real...

And why on the second program do I get no problems? Shouldnt that function be deleted upon exit ? Therefore deleting the data which the returned pointer points to?

If I dynamically allocated memory in both cases then I am guaranteed 100% work so I dont understand what you mean by main uninitializing the pointer, because main doesn't see that either.
Ok, I decided to confuse you even more >:D

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
#include <iostream>
using namespace std;

void func1(int * & pInt)
{
    int temp;
    temp=20;
    cout << "temp in func1: " << temp << endl;
    pInt=&temp;
}

void func2(int * & pInt)
{
    int temp;
    cout << "temp in func2: " << temp << endl;
}

int main()
{
    int* pInt;

    func1(pInt);

    cout << "enter a value for temp in func2: ";
    cin >> *pInt;

    func2(pInt);

    cin.get();
    return 0;
}
Last edited on
Those arguments I have no idea about.... Receives pointer to reference? Or vice versa? And it shouldnt print anything useful in func2..... idk if its just a joke or what )))

Im already confused!!!

Could you tell me why those two snippets of code I posted are the way they are? I dont understand why in the second I can view the value under the pointer even though its a value of a local variable in a function which was exited from.

Topic archived. No new replies allowed.