Programming Language) Week7 -Memory binding & Variable Types

Jay Kim·2022년 1월 17일
0

Programming Language

목록 보기
7/11


1.Scope

-scope =변수에 엑세스 할 수 있는 범위
-visible = 변수에 엑세스 가능함을 의미
-Static scope rule

-scope : 블록 간의 중첩 관계
-비지역 변수(non-local) : 가장 가까운 중첩 블록에 선언된 변수 이용 컴파일 타임에 범위 결정됨

-Dynamic scope rule

-function call order
-비지역 변수(non-local) : 함수 호출에서 변수 search
-런타임에 범위 결정됨

Example )
    program scope_rule;
    	var x: integer;
        procedure sub1;
        begin 
        	print(x)
        end;
        procedure sub2; 
        	var x: integer;
            	begin 
                	x:=4;
                    sub1
                 end;
        begin 
        	x:=2;
            sub2
        end

**==>해석: scope_rule > sub1, sub2

-Dynamic scope rule: print(x)=4 // 현재 활성화 된 다른 하위 프로그램 변수 엑세스 가능하므로 x=4
-static scope rule : print(x)= 2 // 부모 영역에 있는 변수 엑세스 가능하므로 x=2


Lifetime

Lifetime : 메모리 할당 된 시간(allocation) ~ 메모리 해제하는 시간까지(return)
-block structur language에서는 scope = lifetime
static in C,C++ 에서의
-scope = static, local to function
-lifetime = 프로그램이 끝날 때까지

3)Referential Environment

-Referential Envireonment => 사용할 수 있는 데이터, 변수이름, 함수 등 참조 환경

1)Static scope language => 지역 변수 + 부모 영역에 있는 변수 모두 엑세스 가능
2)Dynamic scope language => 지역 변수 + 현재 활성화 된 다른 하위 프로그램의 변수

4)Memory binding & Variable Types

1.Static variables

-실행 전 로딩 시간에 binding 발생 , 프로그램 끝날 때 까지 살아있음
-stack에 저장되며 끝나면 자동으로 메모리 해제됨
-단점) no recursion , 변수들은 메모리 위치 공유 불가

2.Stack-dynamic variables

-시작할 때 메모리 만들어지고 함수 끝나면 자동으로 메모리 해제됨(Runtime stack)
-static variables의 no recursion 단점 보완 -> 서브 프로그램은 지역 변수에 대한 자체 메모리 필요 (Recursive program)

3.Explicit heap-dynamic variables

-explicit runtime instruction => 명령어를 통해 메모리 해제
Heap에 저장되며 명령어를 통해 메모리 해제 하지 않을 경우 garbage가 됨(프로그램이 끝나면 모두 없어지긴 함)
-포인터, 참조 변수로만 엑세스 가능
-해제 명령어 ) C - malloc(), free() function / C++ - new, delete operator

0개의 댓글

Powered by GraphCDN, the GraphQL CDN