2015-11-01 138 views
1

我没有得到任何错误或类似的东西,但问题是,无论我做什么,我都无法摆脱那些内存泄漏。我发现的唯一解决方案是帮助我在每种方法之后添加delete[] vector1,但我不允许修改代码的这一部分。 在代码中,您可以看到评论和我可以添加新说明的区域。有没有解决我的问题?在管理内存泄漏问题C++

这是代码:

#include<iostream> 
using namespace std; 

/*YOU CAN'T ADD NEW METHODS*/ 
/*YOU CAN ONLY MODIFY THE BODY OF THE METHODS*/ 

//read array from the console - number of elements and the elements 
int * readVectorVersion1(int * noElements) { 
    int *vector1; 
    vector1 = new int[*noElements + 1]; 
    for (int i = 0; i < *noElements; i++) 
    { 
     cout << endl << "Vector1[" << i + 1 << "]="; 
     cin >> vector1[i]; 

    } 
    return vector1; 
} 
//read array from the console - number of elements and the elements 
void readVectorVersion2(int ** vector, int* noElements) { 
    *vector = new int[*noElements + 1]; 
    for (int i = 0; i < *noElements; i++) 
    { 
     cout << endl << "Vector1[" << i + 1 << "]="; 
     cin >> (*vector)[i]; 
    } 
} 
//read array from the console - number of elements and the elements 
void readVectorVersion3(int *& vector, int& noElements) { 
    vector = new int[noElements + 1]; 
    for (int i = 0; i < noElements; i++) 
    { 
     cout << endl << "Vector1[" << i + 1 << "]="; 
     cin >> vector[i]; 
    } 
} 

//read array from the console - number of elements and the elements 
int * readVectorVersion4(int& noElements) { 
    int *vector1; 
    vector1 = new int[noElements + 1]; 
    for (int i = 0; i < noElements; i++) 
    { 
     cout << endl << "Vector1[" << i + 1 << "]="; 
     cin >> vector1[i]; 
    } 
    return vector1; 
} 

//read static array from the console - number of elements and the elements 
void readStaticVector(int vector[], int * noElements) { 
    for (int i = 0; i < *noElements; i++) 
    { 
     cout << endl << "Vector1[" << i + 1 << "]="; 
     cin >> vector[i]; 
    } 
} 

//print the elements of the array 
void afisareVector(int* vector, int noElements) { 
    cout << endl << "Vector:" << endl; 
    for (int i = 0; i < noElements; i++) 
     cout << vector[i] << " "; 

} 


//read a name from the console 
char* citesteNume() { 
    char temp[200]; 
    char * nume; 
    cout << endl << "Your name:"; 
    cin >> temp; 
    nume = new char[strlen(temp) + 1]; 
    strcpy(nume, temp); 

    return nume; 
} 

//read a name from the console 
void citesteNume(char* nume) { 


    cout << endl << "Your name:"; 
    cin >> nume; 


} 
//METHODS THAT ADDS AN ELEMENT (THAT IS GIVEN) TO AN EXISTING ARRAY 
//FIRST 
void adaugaElementNou(int** vector, int* noElemente, int elementNou) { 
    (*vector) = new int[*noElemente + 2]; 
    for (int i = 0; i < *noElemente; i++) 
     (*vector)[i] = i; 

    (*vector)[*noElemente] = elementNou; 

} 
//SECOND 
int * adaugaElementNou(int& noElemente, int elementNou) { 
    int *vector; 
    vector = new int[noElemente + 2]; 
    for (int i = 0; i < noElemente; i++) 
     vector[i] = i; 
    vector[noElemente] = elementNou; 
    return vector; 

} 
//THIRD 
int * adaugaElementNou(int* noElemente, int elementNou) { 
    int *vector; 
    vector = new int[(*noElemente) + 2]; 
    for (int i = 0; i < *noElemente; i++) 
     vector[i] = i; 
    vector[*noElemente] = elementNou; 
    return vector; 

} 



