C에는 사실 Call by Value밖에 없다

MySprtlty·2023년 2월 11일
1

C

목록 보기
27/37

One aspect of C functions may be unfamiliar to programmers who are used to some other
languages, particulary Fortran. In C, all function arguments are passed 'by value'. This means
that the called function is given the values of its arguments in temporary variables rather than
the originals. This leads to some different properties than are seen with 'call by reference'
languages like Fortran or with var parameters in Pascal, in which the called routine has access
to the original argument, not a local copy. -TCPL-

  • 흔히, C언어에는 함수에게 값을 전달하는 방식에 Call by Pointer(Address)와 Call by Value 두 종류가 있다고 한다.
    • 🖇️cf. C에는 Call by Reference가 없다.
      (시중에서 흔하게 구입할 수 있는 C언어 책들이 대표적으로 잘못 설명하는 오개념이다.)
  • Call by Pointer도 사실은 포인터 값(주소 값)을 전달하는 것이라서, 사실상 Call by Value밖에 없다고 볼 수 있다
  • 그런데 이런식으로 생각하면, cpp의 Call by Reference도 어셈블리 레벨로 가면 Call by Pointer와 완전히 동일하게 번역되기에 cpp도 Call by Value밖에 없는 것 아닌가 하는 생각이 들었다.
  • 좀 더 나아가 어차피 컴퓨터는 서브루틴을 호출 할때, 인자를 레지스터로 전달하거나 스택을 이용하여 전달하니까 그냥 싹다 Call by Value 아닌가 라는 생각도 든다.
  • 물론 추상화된 언어 차원에서 cpp는 세가지 방식(Call by Value, Call by Pointer, Call by Reference)이 있다고 보는 것이 당연히 옳다.
  • 로우 레벨이 아닌 언어 레벨의 관점으로 바라봐아야 할 문제라는 생각이 들었다.
  • 사실 이 글은 잠이 안와 뒤척이다가 쓰는 뻘글이다.

0개의 댓글