C语言学习NO5:__attribute__((section("section_name")))

目录

 

前言

一、编译时为变量指定段

二、编译时为函数指定段

三、带电复位后不清0的.noinit变量


前言

__attribute__((section("section_name"))),其作用是将作用的函数或数据放入指定名为"section_name"对应的段中。

__attribute__主要用于改变所声明或定义的函数或 数据的特性,它有很多子项,用于改变作用对象的特性。比如对函数,noline将禁止进行内联扩展、noreturn表示没有返回值、pure表明函数除 返回值外,不会通过其它(如全局变量、指针)对函数外部产生任何影响。


一、编译时为变量指定段

Example
/* in RO section */
const int descriptor[3] __attribute__ ((section ("descr"))) = { 1,2,3 };
/* in RW section */
long long rw[10] __attribute__ ((section ("RW")));
/* in ZI section *
long long altstack[10] __attribute__ ((section ("STACK"), zero_init));/

Note:This variable attribute is a GNU compiler extension supported by the ARM compiler.(MDK)


二、编译时为函数指定段

Example
In the following example, Function_Attributes_section_0 is placed into the RO section new_section rather than .text.
 
void Function_Attributes_section_0 (void)
    __attribute__ ((section ("new_section")));
void Function_Attributes_section_0 (void)
{
    static int aStatic =0;
    aStatic++;
}



In the following example, section function attribute overrides the #pragma arm section setting.
 
#pragma arm section code="foo"
  int f2()
  {
      return 1;
  }                                  // into the 'foo' area
  __attribute__ ((section ("bar"))) int f3()
  {
      return 1;
  }                                  // into the 'bar' area
  int f4()
  {
      return 1;
  }                                  // into the 'foo' area
#pragma arm section

三、带电复位后不清0的.noinit变量

.bss段的变量在芯片启动的.init4阶段会被清0,但是定义在.bss.noinit段中的变量在.init4阶段不会被清0。单片机不掉电复位后,.noinit段的这些变量的值依然存在,可以继续使用。

.noinit段的定义方法:int bar __attribute__ ((section (".noinit")));
不能指定初值,因为这些变量是在.bss段。

(验证:定义一个.noinit段变量,按下开发板的复位键(单片机不掉电复位),这个变量的值不会被清0。)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值