표현식(Expression)의 종류
- 수치 연산 표현식 (Arithmetic expressions)
- 관계 표현식 (Relational expressions)
- 참/거짓 표현식 (Boolean expressions)
수치 연산 표현식 (Arithmetic expressions)
- 구성 요소
- 연산자(Operator)
- 피연산자(Operand)
- 괄호(Parentheses)
- 함수 호출 (Function calls)
수치 연산 연산자의 종류
- 1인자 연산자 (un-ary)
- 2인자 연산자 (bin-ary)
- 3인자 연산자 (tern-ary)
- n인자 연산자 (n-ary)
수치 연산 연산자 평가 순서 (Operator Evaluation Order)
- 연산자 우선순위 (Operator Precedence Rules): 서로 다른 연산자 사이 평가 순서
일반적 우선순위:
괄호 > 1인자(un-ary) > 제곱(exponent) > 곱셈, 나눗셈 > 덧셈/뺄셈
- 연산자 결합 법칙 (Operator Associativity Rules): 같은 연산자 사이 평가 순서
일반적 결합법칙:
좌 -> 우
역방향 결합법칙:
제곱(exponent), 1인자 연산자(un-ary)
수치 연산 피연산자
- 변수 (Variables)
- 상수 (Constants)
- 괄호된 표현식 (Parenthesized expressions)
- 함수 참조 (Function reference)
수치 연산 함수 참조시 주의 사항
- 양 방향 변수(two-way variables)
- 비 지역 변수(non-local variables)
-> 함수 참조 순서에 따라 결과가 달라짐
- 부표현식(sub-expressions: ++, --) 등에 의해서도 문제 발생 가능
중첩된 연산자 (Overloaded operators)
- 여러가지 목적으로 사용되는 같은 모양의 연산자 존재
a * b 곱하기, *p 역참조
a & b AND 연산, &v 주소 얻기
int + int 정수 덧셈, int + float 정수 실수 덧셈, float + int 실수 정수 덧셈,
float + float 실수 실수 덧셈
- 가독성(readability) 저하
-> 각 기능별 연산자를 나누어서 해결 가능
실수 나누기 /, 정수 나누기 //
사용자 정의 중첩된 연산자 (User-defined overloaded operator)
- 쓰기 (writability) 편함
- 가독성 (readability) 증가
- 넌센스적인 사용 가능 ( 더하기를 빼기로 바꿀 수 있음) -> 혼란 가중
- 실제 작동 코드 예측 불가
Narrow-ing Type conversion: 고정밀도(실수) -> 저정밀도(정수) 변환
- 정보 유실 가능
Widen-ing Type conversion: 저정밀도(정수) -> 고정밀도(실수) 변환
- 표현 비트 부족(32bit 정수 -> float 변환시 정수부는 24bit 로 부족)
Implict Type Conversion(Coercion)
- 피연산자 타입이 다를때 묵시적 변환
- 사용하기 편하나 타입 에러 찾기 어려움
Explicit Type Conversion(Cast)
- 프로그래머가 명시적으로 타입 변환
수치 연산 표현식에서 발생하는 문제들
- 0 나누기
- 오버플로우(Overflow) 표현가능한 숫자보다 큰 수
- 언더플로우(Underflow) 표현 가능한 숫자보다 작은 수
관계 표현식(Relation Expressions)
- c에서는 true(non-zero) 와 false(0) 으로 구분
- -3 < -2 < -1 은 허용되지만 의도대로 작동 안함
(-3 < -2) < -1 = (1) < -1 = false
관계 표현식에서 단축 평가법 Short circuit Evaluation
- 이미 참거짓이 판별되면 다음 관계 표현식을 평가하지 않고 넘어감
- 함수 호출이나 서브 표현식(++, --) 이 있을 경우 부작용 발생 가능
대입문
- 다중 대상 Multiple Targets
- 조건 대상 Conditional Targets
- Compound Assignment +=, -=, *=, /=
- 단항 대입 연산 Un-ary Assignment Operators
프로그래밍 언어론: 표현식과 대입문(Expressions and Assignment statements)
Posted at 2007/10/23 09:46// Posted in study/pl
Tag pl
프로그래밍 언어론: 포인터와 레퍼런스
Posted at 2007/10/16 09:41// Posted in study/pl
포인터의 값 Values of Pointers
- 메모리 주소 Memory addresses
- nil 값 (or null)
포인터의 용도 Usage of Pointers
- 간접 주소 접근 Indirect Addressing
- 동적 저장 장치 관리 Dynamic Storage Management
포인터 디자인의 쟁점 사항 Design Issues of Pointers
- 포인터 변수의 범위와 라이프 타임 scope and lifetime
- 힙 다이나믹 변수의 라이프 타임 lifetime of heap-dynamic variables
- 특정 타입만 포인팅 여부 Pointers restricted to pointing at a particular type
- 포인터 용도 제한 Pointers used for dynamic storage management, indirect addressing or both
- 포인터, 레퍼런스 지원 여부 Language support point types, reference types or both
포인터 연산 Pointer Operations
- 어싸인먼트 Assignment
ptr = addr;
ptr = &var;
ref = *addr;
ref = &var;
- 디레퍼런싱 Deferencing
var = *ptr; // 명시적 디레퍼런싱 Explicit de-referencing
var = ref; // 묵시적 디레퍼런싱 Implicit de-referencing
포인터 문제
- 댕글링 포인터 Dangling Pointers
이미 해제된 메모리를 포인팅
- 댕글링 오브젝트(가비지) Dangling Object or Garbage
프로그램내 어떤 포인터도 포인팅하지 않는 힙 다이나믹 변수
deallocation 로 인해 발생 (c 언어 delete, pascal dispose)
Ada 의 포인터
- 모든 포인터(access) 는 null 로 초기화 된다
- Dangling Object 문제 해결
힙 다이나믹 변수가 범위를 벗어나면 자동으로 deallocate 된다
- Dangling Pointer 문제 해결
명시적 deallocation 을 지원함으로 인해 발생
C/C++ 의 포인터 지원
- 명시적 디레퍼런싱 Explicit de-referencing *ptr
- 주소 얻기 오퍼레이터 Address-of operator
- 제한된 폼으로 주소 연산 가능 Address arithmetic in restricted forms
*(ptr + index) == ptr[index]
- void * 를 사용해 어떤 타입도 포인트 가능하지만 직접 디레퍼런싱은 불가능하며 캐스팅 해야한다.
C++ 의 레퍼런스 Reference types in C++
- 묵시적 디레퍼런스되는 상수 포인터 Constant Pointer Implicitly de-referenced
자바의 레퍼런스 Reference types in Java
- 포인터 없음 No pointer types
- 포인터 연산 불가 No pointer arithmetic
- 클래스 인스턴스만 레퍼런스 가능
- 명시적 해제 없음 No explicit de-allocation
- 묵시적 디레퍼런싱 Implicit de-referencing
Dangling 포인터 문제 해결 방법
- Tombstone
- Locks-and-Keys
- 프로그래머에게 de-allocation 권한을 주지 않고 실시간으로 힙 다이나믹 변수 de-allocation
Tombstone
- 힙 다이나믹 변수를 직접 포인트하지않고 중간에 tombstone 을 두어 포인터들은 tomebstone 을 포인팅한다.
- 만약 힙 다이나믹 변수가 de-allocate 되면 tombestone 을 null 로 만든다.
- 포인터들은 tombstone 의 null 여부를 체크하면 된다.
- de-referencing 을 두번해야하므로 속도 저하
- tombstone 공간을 만들어주어야함
- de-allocate 시 tombstone null 처리
- 메모리 주소 Memory addresses
- nil 값 (or null)
포인터의 용도 Usage of Pointers
- 간접 주소 접근 Indirect Addressing
- 동적 저장 장치 관리 Dynamic Storage Management
포인터 디자인의 쟁점 사항 Design Issues of Pointers
- 포인터 변수의 범위와 라이프 타임 scope and lifetime
- 힙 다이나믹 변수의 라이프 타임 lifetime of heap-dynamic variables
- 특정 타입만 포인팅 여부 Pointers restricted to pointing at a particular type
- 포인터 용도 제한 Pointers used for dynamic storage management, indirect addressing or both
- 포인터, 레퍼런스 지원 여부 Language support point types, reference types or both
포인터 연산 Pointer Operations
- 어싸인먼트 Assignment
ptr = addr;
ptr = &var;
ref = *addr;
ref = &var;
- 디레퍼런싱 Deferencing
var = *ptr; // 명시적 디레퍼런싱 Explicit de-referencing
var = ref; // 묵시적 디레퍼런싱 Implicit de-referencing
포인터 문제
- 댕글링 포인터 Dangling Pointers
이미 해제된 메모리를 포인팅
- 댕글링 오브젝트(가비지) Dangling Object or Garbage
프로그램내 어떤 포인터도 포인팅하지 않는 힙 다이나믹 변수
deallocation 로 인해 발생 (c 언어 delete, pascal dispose)
Ada 의 포인터
- 모든 포인터(access) 는 null 로 초기화 된다
- Dangling Object 문제 해결
힙 다이나믹 변수가 범위를 벗어나면 자동으로 deallocate 된다
- Dangling Pointer 문제 해결
명시적 deallocation 을 지원함으로 인해 발생
C/C++ 의 포인터 지원
- 명시적 디레퍼런싱 Explicit de-referencing *ptr
- 주소 얻기 오퍼레이터 Address-of operator
- 제한된 폼으로 주소 연산 가능 Address arithmetic in restricted forms
*(ptr + index) == ptr[index]
- void * 를 사용해 어떤 타입도 포인트 가능하지만 직접 디레퍼런싱은 불가능하며 캐스팅 해야한다.
C++ 의 레퍼런스 Reference types in C++
- 묵시적 디레퍼런스되는 상수 포인터 Constant Pointer Implicitly de-referenced
자바의 레퍼런스 Reference types in Java
- 포인터 없음 No pointer types
- 포인터 연산 불가 No pointer arithmetic
- 클래스 인스턴스만 레퍼런스 가능
- 명시적 해제 없음 No explicit de-allocation
- 묵시적 디레퍼런싱 Implicit de-referencing
Dangling 포인터 문제 해결 방법
- Tombstone
- Locks-and-Keys
- 프로그래머에게 de-allocation 권한을 주지 않고 실시간으로 힙 다이나믹 변수 de-allocation
Tombstone
- 힙 다이나믹 변수를 직접 포인트하지않고 중간에 tombstone 을 두어 포인터들은 tomebstone 을 포인팅한다.
- 만약 힙 다이나믹 변수가 de-allocate 되면 tombestone 을 null 로 만든다.
- 포인터들은 tombstone 의 null 여부를 체크하면 된다.
- de-referencing 을 두번해야하므로 속도 저하
- tombstone 공간을 만들어주어야함
- de-allocate 시 tombstone null 처리
Tag pl


python 을 좋아하는 게임 프로그래머