【问题标题】:How Structure can be mapped with section attribute in a c program?如何在 c 程序中将结构与节属性映射?
【发布时间】:2019-08-19 04:04:09
【问题描述】:

这只是对 c 程序中结构的随机思考。

在 C 程序中__attribute__ 提供了一个功能,可以在进行声明时指定特殊属性。

section - 在“section”的帮助下,可以将特定变量或函数放置在特定地址

例如,对于要使用属性定义的变量,可以这样完成

int g1 __attribute__((section (".vector"))) = 100;

对于要使用属性定义的函数,可以这样完成

int some_function(void) __attribute__((section(".vector")));
   int some_function(void)
   {
   int local_variabale1 = 100;
    return local_variable1;

   }

因此,从上面的示例中,可以将部分 .vector 放在我在链接脚本中指定地址的特定地址。

C 中的结构是否有机会将结构放在部分中?

假设这是我在 c 中的structure program

struct database
    {
    int employee_number;
    string name;
    int phone_number;
    };

如果有可能怎么办?

【问题讨论】:

  • C/C++ 不是一个东西。 C 和 C++ 以截然不同的方式处理问题。如果这里不关心 C++,请忽略它。

标签: c memory-mapped-files sections


【解决方案1】:

C 中的结构是否有机会将结构放在部分中?

结构的定义在运行时不存在于内存中的任何地方——它只在编译期间使用。因此,给它一个部分是没有意义的。

如果您要声明该结构的一个实例(例如struct database DB),那是一个变量,可以放在一个部分中。不过,结构本身并不是变量。

【讨论】:

  • 嗯,谢谢,结构实例可以放置在定义的部分...像这样struct database db __attribute__ ((section (".vector")));
猜你喜欢
  • 2022-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-16
  • 1970-01-01
  • 2020-11-03
相关资源
最近更新 更多