2015-01-26 3 views
1

배열을 함수에 전달한 다음 해당 배열 변수를 상기 함수 내에서 새 주소로 지정하려고합니다.배열에 다른 주소를 어떻게 할당합니까?

배열이 함수에 전달 될 때 첫 번째 요소의 주소에 대한 포인터로 동작한다는 것을 알고 있습니다. 그렇다면 왜 배열에서 main에있는 내 배열 변수의 주소가 변경되지 않습니까?

#include <iostream> 

using namespace std; 

void passArrayByReference(int * array) { //passing array as.  pointer should let us modify it's address, correct? 
    cout << "Address of array in function is: " << array << endl; 
    int * localArray = new int [2]; 
    //put some dummy values in our localArray 
    localArray[0] = 9; 
    localArray[1] = 9; 
    array = localArray; 
    cout << "Address of array in function is now: " << array <<  endl; 
} 

int main() 
{ 
    int * array = new int [2]; 
    int totalElements = 2; 
    //put some initial values into our dynamic 1D array 
    array[0] = 0; 
    array[1] = 1; 
    //print our initial values 
    for(int i = 0; i < totalElements; i++) 
     cout << array[i] << endl; 
    cout << "Address of array in main: " << array << endl; 
    passArrayByReference(array); 
    cout << "Address of array in main: " << array << endl; 

    return 0; 
} 
+0

배열에 할당 할 수 없습니다. 그러나 * 포인터 *에 할당하고 있으므로 가능합니다. 그러나 배열과 포인터를 혼동하지 마십시오. – juanchopanza

+0

지금 메모리 누수가 있습니다. 새로운 주소를 할당하기 전에 이전 배열을'delete []'해야하고, 더 이상 필요하지 않을 때 새 주소를'delete [] '해야합니다. – woytaz

+0

배열을 참조로 전달하지 않았으므로 값을 전달했습니다. –

답변

2

는 당신이 바로 그 궤도에,하지만 당신은 당신의 함수 헤더의 '&'기호를 포함해야합니다. '&'기호는 값이 아닌 참조로 인수를 전달하는 데 사용됩니다.

이 경우 주소를 배열의 첫 번째 요소에 참조로 전달합니다. 즉, 함수에서 해당 주소를 수정할 수 있으며 변경 사항이 주 기능에 반영됩니다.

#include <iostream> 

using namespace std; 

void passArrayByReference(int * &array) { 
    cout << "Address of array in function is: " << array << endl; 
    int * localArray = new int [2]; 
    //put some dummy values in our localArray 
    localArray[0] = 9; 
    localArray[1] = 9; 
    array = localArray; 
    cout << "Address of array in function is now: " << array << endl; 
} 

int main() 
{ 
    int * array = new int [2]; 
    int totalElements = 2; 
    //put some initial values into our dynamic 1D array 
    array[0] = 0; 
    array[1] = 1; 
    //print our initial values 
    for(int i = 0; i < totalElements; i++) 
     cout << array[i] << endl; 
    cout << "Address of array in main is: " << array << endl; 
    passArrayByReference(array); 
    cout << "Address of array in main is now: " << array << endl; 

    //now print the values of our 'new' array 
    cout << "The values of array are now:" << endl; 
    for(int i = 0; i < totalElements; i++) 
     cout << array[i] << endl; 

    return 0; 
} 
+0

당신은 또한 메모리 누수를 삭제해야합니다 ... – 4pie0

+0

굉장 고마워요! –

+1

@GregKartrin이 메모리 누수를 처리하면이 순간에 메모리가 누수됩니다. – 4pie0

1

포인터도 변수입니다. 그 이유는 arraypassArrayByReference에 대한 참조로 전달해야하므로 사본을 수정하는 것이 아닙니다. 먼저

void passArrayByReference(int *&array) 
1

, 당신은 여기에 지속적인 변화를 만들기 위해 포인터 또는 참조 포인터를 전달해야한다 - 그 함수 본문에서 원래 포인터와 그뿐만 아니라 사본 변경할 수 있습니다 :

void passArrayByReference(int *&array) { 
    //... 
    array = new_address; 
    std::cout << "Address of array in function is now: " << array << std::endl; 
} 
// and here it is the same 

을 그리고 두 번째로 유효한 주소 new_address을 할당하고 array이 함수를 입력하기 직전에 참조한 메모리 인 을 처리하여 메모리 누수가 발생하지 않도록하십시오 ().

관련 문제