#binding

cachi·2020년 10월 7일
0
* 함수의 바인딩
함수를 만들어 컴파일을 하면 각각의 코드가 메모리 어딘가에 저장된다.
그리고 함수를 호출하는 부분에는 그 함수가 저장된 메모리 번지수(주소값)이 저장된다.
" 프로그램 실행 → 함수 호출 → 함수가 저장된 주소로 점프 → 함수 실행 → 원래 위치 "
위 과정에서 함수를 호출하는 부분에 함수가 위치한 메모리 번지로 연결해 주는 것을 바인딩(Binding)이라고 한다.

* 함수를 바인딩하는 2가지 방법
(1) 정적 바인딩 (일반 함수)
컴파일 시간에 호출될 함수로 점프할 주소가 결정되어 바인딩 되는 것.
(2) 동적 바인딩 (가상 함수)
실행 파일을 만들 때 바인딩 되지 않고 보류 상태 둔다.
점프할 메모리 번지를 저장하기 위한 메모리 공간(4 byte)을 가지고 있다가 런타임에 결정.
단점 : 타입 체킹으로 인한 수행 속도 저하 / 메모리 공간 낭비 -> 가급적 정적 바인딩 사용
- 단점이 있음에도 불구하고 동적 바인딩을 하는 이유?
어떤 포인터에 의해 접근되었는 지에 상관없이
참조된 인스턴스의 실제 클래스형에 따라 재정의된 함수 호출이 가능.

* Understanding Method Calls
Suppose obj is declared to be of type C.
Consider a method call:
obj.f(args) ; args (String)
Step1: at compile time
  • The compiler finds all accessible methods called f in C and its superclasses.
  • The compiler selects the method whose parameter types match the argument types(overloading resolution).
  • If there is no matching to the call f, then compile-error occurs.
  • If the method is private, static, or final, then the compiler binds the matched method to the call f (static binding).
Step2: at runtime
  • The exact method is found at runtime (dynamic binding).

[출처]

https://secretroute.tistory.com/entry/140819
Chapter 5, Core Java, Volume I

0개의 댓글