我知道如果一个指针在C中声明(并且没有初始化),它将指向一个可以包含任何内容的"随机"内存地址.
它实际上指向的地方怎么样?据推测,这不是真正的随机,因为这将是低效和不合逻辑的.
如何检查变量,特别是指针是否在C++中定义?假设我有一个班级:
class MyClass {
public:
MyClass();
~MyClass() {
delete pointer; // if defined!
}
initializePointer() {
pointer = new OtherClass();
}
private:
OtherClass* pointer;
};
Run Code Online (Sandbox Code Playgroud) 我正在创建脚本语言.当我分配东西时,它会分配东西并返回地址然后我对它做任何事情然后删除它.我无法控制变量,比如在我的lang中创建struct(使用指针和bool来检查指针是否指向有效数据)等等因为它会让我的RAM在RAM中变得更慢更大.
例如:(我的脚本语言很容易被理解.我怀疑你不会理解这一点,但无论如何我会在其中加入一些评论)
MyStruct = { //Function. For create object with it use 'new' before it.
TestAliveVar=0
}
Func = { //I'll explain what exactly this function does every place it runs.
if (!exists(arg0)) //C++: ???
exit;
arg0.TestAliveVar=1
println "Still alive!";
}
var MyVar=new MyStruct(); //Returns address of the new object in the heap
//and runs on it the `MyStruct` function.
Func(MyVar); //Sets his 'TestAliveVar' to 1
//and prints 'Still Alive!' with new line
delete(MyVar); //C++: free(MyVar);
Func(MyVar); //Does nothing
Run Code Online (Sandbox Code Playgroud)
问题是如何创建 …