我正在创建脚本语言。 当我分配的东西,它分配的东西,并返回地址 ,然后我做任何事情,然后删除它。我无法控制其中的变量 就像在我的lang中创建结构(struct用指针和bool来检查指针是否指向有效数据)等等,因为它会让我的lang在RAM中变得越来越慢。C++ - 检查指针是否指向有效内存(不能在这里使用NULL检查)
例如:(我的脚本语言是容易理解的,我怀疑你会不明白这一点,但我会放一些评论中也无妨。)
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
的问题是如何创造的功能exists
您在此代码中看到。 顺便说一句我可以在这个lang运行C++代码。
我没有理解任何事情 – log0 2012-08-03 00:26:33
很难想象如何保持“有效”标志可能比测试指针的有效性要慢。 – 2012-08-03 00:27:41
@Ugo如果我将指针发送到代码中没有struct的地方,并在某处删除它,那我该如何检查指针是否仍然有效? – MessyCode 2012-08-03 00:27:55