2016-09-12 1 views
-1

Ich versuche, ein Array zu initialisieren und dann den Speicher auf die gleiche Adresse neu zuweisen. Hier ist mein Code:Pointer Problem mit Speicherverlust C++

//**begin #include files************ 
#include <iostream> // provides access to cin and cout 

//--end of #include files----------- 
//---------------------------------- 

using namespace std; 
//---------------------------------- 

//**begin global constants********** 
const int arraySize = 5; 
//--end of global constants--------- 
//---------------------------------- 


//**begin main program************** 
int main() 
{ 
    cout << endl << endl; 
    int* nArray = new int[arraySize]; 
    cout << " --->After creating and allocating memory for nArray." << endl; 
    cout << " nArray address is <" << nArray << "> and contains the value " << hex << *nArray << dec << endl; 
    for (int i = 0; i < arraySize; i++) 
    { 
     nArray[i] = i*i; 
    } 
    cout << " --->After initializing nArray." << endl; 
    cout << " nArray address is <" << nArray << "> and contains the value " << hex << *nArray << dec << endl << endl; 
    for (int i = 0; i < arraySize; i++) 
    { 
     cout << " nArray[" << i << "] = " << nArray[i] << " at address <" << nArray + i << ">" << endl; 
    } 
    // You'll need a command here to fix the memory leak 
    cout << endl << " --->Before reallocating memory for nArray." << endl; 
    cout << " nArray address is <" << nArray << "> and contains the value " << hex << *nArray << endl; 
    int* aux = new int[arraySize + 2]; 
    cout << dec << " --->After reallocating memory for nArray." << endl; 
    cout << " nArray address is <" << nArray << "> and contains the value " << hex << *nArray << dec << endl; 
    for (int i = 0; i<arraySize; i++) { 
     aux[i] = nArray[i]; 
    } 
    delete[] nArray; 
    nArray = aux; 
    cout << endl << " --->After reinitializing nArray." << endl; 
    cout << " nArray address is <" << nArray << "> and contains the value " << hex << *nArray << dec << endl << endl; 
    for (int i = 0; i < arraySize + 2; i++) 
    { 
     cout << " nArray[" << i << "] = " << nArray[i] << " at address <" << nArray + i << ">" << endl; 
    } 
    // . . . and also here. 
    cout << endl << " --->Getting ready to close down the program." << endl; 
    cout << " nArray address is <" << nArray << "> and contains the value " << hex << *nArray << dec << endl; 
    // Wait for user input to close program when debugging. 
    cin.get(); 
    return 0; 
} 
//--end of main program------------- 
//---------------------------------- 

Ich habe versucht mit delete nArray; dann wieder initialisieren, aber es funktioniert nicht. Nicht sicher, was zu tun ist, aber ich habe nach ein paar Stunden gesucht und jede Hilfe würde geschätzt werden!

Die kommentierten Zeilen zeigen, wo Anweisungen hinzugefügt werden sollten.

Dies ist das Ergebnis, das ich brauche:

enter image description here

const int arraySize = 5; 


int main() 
{ 
    int* nArray = new int[arraySize]; 
    for (int i = 0; i < arraySize; i++) 
     nArray[i] = i*i; 

    int* aux = new int[arraySize + 2]; 
    for (int i = 0; i<arraySize; i++) 
     aux[i] = nArray[i]; 

    delete[] nArray; 
    nArray = aux; 
} 
+0

Wenn Sie dynamische Speicherzuweisung verwenden, steuern Sie nicht die Adresse des zugewiesenen Erinnerung. Dies steht unter der Kontrolle der Implementierung des Speicherzuordners. Warum würde es dich interessieren? Es gibt normalerweise keinen Grund und keinen Grund, dies zu tun. –

+0

Sie haben nicht viel Kontrolle darüber, was von 'new' zurückgegeben wird. Sind das Hausaufgaben? Sind Sie sicher, dass Sie nicht nur ein paar 'delete' Anweisungen hinzufügen sollen? –

+0

Es ist die Hausaufgabe @JohnnyMopp Es gibt auskommentierte Zeilen, aber ich kann nicht herausfinden, was ich dort tun soll – timmyspan

Antwort

0

So löschen Sie ein Array, das Sie tun müssen: delete[] nArray;

+0

Ich bekam eine Ausnahme, als ich das in die kommentierten Zeilen – timmyspan

1

