当前位置: 首页 > news >正文

__attribute__((noreturn))

        GNU C 的一大特色就是__attribute__ 机制,__attribute__ 可以设置函数属性(Function Attribute)、变量属性(Variable Attribute)和类型属性(Type Attribute)。

语法格式为: __attribute__((attribute-list))

当__attribute__ 用于修饰函数时,它就相当于一个函数说明符,跟 inline 等同一类

__attribute__((noreturn)) :这个属性告诉编译器函数永远不会有返回值,可以避免当一个函数需要返回值,在某种条件下未能执行到返回值处编译器警告或报错。

C库函数 abort() 函数和 exit() 函数都使用此属性声明

void exit(int)   __attribute__((noreturn));void abort(void) __attribute__((noreturn));

验证例子如下:

attribute.c(exit_my() 函数未声明__attribute__((noreturn))  属性)

#include <stdio.h>
#include <stdlib.h>void exit_my()
{exit(1);
}int get_v(int v)
{if (v < 3)exit_my();elsereturn v;
}void print_v(int v)
{printf("v = %d\n", v);
}int main(int argc, char *argv[])
{int i = get_v(5);print_v(i);return 0;
}

这时,编译代码时,警告信息

 attribute.c(将 exit_my() 函数声明__attribute__((noreturn))  属性)

#include <stdio.h>
#include <stdlib.h>__attribute__((noreturn)) void exit_my()
{exit(1);
}int get_v(int v)
{if (v < 3)exit_my();elsereturn v;
}void print_v(int v)
{printf("v = %d\n", v);
}int main(int argc, char *argv[])
{int i = get_v(5);print_v(i);return 0;
}

 这时,编译代码时,没有警告信息

参考:

__attribute__ 机制详解_weaiken的博客-CSDN博客

http://www.lryc.cn/news/110452.html

相关文章:

  • 遮挡边界处的深度补全和双曲面外推
  • LK-99室温超导激发万万亿市场,将对我们的生活产生哪些影响?
  • 子集——力扣78
  • 【计算机视觉 | 目标检测 | 图像分割】arxiv 计算机视觉关于目标检测和图像分割的学术速递(8 月 2 日论文合集)
  • JDK中「SPI」原理分析
  • DSL:数字用户线路(Digital Subscriber Line)
  • Java集合ArrayList详解
  • React Dva项目 Model中编写与调用异步函数
  • 小程序自定义tabBar+Vant weapp
  • Dubbo+Zookeeper使用
  • 短视频平台视频怎么去掉水印?
  • Stable Diffusion - Style Editor 和 Easy Prompt Selector 提示词插件配置
  • Stable Diffusion - SDXL 模型测试 (DreamShaper 和 GuoFeng v4) 与全身图像参数配置
  • 中介者模式(Mediator)
  • SpringBoot使用@Autowired将实现类注入到List或者Map集合中
  • 【linux目录的权限和粘滞位】
  • TP DP PP 并行训练方法介绍
  • P005 – Python操作符、操作数和表达式
  • SQL92 SQL99 语法 Oracle 、SQL Server 、MySQL 多表连接、Natural 、USING
  • 物联网平台使用笔记
  • Python-flask项目入门
  • 基于数据库 Sqlite3 的 root 管理系统
  • Hadoop 之 Hive 4.0.0-alpha-2 搭建(八)
  • vue3常用API之学习笔记
  • Python 程序设计入门(005)—— 字符串操作
  • 怎样将项目jar包放到服务器上
  • ruby调试
  • 【云原生】使用kubeadm搭建K8S
  • HCIE-Datacom真题和机构资料
  • 轮足机器人硬件总结