//THE PROGRAM MUST RUN AND NOT GENERATE ANY ERRORS OR MEMORY-LEAKS 
void main() { 
    //YOU CAN'T ADD NEW VARIABLES 

    int * vector1; 
    int vector2[50]; 
    int nrElementeVector1=3; 
    int nrElementeVector2=3; 

    //YOU CAN ADD NEW INSTRUCTIONS 
    // ... 
    vector1 = new int[nrElementeVector1 + 1]; 
    for (int i = 0; i < nrElementeVector1; i++) 
     vector1[i] = i; 
    for (int i = 0; i < nrElementeVector2; i++) 
     vector2[i] = i; 
    //YOU CAN'T MODIFY THE FOLLOWING CODE 

    afisareVector(vector1, nrElementeVector1); 
    afisareVector(vector2, nrElementeVector2); 
    //delete[]vector1; /*This instruction is added by me but i`m not allowed to modify this area of the code*/ 
    vector1 = readVectorVersion1(&nrElementeVector1); 
    afisareVector(vector1, nrElementeVector1); 
    //delete[]vector1; 
    readVectorVersion2(&vector1, &nrElementeVector1); 
    afisareVector(vector1, nrElementeVector1); 
    //delete[]vector1; 
    readVectorVersion3(vector1, nrElementeVector1); 
    afisareVector(vector1, nrElementeVector1); 
    //delete[]vector1; 
    vector1 = readVectorVersion4(nrElementeVector1); 
    afisareVector(vector1, nrElementeVector1); 
    //delete[]vector1; 
    readStaticVector(vector2, &nrElementeVector2); 
    afisareVector(vector2, nrElementeVector2); 

    char* string1; 
    char string2[50]; 

    string1 = citesteNume(); 
    cout << endl << "Hello " << string1; 
    //delete[]string1; /*THIS IS NOT ALLOWED HERE*/ 
    citesteNume(string2); 
    cout << endl << "Hello " << string2; 

    vector1 = adaugaElementNou(nrElementeVector1, 99); 
    afisareVector(vector1, nrElementeVector1+1); 
    //delete[]vector1; 
    adaugaElementNou(&vector1, &nrElementeVector1, 55); 
    afisareVector(vector1, nrElementeVector1+1); 
    //delete[]vector1; 
    vector1 = adaugaElementNou(&nrElementeVector1, 77); 
    afisareVector(vector1, nrElementeVector1+1); 
    //delete[]vector1; 

    //YOU CAN ADD NEW INSTRUCTIONS HERE 
    // ... 

    delete[] vector1; //I`ve tried to use delete here because I didn`t knew what else i should do, but I know that it makes no sense(and it`s not working); 
    delete[] string1; 

    //THE FOLLOWING CODE CHECKS IF THERE ARE ANY MEMORYLEAKS 
    _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE); 
    _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDOUT); 
    _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE); 
    _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDOUT); 
    _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE); 
    _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDOUT); 
    _CrtDumpMemoryLeaks(); 

    //YOU CAN'T MODIFY THE FOLLOWING CODE 
    vector1 = NULL; 
    string1 = NULL; 
    cout << endl << "In this moment there are no memory-leaks!"; 
} 
+0

此任务是否允许修改程序的行为(预期输出)以摆脱内存泄漏? –

+2

这个家庭作业显然是由大约20年前学过C++的人创建的,他从来没有打算更新他的知识。 –

+0

我想我知道该怎么做'delete [] vector1'。我会尝试和我的黑客告诉你,如果我成功了。 –

回答

0

对于字符串1这很简单,你可以:

delete[] string1; 

这是我认为你应该做处理向量1:

void afisareVector(int* vector, int noElements) { 
    cout << endl << "Vector:" << endl; 
    for (int i = 0; i < noElements; i++) 
     cout << vector[i] << " "; 

    // A very dirty hack, PLEASE **NEVER** USE IT. 
    static int callIndex = 0; 
    if(callIndex != 1 && callIndex != 6) 
     delete[] vector; 
    ++callIndex; 
} 

的valgrind摘要:

==20937== HEAP SUMMARY: 
==20937==  in use at exit: 0 bytes in 0 blocks 
==20937== total heap usage: 9 allocs, 9 frees, 144 bytes allocated 
==20937== 
==20937== All heap blocks were freed -- no leaks are possible 
==20937== 
==20937== For counts of detected and suppressed errors, rerun with: -v 
==20937== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0) 

编辑:我改变了主意。上面的例子是你可以做的,这是你应该做的:
注意#1:这种黑客感觉对我来说更脏。
注意#2:为了编译它,我必须删除所有微软特定的东西,并用valgrind执行泄漏检查。

#include<iostream> 
#include<cstring> 
using namespace std; 

/*YOU CAN'T ADD NEW METHODS*/ 
/*YOU CAN ONLY MODIFY THE BODY OF THE METHODS*/ 

