【发布时间】:2018-03-11 01:39:15
【问题描述】:
我还是 C++ 新手,所以回答时请善待。 当谈到动态内存管理时,许多教程都给出了下面的示例或类似内容,它们通常在同一范围内。
MyClass * pt;
pt = new MyClass[3];
delete[] pt;
如果我失去了对原始动态分配变量的访问权限但只有它的地址,我有一个问题。考虑以下
int* intP; //Global variable
void SomeFunction()
{
int* intP2 = new int;
*intP2 = 10;
intP = intP2;
//Some other actions.....and lost access to intP2 when this function ends
}
void SomeOtherFunction()
{
delete intP; //Valid?
}
【问题讨论】:
-
一个是intP,另一个是intP2....
标签: c++ pointers dynamic-memory-allocation