在C中声明函数中的结构 [英] Declaring structures within functions in C

查看:194
本文介绍了在C中声明函数中的结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个结构,只有一个功能必须访问。该功能将诸如k,K,kb,KB,m,M,mb,MB,...的令牌转换为实际单位。这样做的目的是简化配置文件。



所以,假设我们有:

  static uint32_t real_unit(const char * str)
{
struct u2type {
char key [3];
uint32_t val;
} const u2types [] = {
{k,KB_UNIT},
{K,KB_UNIT},
{kb,KB_UNIT},
{KB,KB_UNIT},
{m,MB_UNIT},
{M,MB_UNIT},
{mb,MB_UNIT},
{ MB,MB_UNIT},
{g,GB_UNIT},
{G,GB_UNIT},
{gb,GB_UNIT},
{GB ,GB_UNIT},
{{0},0}
};

...代码查找...
}



我已经看到其他程序,其中struct u2type将被声明为静态(再次,在一个函数内),我看不到这是有用的。结构不会改变,每次输入功能将始终是相同的。这就是为什么我做了const。



然而,我看到很多人做了statc struct foo {...} const foos [] = {...}一个范围很明显的功能。



这样做有什么好处吗?我尝试学习ASM输出,然后再来这些优化问题,但我不是一个汇编大师:)



编辑:



是的,我知道这种方式闻起来像脚。一些项目只有奇怪的要求(通常是由奇怪的人要求)。这个问题与功能的使用完全不同。

解决方案

使其成为静态,使其成为两个不同的东西




  • 如果它是const,每个函数调用获得自己不可改变的struct
  • 实例
  • 如果它是静态的,则在所有函数调用之间共享一个可变的实例。



听起来好像你想要的是一个struct实例是静态和常量,这是一个合理的事情。



性能明智,静态版本应该有一点点,因为结构实例的构造将只需一次。


I have a structure that only one function must access. The function converts tokens like "k, K, kb, KB, m, M, mb, MB, ..." into an actual unit. The purpose of this is to simplify a configuration file.

So, suppose we have:

static uint32_t real_unit(const char *str)
{
    struct u2type {
      char key[3];
      uint32_t val;
    } const u2types[] = {
       { "k", KB_UNIT },
       { "K", KB_UNIT },
       { "kb", KB_UNIT },
       { "KB", KB_UNIT },
       { "m", MB_UNIT },
       { "M", MB_UNIT },
       { "mb", MB_UNIT },
       { "MB", MB_UNIT },
       { "g", GB_UNIT },
       { "G", GB_UNIT },
       { "gb", GB_UNIT },
       { "GB", GB_UNIT },
       { { 0 }, 0 }
    };

    ... code to look up str ...
}

I have seen other programs where struct u2type would be declared as static (again, within a function) and I can't see how that is useful. The structure is not going to change, it will always be the same every time the function is entered. That's why I made it const.

Yet, I have seen many people do statc struct foo { ... } const foos[] = { ... }, within a function where the scope is just obvious.

Is there any benefit to doing that? I try to study ASM output prior to coming to SO with optimization questions like these, but I am not an assembly guru :)

EDIT:

Yes, I know this approach smells like feet. Some projects just have odd requirements (typically mandated by odd people). The question, however remains entirely separate from the use of the function.

解决方案

Making it const and making it static do two different things.

  • if it is const, each function call gets its own unchangeable instance of the struct
  • if it is static there is one changeable instance of the struct shared across all function calls

It sounds as though what you want is a struct instance that is both static and const, which is a reasonable thing to do.

Performance wise, the static version should have a slight edge, as construction of the struct instance will only be done once.

这篇关于在C中声明函数中的结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