2012-10-05 2 views
0

배열의 크기를 조정하는 기능을 찾았지만 제대로 작동하는지 이해하는 데 문제가 있습니다. 테스트를 위해 "temp"배열을 새 값으로 설정하고 "startCounter"가 해당 값에 할당되지만 startCounter의 메모리 위치는 변경되지 않습니다. 여기배열 크기 조정 기능이 메모리 위치를 변경하지 않습니다.

int * startCounter; 


void resizeArray(int *&arraySent,int origSize,int newSize) { 
    output << "&arraySent " << &arraySent << endl; 
    output << "arraySent[0] " << arraySent[0] << endl; 
    int* temp = new int[newSize]; 
    output << "&temp " << &temp << endl; 
    for (int i=0; i<origSize; i++) { 
     temp[i] = arraySent[i]; 
    } 
    temp[0]=744; 
    delete [] arraySent; 
    arraySent = temp; 
    output << "&arraySent " << &arraySent << endl; 
} 

//.... 

startCounter = new int [3]; 
startCounter[0]=345; 
output << &startCounter << endl; 
resizeArray(startCounter,3,10); 
output << "startCounter[0]" << startCounter[0] << endl; 
output << "&startCounter" << &startCounter << endl; 

나는이에서 얻을 출력됩니다 :

&startCounter 0x60fab8 
    &arraySent 0x60fab8 
    arraySent[0] 345 
    &temp 0x82cfe54 
    &arraySent 0x60fab8 
    startCounter[0] 744 
    &startCounter 0x60fab8 

내 질문은, 왜 startCounter의 메모리 위치가 없습니다 그것을 삭제하고에 할당 한 후 0x60fab8에서 변경 않습니다 여기 내 코드입니다 새로운 "temp"배열? 이제 0x82cfe54가되어서는 안됩니까?

P. 나는 벡터들에 대해 이해하지만,이 특정 기능이 어떻게 작동하는지 이해하는 것에 주로 관심이 있습니다.

답변

2
void resizeArray(int *&arraySent,int origSize,int newSize) { 
    output << "&arraySent " << &arraySent << endl; 

은 보유하고있는 주소가 아니라 포인터 변수의 주소를 출력합니다.

단순히 (아마) 효과

+0

그건 ... 말이 되네. 감사. – Shawn

1

&startCounter 포인터의 주소,하지가 가리키는 주소가 의도 얻기 위해 주소 연산자를 생략합니다. 그것은 가치가 변하지 않을 것입니다. 단순히 startCounter을 사용하십시오.

관련 문제