首页
学习
活动
专区
圈层
工具
发布
    • 综合排序
    • 最热优先
    • 最新优先
    时间不限
  • 来自专栏difcareer的技术笔记

    C代码 从源代码到可执行文件——编译全过程解析

    __ ((__nothrow__)) tmpnam (char*); char* __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) __ ((__nothrow__)) gets (char*); int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) puts ( __ ((__nothrow__)) _fdopen (int, const char*); int __attribute__((__cdecl__)) __attribute__ ((__nothrow nothrow__)) putwc (wint_t, FILE*); int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) _putws __nothrow__)) fputwchar (wint_t); int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) getw

    2.5K50发布于 2018-08-23
  • 来自专栏ClickHouse

    C++ const_cast static_cast dynamic_cast reinterpret_cast

    还可以用在基础数据和对象上 static_cast来处理的转换就需要两者具有"一定的关系"了 void staticcast() { /*子类转父类*/ Children *p = new(std::nothrow Parent *b = static_cast<Parent *>(p); b->fly(); /* * 语法错误 Parent *b = new(std::nothrow void dynamiccast() { // 相比较 static_cast 是运行时检测 Children *p = new(std::nothrow) Children; Children *p2 = dynamic_cast<Children *>(b); p2->fly(); /* 空指针 Parent *b = new(std::nothrow fly(); } else { cout << "cast failed" << endl; } Children *children = new(std::nothrow

    75671发布于 2021-10-13
  • 来自专栏zingpLiu

    5分钟理解编译系统

    __ , __leaf__)); 313 extern int _IO_ferror (_IO_FILE *__fp) __attribute__ ((__nothrow__ , __leaf__)); __ , __leaf__)); 322 extern void _IO_funlockfile (_IO_FILE *) __attribute__ ((__nothrow__ , __leaf__) ); 323 extern int _IO_ftrylockfile (_IO_FILE *) __attribute__ ((__nothrow__ , __leaf__)); 324 # 465 " __ , __leaf__)); 467 468 469 extern void setlinebuf (FILE *__stream) __attribute__ ((__nothrow__ , __attribute__ ((__nothrow__)); 488 489 490 491 492 493 extern int vfprintf (FILE *__restrict __s

    1.1K20发布于 2018-09-05
  • 来自专栏程序员的园——原创文章

    指针这几个没用的判空,你做了吗?

    C++标准库提供了std::nothrow 关键字,它可以关闭new 分配失败时抛出异常的机制,转而返回空指针。 } 在MSVC中,std::nothrow版本的new操作符实现如下: _VCRT_EXPORT_STD _NODISCARD _Ret_maybenull_ _Success_(return ! const& ) noexcept; 可以看到,std::nothrow版本的new操作符实现中加入了修饰符_Ret_maybenull_,它的作用是告诉编译器,该函数返回的指针可能为空。 之前抛出异常的代码使用std::nothrow后,就不会抛出异常了,执行了返回值为空的逻辑,保证了程序的健壮性。 int main() { for (size_t i = 0; i <( 2<<30); i++) { auto p = new(std::nothrow) int[2

    32700编辑于 2025-05-09
  • 来自专栏乐行僧的博客

    C++中的new有几种?

    (nothrow) new 形式:int *p = new (nothrow) int(20); 此时指针已经退化为C语言中通过malloc开辟内存得到的指针,是可以通过判空来验证是否成功开辟内存。

    71330编辑于 2022-02-25
  • 来自专栏程序员的园——原创文章

    C++内存分配失败的那些事儿

    这时可以使用std::nothrow来防止抛出异常,而是返回一个空指针。 示例如下: int* ptr = new(std::nothrow) int[100]; //如果分配失败,ptr将为nullptr 使用std::nothrow后,内存分配失败时,new操作符将返回nullptr 如: int* ptr = new(std::nothrow) int[100]; if (ptr == nullptr) { std::cerr << "Memory allocation failed 返回空指针:当程序希望在内存分配失败时继续运行,并且需要手动处理失败的情况时,可以使用std::nothrow来避免抛出异常。 通过使用std::nothrow,开发者能够显式地控制内存分配失败后的行为。此外,内存池和预分配内存等策略也能有效减少内存分配失败的概率,适用于对性能和稳定性要求较高的应用。

    1.1K10编辑于 2024-12-19
  • 深入理解 C++17 std::is_swappable

    std::is_swappable 的变体除了 std::is_swappable,C++17 还提供了几个相关的类型特征:std::is_nothrow_swappable:检查一个类型的对象是否可以被交换 std::is_nothrow_swappable_with:检查两个不同类型的对象是否可以相互交换,并且交换操作不会抛出异常。 <type_traits>#include <vector>int main() { std::cout << std::boolalpha; std::cout << "Is int nothrow " << std::is_nothrow_swappable<int>::value << std::endl; std::cout << "Can int and int be swapped? " << std::is_nothrow_swappable_with<int, int>::value << std::endl; return 0;}注意事项自定义类型:如果我们定义了一个自定义类型

    26300编辑于 2025-02-08
  • 来自专栏鲸落学习笔记

    用于动态内存的 C++ 中的 new 和 delete 运算符

    如果堆中没有足够的内存可供分配,则新请求通过抛出类型为 std::bad_alloc 的异常指示失败,除非“nothrow”与 new 运算符一起使用,在这种情况下它返回一个 NULL 指针(滚动到节中的 int *p = new(nothrow) int; if (! 说明动态分配和释放内存的 C++ 程序 #include <iostream> using namespace std; int main () { int* p = NULL; p = new(nothrow float *r = new float(75.25); cout << "Value of r: " << *r << endl; int n = 5; int *q = new(nothrow

    1.2K30编辑于 2022-11-14
  • 来自专栏C++

    深入探究 C++17 std::is_invocable

    " << std::is_invocable_r_v<double, decltype(add), int, int> << std::endl; return 0;}2. std::is_nothrow_invocable 和 std::is_nothrow_invocable_rstd::is_nothrow_invocable 检查一个可调用对象是否可以使用给定的参数类型进行调用,并且调用过程不会抛出异常。 std::is_nothrow_invocable_r 则在此基础上还要求返回值可以隐式转换为指定的类型。 { std::cout << std::boolalpha; // 检查 safe_foo 是否可以用 int 调用且不抛出异常 std::cout << "Is safe_foo nothrow " << std::is_nothrow_invocable_v<decltype(safe_foo), int> << std::endl; return 0;}五、使用场景1.

    59300编辑于 2025-02-08
  • 来自专栏C++核心准则原文翻译

    C++核心准则:R.10: 避免使用malloc()和free()

    p2 = new Record; // unless an exception is thrown, *p2 is default initialized auto p3 = new(nothrow In such cases, consider the nothrow versions of new. 有些应用或者代码片段不能接受异常。这方面最好的例子是生命周期敏感的硬实时代码。

    87220发布于 2020-03-27
  • 来自专栏HelloCode开发者学习平台

    【iOS底层技术】- Dispatch Source

    , ios(4.0)) DISPATCH_EXPORT DISPATCH_MALLOC DISPATCH_RETURNS_RETAINED DISPATCH_WARN_RESULT DISPATCH_NOTHROW macos(10.6), ios(4.0)) DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_WARN_RESULT DISPATCH_PURE DISPATCH_NOTHROW API_AVAILABLE(macos(10.6), ios(4.0)) DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW void dispatch_resume API_AVAILABLE(macos(10.6), ios(4.0)) DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW void dispatch_suspend API_AVAILABLE(macos(10.6), ios(4.0)) DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW void dispatch_source_cancel

    1.2K30编辑于 2022-01-11
  • 来自专栏along的开发之旅

    [C++模版] 判断函数入参是不是一个lambda

    ::same_as<std::invoke_result_t<Factory&>, T> T& construct_from(Factory factory) noexcept(std::is_nothrow_invocable_v return *::new (static_cast<void*>(&storage)) T(factory()); } void destroy() noexcept(std::is_nothrow_destructible_v

    4.5K40编辑于 2023-02-23
  • 来自专栏JadePeng的技术博客

    Hnswlib 介绍与入门使用

    if (IsMetricType(hnsw_cfg.metric_type.value(), metric::L2)) { space = new (std::nothrow } else if (IsMetricType(hnsw_cfg.metric_type.value(), metric::IP)) { space = new (std::nothrow else if (IsMetricType(hnsw_cfg.metric_type.value(), metric::COSINE)) { space = new (std::nothrow else if (IsMetricType(hnsw_cfg.metric_type.value(), metric::HAMMING)) { space = new (std::nothrow else if (IsMetricType(hnsw_cfg.metric_type.value(), metric::JACCARD)) { space = new (std::nothrow

    1K10编辑于 2023-12-14
  • 来自专栏jiajia_deng

    C++ new关键字的返回值

    如下所示: //C++ 内存申请失败不抛出异常版本 int *q = new (std::nothrow)int[10]; if(q == NULL) return -1; 在new后面增加(std: :nothrow)以后,new不再抛出异常,而是真正得到返回值。

    56630编辑于 2023-10-20
  • 来自专栏HappenLee的技术杂谈

    C++雾中风景11:厘清C++类型转换(static_cast,dynamic_cast,reinterpret_cast,const_cast)

    ." << endl; } }; 上述代码我们定义了两个类Bird与Penguin: int main() { Penguin* p = new (std::nothrow) Penguin int main() { Bird* b = new (std::nothrow) Bird; Penguin* p = static_cast<Penguin *>(b); int main() { Bird* b = new (std::nothrow) Bird; Penguin* p = dynamic_cast<Penguin *>(b); 而reinterpret_cast能够处理所有指针(引用)之间的转换 int main() { Bird* b = new (std::nothrow) Bird; Penguin*

    79121发布于 2018-10-10
  • 来自专栏JadePeng的技术博客

    Hnswlib 介绍与入门使用

    if (IsMetricType(hnsw_cfg.metric_type.value(), metric::L2)) { space = new (std::nothrow } else if (IsMetricType(hnsw_cfg.metric_type.value(), metric::IP)) { space = new (std::nothrow else if (IsMetricType(hnsw_cfg.metric_type.value(), metric::COSINE)) { space = new (std::nothrow else if (IsMetricType(hnsw_cfg.metric_type.value(), metric::HAMMING)) { space = new (std::nothrow else if (IsMetricType(hnsw_cfg.metric_type.value(), metric::JACCARD)) { space = new (std::nothrow

    1.4K10编辑于 2023-12-14
  • 来自专栏鲸落学习笔记

    C++ 中用于动态内存的 的 new 和 delete 运算符

    如果堆中没有足够的内存可供分配,则新请求通过抛出类型为 std::bad_alloc 的异常指示失败,除非“nothrow”与 new 运算符一起使用,在这种情况下它返回一个 NULL 指针。 int *p = new(nothrow) int; if (! delete[] p; #include <iostream> using namespace std; int main () { int* p = NULL; p = new(nothrow *r = new float(75.25); cout << "Value of r: " << *r << endl; int n = 5; int *q = new(nothrow

    86810编辑于 2022-11-14
  • 【C++特殊工具与技术】优化内存分配(三):operator new函数和opertor delete函数

    版本) // 分配失败时返回nullptr,不抛出异常 void* operator new(std::size_t size, const std::nothrow_t&) noexcept; void * operator new[](std::size_t size, const std::nothrow_t&) noexcept; ③定位分配(placement new) // 在已分配的内存地址 版本配对的释放函数 void operator delete(void* ptr, const std::nothrow_t&) noexcept; void operator delete[](void * ptr, const std::nothrow_t&) noexcept; // 与对齐分配配对的释放函数(C++17) void operator delete(void* ptr, std:: 关键特性总结 特性 说明 全局默认实现 标准库提供的operator new底层调用malloc,operator delete调用free 异常行为 普通版本分配失败抛std::bad_alloc,nothrow

    13510编辑于 2026-01-21
  • 来自专栏方亮

    C++的四种强制转换

    如果是调用new (std::nothrow) Child,则调用的是Child的printv。如果是调用new (std::nothrow) Parent,则调用的是Parent的printv。   void reinterpret_cast_test() { { Parent* pParent = new (std::nothrow) Parent(); if (! 我们以static_cast为例: { Parent* pParent = new (std::nothrow) Parent(); if (! 我们看段使用dynamic_cast规避掉运行时出错的代码 { Parent* pParent = new (std::nothrow) Parent(); if (! 下面两段代码是安全的 { Child* pChild = new (std::nothrow) Child(); if (!

    2.7K30发布于 2019-01-16
  • 来自专栏IT大咖说

    nodejs写bash脚本终极方案!

    module的文件结尾,也就是这个文件直接import模块就行,不用其它工具转义 2、自带支持管道操作pipe方法 3、自带fetch库,可以进行网络请求,自带chalk库,可以打印有颜色的字体,自带错误处理nothrow Promise<string> type QuestionOptions = { choices: string[] } sleep() 基于setTimeout 函数 await sleep(1000) nothrow () 将 $ 的行为更改, 如果退出码不是0,不跑出异常. ts接口定义 function nothrow

    (p: P): P await nothrow($`grep something from-file /examples -type f -print0` .pipe(nothrow($`xargs -0 grep something`)) .pipe($`wc -l`) 以下的包,无需导入,

    4.5K20发布于 2021-07-19
领券