小编pla*_*fer的帖子

在C++中为释放的内存分配值

我正在使用http://www.learncpp.com上的教程学习c ++ .在使用new和delete(http://www.learncpp.com/cpp-tutorial/69-dynamic-memory-allocation-with-new-and-delete/)的动态内存分配课程中,它指出:

类似地,当删除动态分配的变量时,指向它的指针不为零.请考虑以下代码段:

int *pnValue = new int;
delete pnValue; // pnValue not set to 0

if (pnValue)
    *pnValue = 5; // will cause a crash
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试它(编译器:GNU GCC编译器,ubuntu)时,我的程序不会崩溃.

int *pnValue = new int;
delete pnValue; // pnValue not set to 0
if (pnValue)
    *pnValue = 5; // will cause a crash -> doesn't
std::cout << "Did not crash" << std::endl;
Run Code Online (Sandbox Code Playgroud)

这里发生了什么?在C++中是否有任何形式的运行时检查?

c++ dynamic-memory-allocation

3
推荐指数
1
解决办法
160
查看次数

Python 3 ==运算符

我对==操作符在Python 3中的工作方式感到困惑.从文档中,eq(a, b)相当于a == b.也eq__eq__是等价的.

请看以下示例:

class Potato:
    def __eq__(self, other):
        print("In Potato's __eq__")
        return True

>> p = Potato()
>> p == "hello"
In Potato's __eq__ # As expected, p.__eq__("hello") is called
True
>> "hello" == p
In Potato's __eq__ # Hmm, I expected this to be false because
True               # this should call "hello".__eq__(p)
>> "hello".__eq__(p)
NotImplemented     # Not implemented? How does == work for strings …
Run Code Online (Sandbox Code Playgroud)

python equality python-3.x

2
推荐指数
1
解决办法
1414
查看次数

C++:特殊的typedef

根据我的理解,typedef语法是:

typedef existing_type new_type_name;
Run Code Online (Sandbox Code Playgroud)

但是,在Chrome的v8命名空间中,有许多typedef似乎与其他语法一起使用.例如,

typedef void(*  FunctionCallback )(const FunctionCallbackInfo< Value > &info)
Run Code Online (Sandbox Code Playgroud)

有两点我不明白:
1- FunctionCallbackInfo <Value>是在命名空间中定义的类,而FunctionCallback不是; 应该是先存在的现有类型?
2-没有空格分隔existing_typenew_type_name

读取它的正确方法是什么?

编辑:我正在学习本教程中的所有typedef信息.

c++ typedef v8

-4
推荐指数
1
解决办法
203
查看次数