C++ 内存泄漏C+;的指针故障+;
我试图初始化一个数组,然后将内存重新分配到相同的地址。这是我的密码:C++ 内存泄漏C+;的指针故障+;,c++,C++,我试图初始化一个数组,然后将内存重新分配到相同的地址。这是我的密码: //**begin #include files************ #include <iostream> // provides access to cin and cout //--end of #include files----------- //---------------------------------- using namespace std; //--------------
//**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-------------
//----------------------------------
//**开始#包括文件************
#include//提供对cin和cout的访问
//--结束#包含文件-----------
//----------------------------------
使用名称空间std;
//----------------------------------
//**开始全局常量**********
常数int arraySize=5;
//--全局常数结束---------
//----------------------------------
//**开始主程序**************
int main()
{
CUT< P>删除一个必须做的数组:<代码> Dele[[n]数组;> P> >没有强> > ++< /强>一种在C++中实际重新分配向量的方法。所以,你所做的是:
分配一个更大的新向量-将指针存储在aux
复制aux内容中的旧nArray内容
delete[]
nArray矢量
将aux指定给nArray向量
像
/1,
int*aux=newint[arraySize+2];
// 2.
//这里故意简单
对于(size_t i=0;我使用std::vector
fnote++有关“Is Not”的某些值,请参阅链接的,您可以将同一位置与“”重复使用
你的例子可以是:
//**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-------------
//----------------------------------
//**开始#包括文件************
#include//提供对cin和cout的访问
//--结束#包含文件-----------
//----------------------------------
使用名称空间std;
//----------------------------------
//**开始全局常量**********
常数int arraySize=5;
//--全局常数结束---------
//----------------------------------
//**开始主程序**************
int main()
{
int reserved[arraySize+2];//预先分配足够的内存
cout当您使用动态内存分配时,您不控制所分配内存的地址。这是由内存分配器的实现控制的。为什么您会在意呢?通常没有办法也没有理由这样做。您无法控制new
返回的内容。这是家庭作业吗?您确定是吗我们不应该只添加几个delete
语句?这是家庭作业@johnnympp有注释掉的行,但我不知道该怎么做如果这是家庭作业,那么你就误解了作业是什么。也许你被要求使用相同的指针变量(与重新分配的动态内存不同,它将保持在相同的地址)。代码的外观并不重要。不能将数组大小调整为更大的大小,也不能保证获得相同的地址—简单明了。实现这种效果的唯一方法是编写一个保留内存用作“能力",但在逻辑上保留可使用的实际项目数,类似于std::vector
。但即使如此,std::vector
也不能保证在调整其大小大于容量时使用相同的指针。当我将其包含在注释行中时,出现了一个异常,该行似乎正常工作,我现在有3个位置!因此,ge其他的位置匹配,我不太确定如何做。这是我的第一个C++类“我现在有3个位置!”你指的是“位置”。?抱歉,在图片中,我需要8个内存地址才能相同。我在中输入了您的代码,现在8个内存地址中有5个。重新初始化后,我得到了不同的地址。我编辑了我的代码,以显示我将您的代码@AdrianColomitchiNope放置在何处,这是无法完成的。我们不保证,无论发生什么情况,在重新定位后,您将获得同一个地址。我的教授让他们都是一样的,所以我不知道他做了什么。谢谢你帮我靠近。
// 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;
//**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-------------
//----------------------------------