草庐IT

malloc_ptr_t

全部标签

c++ - 如何将 shared_ptr 与指向不应释放的结构的指针一起使用

目前我正在使用glib库中的一些函数。伴随着glib的还有gio。glib是一个C库,因此我需要删除我创建的一些结构。我为许多对象创建了一个智能指针,例如:std::shared_ptrmy_queue=std::shared_ptr(g_async_queue_create(),g_async_queue_unref);为此创建了一个指向GAsyncQueue的共享指针,这会在队列生命结束时安全地销毁队列。但是,当我从gio库中获得一个我不应该释放的指针时,我遇到了一个问题。在下面的代码中,my_connection是一个GSocketClient,它实现(在glib中)GIOStr

c++ - 如何将 shared_ptr 与指向不应释放的结构的指针一起使用

目前我正在使用glib库中的一些函数。伴随着glib的还有gio。glib是一个C库,因此我需要删除我创建的一些结构。我为许多对象创建了一个智能指针,例如:std::shared_ptrmy_queue=std::shared_ptr(g_async_queue_create(),g_async_queue_unref);为此创建了一个指向GAsyncQueue的共享指针,这会在队列生命结束时安全地销毁队列。但是,当我从gio库中获得一个我不应该释放的指针时,我遇到了一个问题。在下面的代码中,my_connection是一个GSocketClient,它实现(在glib中)GIOStr

c++ - 为什么我不能将 nullptr 转换为 weak_ptr<>

classMyClass{public:MyClass(std::weak_ptrparent){}}我想这样做:autonewInstance=std::make_shared(nullptr);或者weak_ptr参数的默认值为null,如:voidfunction(intarg,std::weak_ptrobj=nullptr);但是,我需要这样做:autonewInstance=std::make_shared(std::shared_ptr(nullptr));这是为什么呢? 最佳答案 因为weak_ptr在概念上只能从另

c++ - 为什么我不能将 nullptr 转换为 weak_ptr<>

classMyClass{public:MyClass(std::weak_ptrparent){}}我想这样做:autonewInstance=std::make_shared(nullptr);或者weak_ptr参数的默认值为null,如:voidfunction(intarg,std::weak_ptrobj=nullptr);但是,我需要这样做:autonewInstance=std::make_shared(std::shared_ptr(nullptr));这是为什么呢? 最佳答案 因为weak_ptr在概念上只能从另

c++ - clang 3.1 看不到 unique_ptr?

我刚刚开始玩clang并尝试编译以下示例程序:#include#includeintmain(){std::unique_ptru(newunsigned(10));std::cout编译时出现以下错误:$clang++helloworld.cpphelloworld.cpp:6:10:error:nomembernamed'unique_ptr'innamespace'std'std::unique_ptru(newunsigned(10));~~~~~^helloworld.cpp:6:29:error:expected'('forfunction-stylecastortypec

c++ - clang 3.1 看不到 unique_ptr?

我刚刚开始玩clang并尝试编译以下示例程序:#include#includeintmain(){std::unique_ptru(newunsigned(10));std::cout编译时出现以下错误:$clang++helloworld.cpphelloworld.cpp:6:10:error:nomembernamed'unique_ptr'innamespace'std'std::unique_ptru(newunsigned(10));~~~~~^helloworld.cpp:6:29:error:expected'('forfunction-stylecastortypec

c++ - 为什么 malloc(0) 的返回值是实现定义的?

ISO/IEC9899:TC2(即C99标准),§7.20.3规定:Ifthesizeofthespacerequestediszero,thebehaviorisimplementation-defined:eitheranullpointerisreturned,orthebehaviorisasifthesizeweresomenonzerovalue,exceptthatthereturnedpointershallnotbeusedtoaccessanobject.换句话说,malloc(0)可能返回NULL或我不能取消引用的有效指针。这种行为背后的原因是什么?仅仅定义mal

c++ - 为什么 malloc(0) 的返回值是实现定义的?

ISO/IEC9899:TC2(即C99标准),§7.20.3规定:Ifthesizeofthespacerequestediszero,thebehaviorisimplementation-defined:eitheranullpointerisreturned,orthebehaviorisasifthesizeweresomenonzerovalue,exceptthatthereturnedpointershallnotbeusedtoaccessanobject.换句话说,malloc(0)可能返回NULL或我不能取消引用的有效指针。这种行为背后的原因是什么?仅仅定义mal

c++ - 在混合 C/C++ 程序中协调 malloc 和 new 的 "correct"方法是什么?

我有一个混合的C/C++程序。它包含一个针对C的flex/bison解析器,而其余部分是C++。作为C,生成的解析器和扫描器使用malloc、realloc和free管理它们的内存。它们足以暴露钩子(Hook),允许我提交我自己的这些函数的实现。如您所料,(C++)程序的其余部分“想要”使用new、delete等。做一些研究似乎表明相关标准并不能保证这种混合应该有效。特别是C“堆”不一定是C++“空闲区域”。看来这两个方案可以互相践踏。除此之外,有一天(很快)这个程序可能会想要集成一个自定义的堆实现,例如tcmalloc,C和C++都使用。什么是“正确”的做法?考虑到集成tcmallo

c++ - 在混合 C/C++ 程序中协调 malloc 和 new 的 "correct"方法是什么?

我有一个混合的C/C++程序。它包含一个针对C的flex/bison解析器,而其余部分是C++。作为C,生成的解析器和扫描器使用malloc、realloc和free管理它们的内存。它们足以暴露钩子(Hook),允许我提交我自己的这些函数的实现。如您所料,(C++)程序的其余部分“想要”使用new、delete等。做一些研究似乎表明相关标准并不能保证这种混合应该有效。特别是C“堆”不一定是C++“空闲区域”。看来这两个方案可以互相践踏。除此之外,有一天(很快)这个程序可能会想要集成一个自定义的堆实现,例如tcmalloc,C和C++都使用。什么是“正确”的做法?考虑到集成tcmallo