//read array from the console - number of elements and the elements 
int * readVectorVersion1(int * noElements) { 
    static int vector1[50]; 
    for (int i = 0; i < *noElements; i++) 
    { 
     cout << endl << "Vector1[" << i + 1 << "]="; 
     cin >> vector1[i]; 

    } 
    return vector1; 
} 
//read array from the console - number of elements and the elements 
void readVectorVersion2(int ** vector1, int* noElements) { 
    static int vector [50]; 
    for (int i = 0; i < *noElements; i++) 
    { 
     cout << endl << "Vector1[" << i + 1 << "]="; 
     cin >> vector[i]; 
    } 
    *vector1 = vector; 
} 
//read array from the console - number of elements and the elements 
void readVectorVersion3(int *& vector1, int& noElements) { 
    static int vector [50]; 
    for (int i = 0; i < noElements; i++) 
    { 
     cout << endl << "Vector1[" << i + 1 << "]="; 
     cin >> vector[i]; 
    } 
    vector1 = vector; 
} 

//read array from the console - number of elements and the elements 
int * readVectorVersion4(int& noElements) { 
    static int vector1 [50]; 
    for (int i = 0; i < noElements; i++) 
    { 
     cout << endl << "Vector1[" << i + 1 << "]="; 
     cin >> vector1[i]; 
    } 
    return vector1; 
} 

//read static array from the console - number of elements and the elements 
void readStaticVector(int vector[], int * noElements) { 
    for (int i = 0; i < *noElements; i++) 
    { 
     cout << endl << "Vector1[" << i + 1 << "]="; 
     cin >> vector[i]; 
    } 
} 

//print the elements of the array 
void afisareVector(int* vector, int noElements) { 
    cout << endl << "Vector:" << endl; 
    for (int i = 0; i < noElements; i++) 
     cout << vector[i] << " "; 
} 


//read a name from the console 
char* citesteNume() { 
    char temp[200]; 
    char * nume; 
    cout << endl << "Your name:"; 
    cin >> temp; 
    nume = new char[strlen(temp) + 1]; 
    strcpy(nume, temp); 

    return nume; 
} 

//read a name from the console 
void citesteNume(char* nume) { 


    cout << endl << "Your name:"; 
    cin >> nume; 


} 
//METHODS THAT ADDS AN ELEMENT (THAT IS GIVEN) TO AN EXISTING ARRAY 
//FIRST 
void adaugaElementNou(int** vector, int* noElemente, int elementNou) { 
    static int vector1 [50]; 
    for (int i = 0; i < *noElemente; i++) 
     vector1[i] = i; 

    vector1[*noElemente] = elementNou; 
    *vector = vector1; 
} 
//SECOND 
int * adaugaElementNou(int& noElemente, int elementNou) { 
    static int vector [50]; 
    for (int i = 0; i < noElemente; i++) 
     vector[i] = i; 
    vector[noElemente] = elementNou; 
    return vector; 
} 
//THIRD 
int * adaugaElementNou(int* noElemente, int elementNou) { 
    static int vector [50]; 
    for (int i = 0; i < *noElemente; i++) 
     vector[i] = i; 
    vector[*noElemente] = elementNou; 
    return vector; 
} 



//THE PROGRAM MUST RUN AND NOT GENERATE ANY ERRORS OR MEMORY-LEAKS 
int main() { 
    //YOU CAN'T ADD NEW VARIABLES 

    int * vector1; 
    int vector2[50]; 
    int nrElementeVector1=3; 
    int nrElementeVector2=3; 

    //YOU CAN ADD NEW INSTRUCTIONS 
    for (int i = 0; i < nrElementeVector2; i++) 
     vector2[i] = i; 
    vector1 = vector2; 
    //YOU CAN'T MODIFY THE FOLLOWING CODE 

    afisareVector(vector1, nrElementeVector1); 
    afisareVector(vector2, nrElementeVector2); 
    vector1 = readVectorVersion1(&nrElementeVector1); 
    afisareVector(vector1, nrElementeVector1); 
    readVectorVersion2(&vector1, &nrElementeVector1); 
    afisareVector(vector1, nrElementeVector1); 
    readVectorVersion3(vector1, nrElementeVector1); 
    afisareVector(vector1, nrElementeVector1); 
    vector1 = readVectorVersion4(nrElementeVector1); 
    afisareVector(vector1, nrElementeVector1); 
    readStaticVector(vector2, &nrElementeVector2); 
    afisareVector(vector2, nrElementeVector2); 

    char* string1; 
    char string2[50]; 

    string1 = citesteNume(); 
    cout << endl << "Hello " << string1; 
    citesteNume(string2); 
    cout << endl << "Hello " << string2; 

    vector1 = adaugaElementNou(nrElementeVector1, 99); 
    afisareVector(vector1, nrElementeVector1+1); 
    adaugaElementNou(&vector1, &nrElementeVector1, 55); 
    afisareVector(vector1, nrElementeVector1+1); 
    vector1 = adaugaElementNou(&nrElementeVector1, 77); 
    afisareVector(vector1, nrElementeVector1+1); 

    //YOU CAN ADD NEW INSTRUCTIONS HERE 
    // ... 

    delete[] string1; 

    //YOU CAN'T MODIFY THE FOLLOWING CODE 
    vector1 = NULL; 
    string1 = NULL; 
} 

