배열의 length overflow를 질문한것은 아닙니다.
find 함수가 원하는 값을 찾지 못했을때 last Iterator리턴하는데..
그것이 가리키는 값을 바꾸어 버리는가 하는 점이죠
find 함수 호출후 우연히 마지막 iterator 위치의 값(처음엔 쓰레기값)이 찾고자 했던 값(15)으로 바뀐것 같네요
Lyn 님이 쓰신 글 :
: 소스 자체가 오류입니다.
:
: a[10] 이라고 선언했으면 a[0]~a[9] 까지 존재하지 a[10]은 Length over 입니다
: a+10도 마찬가지구요
:
: 그리고 마지막 이터레이터는 접근하면 안되는값입니다.
: < 연산으로 끝을 점검 하기 위해 존재하는 마지막값 다음을 가르키게 되어 있거든요 ㅡ.ㅡ;
:
: 크레브 님이 쓰신 글 :
: : 요즘 STL 조금 보고 있는데..
: : 이상한 부분이 있어서 따로 질문할데도 없고해서 여기에 올립니다.
: :
: : 아래 소스 실행해 보시면
: : find 함수 실행후에 할당도 안된 메모리인 a[10] 의 내용이 변하는것을 알수 있습니다.
: : find 알고리즘이 찾은 내용이 없을때 last Iterator의 값을 바꾸어 버리는건가요?
: :
: :
: : //---------------------------------------------------------------------------
: :
: : #pragma hdrstop
: :
: : #include
: : #include
: : #include
: : #include
: : #include
: :
: : using namespace std;
: :
: :
: :
: : //---------------------------------------------------------------------------
: :
: : #pragma argsused
: : int main(int argc, char* argv[])
: : {
: : int a[10] = {1, 2, 3, 4, 5 , 6, 7, 8, 9 , 10};
: :
: : int * ptr;
: :
: : printf("pointer of (a+10) = %02p\n", a+10);
: : printf("value of (a+10) = %d\n", *(a+10));
: :
: : ptr = find(a , a+10, 9);
: : printf("%02p\n", ptr);
: : cout <<"<----result=" << *ptr << endl;
: :
: : ptr = find(a , a+10, 10);
: : printf("%02p\n", ptr);
: : cout <<"<----result=" << *ptr << endl;
: :
: : ptr = find(a , a+10, 15);
: : printf("%02p\n", ptr);
: : cout <<"<----result=" << *ptr << endl;
: :
: : cout <<"result=" << *ptr << endl;
: : cout <<"result+1=" << *(ptr+1) << endl;
: : cout <<"result+2=" << *(ptr+2) << endl;
: : cout <<"result+3=" << *(ptr+3) << endl;
: :
: : printf("value of (a+10) = %d\n", *(a+10));
: : printf("value of (ptr) = %d\n", *ptr);
: :
: :
: : // list list1(a , a+10);
: : // cout << "count=" << list1.size();
: : //list::iterator i =
: :
: : getch();
: : return 0;
: : }
: : //---------------------------------------------------------------------------
: :
: :
: :
: : 실행 결과
: :
: :
: : pointer of (a+10) = 0013FF7C
: : value of (a+10) = 4472480
: : 0013FF74
: : <----result=9
: : 0013FF78
: : <----result=10
: : 0013FF7C
: : <----result=15
: : result=15
: : result+1=10
: : result+2=9
: : result+3=1310588
: : value of (a+10) = 15
: : value of (ptr) = 15
: :