Question about HW5 #536
-
이름 Your Name고상준 Sangjun Ko 질문 Question안녕하세요. HW5를 하다가 궁금한 점이 생겨서 질문드립니다. 혹시 이렇게 함수 안에서 선언된 변수 y의 주소를 리턴하는 경우, 원래라면 y는 스택에서 없어지니 (정확히 말하면 더 이상 유효한 스택이 아니니), 아마 C언어에서는 오류를 뱉을 것입니다. 근데 컴파일을 했을 때 오류가 안 생기는데, 혹시 이러한 예시는 테스트 케이스에 없다고 봐도 되는건가요? 즉, 스코프를 벗어난 변수의 주소를 사용하는 예시는 없다고 가정하고 구현해도 되나요? 번역본 Translated VersionHello, I have a question while working on HW5. In this case, returning the address of the variable y declared inside the function would normally cause an error in C, because y is removed from the Stack (more precisely, the stack is no longer valid) once the function returns. However, when I compile this, no error occurs. Can I assume that such cases are not included in the test cases? In other words, can I implement it under the assumption that there are no examples of using the address of a variable that has gone out of Scope? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
로컬 변수의 주소를 반환하는 경우, 컴파일 오류가 발생하지는 않지만 정의되지 않은 동작(Undefined Behavior)으로 이어집니다. 테스트로 주어지는 프로그램들은 0으로 나누기(division by zero)나 널 포인터 역참조(null dereference)를 제외하고는 다른 오류나 정의되지 않은 동작을 포함하지 않습니다. Returning the address of a local variable may not cause a compile-time error, but it results in undefined behavior at runtime. The test programs may include division-by-zero and null dereference errors, but will not contain any other types of errors or undefined behavior. |
Beta Was this translation helpful? Give feedback.
로컬 변수의 주소를 반환하는 경우, 컴파일 오류가 발생하지는 않지만 정의되지 않은 동작(Undefined Behavior)으로 이어집니다. 테스트로 주어지는 프로그램들은 0으로 나누기(division by zero)나 널 포인터 역참조(null dereference)를 제외하고는 다른 오류나 정의되지 않은 동작을 포함하지 않습니다.
Returning the address of a local variable may not cause a compile-time error, but it results in undefined behavior at runtime. The test programs may include division-by-zero and null dereference errors, but will not contain any other types of errors or undefined behavior.