Valgrind的输出:

==21224== HEAP SUMMARY: 
==21224==  in use at exit: 0 bytes in 0 blocks 
==21224== total heap usage: 1 allocs, 1 frees, 2 bytes allocated 
==21224== 
==21224== All heap blocks were freed -- no leaks are possible 
==21224== 
==21224== For counts of detected and suppressed errors, rerun with: -v 
==21224== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0) 

编辑:我想出了第三个(更脏)的解决方案,但我太累了这一切的黑客编写完整的版本。下面是一个示例:

int * readVectorVersion1(int * noElements) { 
    static int *vector1 = NULL; 
    delete[] vector1; 
    if (noElements < 0) 
     return NULL; 
    vector1 = new int[*noElements + 1]; 
    for (int i = 0; i < *noElements; i++) 
    { 
     cout << endl << "Vector1[" << i + 1 << "]="; 
     cin >> vector1[i]; 

    } 
    return vector1; 
} 

// ... 

int main() { 
    // ... 
    // Just before _CrtSetReportMode 
    nrElementeVector1 = -1; 
    readVectorVersion1(*nrElementeVector1); 
    // .. 
} 
+0

if语句检查什么?你能解释给我吗?你用什么来显示内存泄漏? –

+0

@AlexChihaia你第二次打电话给afisareVector,第七次打电话给afisareVector时,它不能释放记忆,在所有其他地方都不能释放记忆。我只是添加了一个静态计数器来检测是否调用delete []。附:我无法找到解释静态工作原理的良好链接。 –

+0

@AlexChihaia如果你想解释一下'static'是如何工作的,我认为[这个链接](http://www.linuxtopia.org/online_books/programming_books/thinking_in_c++/Chapter10_002.html)会给你一些线索。另请注意:我改变了自己的想法(阅读更新后的答案)。 –

0

我说这个任务是不可能完成的任务。该行

vector1 = readVectorVersion1(&nrElementeVector1); 

在vector1持有动态分配的资源时执行。在这一点上创建一个不可恢复的泄漏。这同样适用于以下

readVectorVersion2(&vector1, &nrElementeVector1); 
0

vector1指针被覆盖的,你可能不修改代码,所以显然这可能不是唯一的指向动态分配的内存,否则会泄漏并没有什么你可以在你的任务限制内做到这一点。

因此,让我们探讨使用静态对象来保存指针的可能性。为简单起见,我将使用std::vector,但如果需要,可以使用静态指针指向动态内存。在这种情况下,您需要手动管理内存。

int * readVectorVersion1(int * noElements) { 
    static std::vector<int> vector1; 
    vector1.resize(*noElements); 

    // initialize the way you want to 

    return vector1.data(); 
    // return &vector1[0]; // use this if you use older version of c++ than c++11 
} 

然后你去了。内存将在静态对象被销毁时解除分配。这当然会限制你可以用这个函数做什么。您不能再分配两个单独的数组,因为随后的调用将使用相同的向量。但是这个任务不需要,所以我想这是你的老师所要求的。

+0

好的。我写了这段代码,并且仍然有内存泄漏(至少这是输出)。当我使用“删除[] vector1”在主程序运行,但比我得到一个错误。我做错了什么? 这是代码: –

+0

'#include #include using namespace std; int * readVectorVersion1(int * noElements){ \t \t static vector vector1; \t vector1.resize(* noElements); \t unsigned int i; \t //初始化要 \t的方式(I = 0; I > vector1 [i]; } \t return&vector1 [0]; } void afisareVector(int * vector,int noElements){ \t cout << endl <<“Vector:”<< endl; \t for(int i = 0; i

+0

'void main(){ \t int * vector1; \t int nrElem = 4; \t vector1 = readVectorVersion1(&nrElem); \t afisareVector(vector1,nrElem); \t //删除[] vector1; \t _CrtSetReportMode(_CRT_WARN,_CRTDBG_MODE_FILE); \t _CrtSetReportFile(_CRT_WARN,_CRTDBG_FILE_STDOUT); \t _CrtSetReportMode(_CRT_ERROR,_CRTDBG_MODE_FILE); \t _CrtSetReportFile(_CRT_ERROR,_CRTDBG_FILE_STDOUT); \t _CrtSetReportMode(_CRT_ASSERT,_CRTDBG_MODE_FILE); \t _CrtSetReportFile(_CRT_ASSERT,_CRTDBG_FILE_STDOUT); \t _CrtDumpMemoryLeaks(); }' –