Firmware_Memory/C, C++

cout 으로 함수, 함수 포인터의 주소를 읽어보자

페타바이트 2022. 5. 4. 15:29

 

cout으로 함수 주소를 읽으려고 하면 1 이라는 숫자만 나오게 된다.

 

그 원인은 ostream 함수에서 overloading으로 bool 타입 함수를 가져와서 그런 것이라고 한다.

 

따라서 overloading에서 bool 타입을 선택하지 않도록 void * 로 형변환을 해서 넣어주면 주소 값을 확인 할 수 있다.

 

 

 

 

#include <iostream>
using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int sum (int a, int b)
{
	return a+b;
}

int main(int argc, char** argv) 
{
	
	int (*ptr_fun) (int alpha,int beta);
	
	ptr_fun = sum;
	
	cout << "sum address : " << sum  << '\n';
	
	cout << "ptr_fun point address : " << ptr_fun << '\n';
	
	cout << "ptr_fun address : " << &ptr_fun << '\n';
	
	
	
	cout << "sum address : " << reinterpret_cast<void *>(sum)  << '\n';
	
	cout << "ptr_fun point address : " << reinterpret_cast<void *>(ptr_fun) << '\n';
	
	cout << "ptr_fun address : " << &ptr_fun << '\n';
	
	
	
	printf("sum address : %x\n" , sum );
	
	printf("ptr_fun point address : %x\n", ptr_fun);
	
	printf("ptr_fun address : %x\n" , &ptr_fun);

	return 0;
}