Same address between functions. Coincidence or not?

Hello,
I'm new to C++ and was playing around with functions, pointers and references and noticed something. I have the following code:

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
  class myClass
{
public:
	static void myFunction1(int m_myInt)
	{
		std::cout << "---------- myfunction1(int m_myInt) ----------"	<< "\n";
		std::cout << "m_myInt = "	<< m_myInt						<< "\n";
		std::cout << "&m_myInt = "	<< &m_myInt						<< "\n";
		std::cout << "----------------------------------------------"	<< "\n\n\n";
	}

	static void myFunction3(int* m_ptr_myInt)
	{
		std::cout << "---------- myfunction3(int* m_ptr_myInt) ----------"<< "\n";
		std::cout << "m_ptr_myInt = "	<< m_ptr_myInt					<< "\n";
		std::cout << "&m_ptr_myInt = "	<< &m_ptr_myInt					<< "\n";
		std::cout << "*m_ptr_myInt = "	<< *m_ptr_myInt					<< "\n";
		std::cout << "---------------------------------------------------"<< "\n\n";
	}

};

int main()
{
	int	myInt		= 5;
	int*	ptr_myInt	= &myInt;

	std::cout << "***************************Variables***************************" << "\n";
	
	std::cout << "myInt = "		<< myInt	<< "\n";
	std::cout << "&myInt = "	<< &myInt	<< "\n\n";

	std::cout << "ptr_myInt = "	<< ptr_myInt	<< "\n";
	std::cout << "&ptr_myInt = "	<< &ptr_myInt	<< "\n";
	std::cout << "*ptr_myInt = "	<< *ptr_myInt	<< "\n";

	std::cout << "***************************************************************" << "\n\n";


	std::cout << "*************************Functon calls*************************" << "\n\n\n";
	
	myClass::myFunction1(myInt);
	myClass::myFunction3(&myInt);

	std::cout << "***************************************************************" << "\n\n";

	system("pause");
	return 0;
}


This gave me the following output:


***************************Variables***************************
myInt = 5
&myInt = 00F6FC30

ptr_myInt = 00F6FC30
&ptr_myInt = 00F6FC24
*ptr_myInt = 5
***************************************************************

*************************Functon calls*************************

---------- myfunction1(int m_myInt) ----------
m_myInt = 5
&m_myInt = 00F6FB50
----------------------------------------------


---------- myfunction3(int* m_ptr_myInt) ----------
m_ptr_myInt = 00F6FC30
&m_ptr_myInt = 00F6FB50
*m_ptr_myInt = 5
---------------------------------------------------

***************************************************************


Now to my question. "m_myInt" and "m_ptr_myInt" have the same address "00F6FB50". Is this just a coincidence and the programm uses the same address since it can, or is there some connection between them, that I dont see?
If they are not connected, can I enforce the use of a different address? (Just out of curiosity)

Thanks
loetmann
Last edited on
You've discovered the stack. The working memory.

When calling functions, parameters to that function are put onto the stack. When the function ends, that memory becomes available again. If you call another function, the parameters are put on the stack. In the same place the parameters to the previous function were placed.

See diagram on right here: https://en.wikipedia.org/wiki/Call_stack#Structure

It shows memory layouts as functions are called. You can see that if a function ends, and then another function is called, the parameters for that next function will go into the same memory location as the parameters for the function that just ended.

Not every system will use the memory in the same way, but it's pretty common.
Last edited on
Ah ok, that makes sense.

When I add a variable "int test = 12345;" betwen my functions calls, is this variable also stored on the stack? But than after calling myFunction3, m_prt_myInt should be on a different address, which in practice isn't the case. Why is that?
When I add a variable "int test = 12345;" betwen my functions calls,


But when is the memory for that variable actually allocated? Typically, at the start of the function call, no matter where in the function the variable appears to be created.

So by the time you're calling these functions, that variable int test is already there on the stack because it was put there at the start of the call to the function main, so it has no effect on the memory address those function parameters occupy.
Last edited on
Topic archived. No new replies allowed.