소개
게시글
질문&답변
2022.10.18
Buffer overrun 관련해서 질문드립니다!
안녕하세요! 일단 저는 VS2022 사용하고 있고 얼마전 stackoverflow를 찾아본 결과 vs에서 발생하는 일종의 버그라고 생각됩니다! https://stackoverflow.com/questions/64713842/any-way-to-avoid-warning-c6386-without-disabling-it-or-code-analysis-altogether Complier explorer를 통해 gcc 등 컴파일러로 실행해봤는데 warning이 잡히지 않았습니다! VS code analyzer가 위 코드의 list.size()가 0보다 큰 지 확실하게 판단하기 어려워서 해당 버그가 발생하는것으로 추정됩니다! 이를 max(list.size(), 0)을 사용한 트릭으로 없앨 수는 있다고는 하는데 근본적인 해결책은 정확히 모르겠네요.
- 0
- 2
- 274
질문&답변
2022.10.16
cout<<mon 질문
교수님께서 Position header안에 Position2D class에 대해서도 다시 한번 확인해 보시면 이해하실 거예요!
- 0
- 2
- 160
질문&답변
2022.10.15
<< 오버로딩 시 const 유무에 따른 문제
d++의 경우 Digit operator ++ (int) { Digit temp(m_digit); ++(*this); return temp; } 에서 반환하는 값은 기본적으로 rvalue입니다. pointer나 reference 반환을 하지 않는 이상 함수를 호출한 expression에 return by value가 rvalue로 "잠시" 대체 되는 것입니다. d++앞에 주소를 확인하기 위해 &를 붙여보면 rvalue임을 확인할 수 있습니다. rvalue의 경우에는 함수 parameter가 reference로 받을 때(pass by reference)는 const를 꼭 붙여주어야 합니다. 따라서 const reference는 rvalue의 수명을 연장시켜 준다고 합니다. 이와 관련해서는 아래링크를 통해 확인할 수 있습니다! https://stackoverflow.com/questions/36102728/why-is-it-allowed-to-pass-r-values-by-const-reference-but-not-by-normal-referenc?newreg=af09de2f1d8d479ca830d2af2a5cb732
- 0
- 2
- 146
질문&답변
2022.10.14
후위 연산자에서 this가 아닌 다른 인스턴스에 왜 주소가 아닌 값을 넘겨주면 안되나요?
Digit& operator ++ ( int ) { Digit temp(m_digit); ++(* this ); return *temp; } 함수 안에서 정의된 temp라는 instance의 scope는 함수 내부가 됩니다. 함수를 벗어나게 되면 소멸하게 되는 거죠. reference로 반환을 하려면 원본이 존재해야하는데 함수 밖을 나가면 사라지는 temp를 return by reference하면 reference는 원본을 찾을 수 없습니다.
- 0
- 2
- 119
질문&답변
2022.10.14
초급한 질문이라 죄송합니다.
윗 분 말씀처럼 rvalue와 관련이 있습니다. -cents는 Cents(-m_cents)를 return하는데 이건 익명 객체이죠. 익명 객체(Cents(10) 등)는 rvalue로 여겨지는데 함수로 pass rvalue를 하려면 parameter는 const reference만 가능합니다. rvalue를 왜 const reference로만 받아야 하는가에 대해서는 아래 링크에 설명되어있습니다. https://stackoverflow.com/questions/36102728/why-is-it-allowed-to-pass-r-values-by-const-reference-but-not-by-normal-referenc?newreg=af09de2f1d8d479ca830d2af2a5cb732 한 가지 예시가 나오는데 void display(int& a) { a = 10 }; display(1); 가 있다면 말 그대로 a = 10은 rvalue에 rvalue를 넣으라는 말입니다. 말이 안되죠. 따라서 질문자님이 말씀하신 reference만 사용할 수 없습니다 그럼 왜 const가 이 역할을 하는가는 c++의 feature로 인한것 같습니다. Q1: Is the following code legal C++? // Example 1 string f() { return "abc"; } void g() { const string& s = f(); cout } A1: Yes. This is a C++ feature… the code is valid and does exactly what it appears to do. Normally, a temporary object lasts only until the end of the full expression in which it appears. However, C++ deliberately specifies that binding a temporary object to a reference to const on the stack lengthens the lifetime of the temporary to the lifetime of the reference itself, and thus avoids what would otherwise be a common dangling-reference error. In the example above, the temporary returned by f() lives until the closing curly brace. (Note this only applies to stack-based references. It doesn’t work for references that are members of objects.) Does this work in practice? Yes, it works on all compilers I tried (except Digital Mars 8.50, so I sent a bug report to Walter to rattle his cage :-) and he quickly fixed it for the Digital Mars 8.51.0 beta). Q2: What if we take out the const… is Example 2 still legal C++? // Example 2 string f() { return "abc"; } void g() { string& s = f(); // still legal? cout } A2: No. The "const" is important. The first line is an error and the code won’t compile portably with this reference to non-const, because f() returns a temporary object (i.e., rvalue) and only lvalues can be bound to references to non-const. 요약하자면 const reference는 temporary object(rvalue)의 수명을 연장시켜 준다고 합니다. reference로만 받았을 때는 그 수명이 해당 expression에만 머물러 문제가 발생합니다. 덕분에 저도 찾아보면서 많은 공부가 되었습니다! 혹시 잘못된 내용있으면 답변으로 수정 부탁드립니다.
- 0
- 5
- 147