ist es nicht ++ ein Weg zu tatsächlich neu zuordnen einen Vektor in C++. Also, was Sie tun, ist:

  1. einen neuen Vektor mit einer höheren Größe zuweisen - speichern Sie den Zeiger in aux
  2. kopieren Sie die alten nArray Inhalt in den Inhalt von aux
  3. delete[] die nArray Vektor
  4. assign aux zum nArray Vektor

wie

// 1, 
int* aux=new int[arraySize+2]; 
// 2. 
// deliberately simple here 
for(size_t i=0; i<arraySize; i++) { 
    aux[i]=nArray[i]; 
} 
// 3. 
delete[] nArray; // the entire memory occupied bu nArray is recycled 
// 4. 
nArray=aux; // make nArray point to the newly allocated chunck 

// supplementary, adjust the arraySize, as it is now larger by 2 
// scratch that, the rest of the code works afterwards using arraySize + 2 
// arraySize += 2; 

Oder verwenden Sie als Stroustrup recommends den std :: vector.


fnote ++ für einige Werte von "nicht" - siehe die

+0

einfügte, das zu funktionieren schien und ich habe 3 Standorte gerade jetzt! Um die anderen Standorte in Übereinstimmung zu bringen, bin ich mir nicht sicher, wie das geht. Dies ist meine erste C++ Klasse – timmyspan

+0

"Ich habe momentan 3 Standorte!" Was meinst du mit "Orte"? –

+0

Entschuldigung, auf dem Bild brauche ich 8 Speicheradressen, um gleich zu sein. Ich habe Ihren Code eingegeben und jetzt habe ich 5 von 8. Nach der Reinitialisierung bekomme ich verschiedene Adressen. Ich habe meinen Code bearbeitet, um zu zeigen, wo ich Ihren Code @AdrianColomitchi – timmyspan

1

verknüpft Sie können die gleichen Ort wiederverwenden mit "placement new"

Ein Beispiel auf Ihrem Beispiel könnte sein:

//**begin #include files************ 
#include <iostream> // provides access to cin and cout 

//--end of #include files----------- 
//---------------------------------- 

using namespace std; 
//---------------------------------- 

//**begin global constants********** 
const int arraySize = 5; 
//--end of global constants--------- 
//---------------------------------- 


//**begin main program************** 
int main() 
{ 
    int reserved[arraySize+2]; // pre allocated enough memory 

    cout << endl << endl; 
    int* nArray = new (reserved) int[arraySize]; // placement new place nArray at "reserved" location 
    cout << " --->After creating and allocating memory for nArray." << endl; 
    cout << " nArray address is <" << nArray << "> and contains the value " << hex << *nArray << dec << endl; 
    for (int i = 0; i < arraySize; i++) 
    { 
     nArray[i] = i*i; 
    } 
    cout << " --->After initializing nArray." << endl; 
    cout << " nArray address is <" << nArray << "> and contains the value " << hex << *nArray << dec << endl << endl; 
    for (int i = 0; i < arraySize; i++) 
    { 
     cout << " nArray[" << i << "] = " << nArray[i] << " at address <" << nArray + i << ">" << endl; 
    } 
    // You'll need a command here to fix the memory leak 
    // Well, not now. 
    cout << endl << " --->Before reallocating memory for nArray." << endl; 
    cout << " nArray address is <" << nArray << "> and contains the value " << hex << *nArray << endl; 
    int* aux = new (reserved) int[arraySize + 2]; // and again, placement new place aux at "reserved" location 
    cout << dec << " --->After reallocating memory for nArray." << endl; 
    cout << " nArray address is <" << nArray << "> and contains the value " << hex << *nArray << dec << endl; 
    for (int i = 0; i<arraySize; i++) { 
     aux[i] = nArray[i]; 
    } 
    //delete[] nArray; // you can't do this now 
    nArray = aux; 
    cout << endl << " --->After reinitializing nArray." << endl; 
    cout << " nArray address is <" << nArray << "> and contains the value " << hex << *nArray << dec << endl << endl; 
    for (int i = 0; i < arraySize + 2; i++) 
    { 
     cout << " nArray[" << i << "] = " << nArray[i] << " at address <" << nArray + i << ">" << endl; 
    } 
    // . . . and also here. 
    cout << endl << " --->Getting ready to close down the program." << endl; 
    cout << " nArray address is <" << nArray << "> and contains the value " << hex << *nArray << dec << endl; 
    // Wait for user input to close program when debugging. 
    cin.get(); 
    return 0; 
} 
//--end of main program------------- 
//----------------------------------