C++ 管理内存泄漏的问题C++;
我没有得到任何错误或类似的东西,但问题是,无论我做什么,我都无法摆脱这些内存泄漏。我发现唯一有帮助的解决方案是在每个方法之后添加C++ 管理内存泄漏的问题C++;,c++,memory-leaks,C++,Memory Leaks,我没有得到任何错误或类似的东西,但问题是,无论我做什么,我都无法摆脱这些内存泄漏。我发现唯一有帮助的解决方案是在每个方法之后添加delete[]vector1,但不允许修改代码的这一部分。 在代码中,您可以看到注释和我可以添加新指令的区域。我的问题有什么解决办法吗 代码如下: #include<iostream> using namespace std; /*YOU CAN'T ADD NEW METHODS*/ /*YOU CAN ONLY MODIFY THE BODY OF
delete[]vector1
,但不允许修改代码的这一部分。
在代码中,您可以看到注释和我可以添加新指令的区域。我的问题有什么解决办法吗
代码如下:
#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!";
}
#包括
使用名称空间std;
/*不能添加新方法*/
/*您只能修改方法的主体*/
//从控制台读取数组-元素数和元素数
int*readVectorVersion1(int*noElements){
int*vector1;
向量1=新整数[*noElements+1];
对于(int i=0;i<*noElements;i++)
{
我认为这项任务是不可能完成的
vector1 = readVectorVersion1(&nrElementeVector1);
在vector1持有动态分配的资源时执行。此时将创建一个不可恢复的泄漏。以下情况也是如此
readVectorVersion2(&vector1, &nrElementeVector1);
我认为这项任务是不可能完成的
vector1 = readVectorVersion1(&nrElementeVector1);
在vector1持有动态分配的资源时执行。此时将创建一个不可恢复的泄漏。以下情况也是如此
readVectorVersion2(&vector1, &nrElementeVector1);
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
}
int*readVectorVersion1(int*noElements){
静态std::向量向量1;
向量1.调整大小(*无元素);
//按您想要的方式初始化
返回向量1.data();
//返回/VoCord1(0);/使用该版本的C++比C++ 11更旧
}
这样就可以了。当静态对象被销毁时,内存将被释放。这当然限制了您对函数的使用。您不能再分配两个单独的数组,因为后续调用将使用相同的向量。但此分配不需要它,所以我想这是您的讲师要求的。Tvector1
指针在代码中被覆盖,您不能修改,因此很明显,它可能不是指向动态分配内存的唯一指针,否则它将泄漏,并且在您的分配限制范围内,您无法阻止它
因此,让我们探讨一下使用静态对象来保存指针的可能性。为了简单起见,我将使用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
}
int*readVectorVersion1(int*noElements){
静态std::向量向量1;
向量1.调整大小(*无元素);
//按您想要的方式初始化
返回向量1.data();
//返回/VoCord1(0);/使用该版本的C++比C++ 11更旧
}
就这样。当静态对象被销毁时,内存将被释放。这当然限制了您对函数的使用。您不能再分配两个单独的数组,因为后续调用将使用相同的向量。但此分配不需要它,所以我想这是您的指导老师要求的。F或者string1很简单,您可以:
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;
}
编辑:我改变了主意。上面的例子是你可以做的,这是你应该做的:注#1:我觉得这种黑客行为更加肮脏。
注#2:为了编译它,我必须删除所有特定于Microsoft的内容,并使用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;
}
编辑:我提出了第三个(甚至更脏的)解决方案,但我对所有这些黑客行为感到厌倦,无法编写完整的版本。以下是一个示例:
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);
// ..
}
int*readVectorVersion1(int*noElements){
静态int*vector1=NULL;
删除[]向量1;
如果(无元素<0)
返回NULL;
向量1=新整数[*noElements+1];
对于(int i=0;i<*noElements;i++)
{
cout对于string1,它很简单,您可以:
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;
}
编辑:我改变了主意。上面的例子是你可以做的,这是你应该做的:
注#1:我觉得这种黑客行为更加肮脏。
注#2:为了编译它,我必须删除所有特定于Microsoft的内容,并使用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;
}
编辑:我提出了第三个(甚至更脏的)解决方案,但我对所有这些黑客行为感到厌倦,无法编写完整的版本。以下是一个示例:
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);
// ..
}
int*readVectorVersion1(int*noElements){
静态int*vector1=NULL;
删除[]向量1;
如果(无元素<0)
返回NULL;
向量1=新整数[*noElements+1];
对于(int i=0;i<*noElements;i++)
{
这个任务允许修改程序的行为(预期输出)以消除内存泄漏吗?这项作业显然是由20年前学习C++的人创建的,而且从来没有费心更新知识。我想我该怎么办<代码>删除[]。vector1
。我会尝试,我的黑客会告诉你我是否成功。你是否可以在AfisaRevert
内部进行更改?最简单的选择是根本不使用new
和delete
。如果你真的需要,那么你应该等到你真的知道如何处理它们。这个任务允许修改为了避免内存泄漏,程序的行为(预期输出)?这个作业显然是由学习C++的人创建的,大约20年。