如何添加静态断言来检查变量是否是静态的?
我有一个仅适用于静态局部变量的宏(因为它使用内联汇编表示法来提取有关变量的数据)。我需要一种方法来强制宏的输入确实是静态局部变量:
正确:
func f()
{
static int x;
my_macro(x);
}
不正确:
func f()
{
int x;
my_macro(x);
}
我使用 GCC for C(没有 C++)。
I have a macro which works well only on static local variables (since it uses inline assembly notation to extract data about the variable). I need a way to enforce that the input to the macro is indeed a static local variable:
correct:
func f()
{
static int x;
my_macro(x);
}
not correct:
func f()
{
int x;
my_macro(x);
}
I work with GCC for C (no C++).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用以下技巧:
GCC 对于非静态变量发出错误“初始化器元素不是常量”。
You can use following trick:
GCC issues an error "initializer element is not constant" for non-static variables.
您也许可以通过使用静态变量和局部变量的地址来区分它们:
静态变量存储在.BSS 或 .DATA 部分
局部变量存储在堆栈中
例如,以下程序在我的系统上的输出
是这样的:
每个部分和堆栈的放置位置取决于您平台的 ABI,但您可以使用块局部变量的地址来形成条件:
此输出:
警告:我不建议使用此“技巧”。这就是全部:一个诡计,一个在最不合时宜的情况下就会被破解的诡计。只需正确记录您的宏,并让使用它的人处理误用的后果即可。
You might be able to tell static and local variables apart by using their addresses:
Static variables are stored in either the .BSS or .DATA sections
Local variables are stored in the stack
For example the output of the following program on my system
is this:
Where each section and the stack are placed depends on the ABI of your platform, but you could use the address of a block-local variable to form a condition:
This outputs:
Warning: I would not advise using this "trick". That's all it is: a trick, one that will break in the most unopportune of circumstances. Just document your macro properly and let the people who use it handle the aftermath of its misuse.
只需按照大多数 C 库使用的方式进行即可:告诉用户您的宏适用于静态变量,而其他任何内容的行为可能是未定义/意外的。
就像您也可以将 NULL 指针传递给
strdup()
等一样,这只会导致段错误,不强制执行不会产生大问题。Simply do it in the way most C libraries use: Tell users that your macro works for static variables and the behavior for anything else may be undefined/unexpected.
Just like you can also pass NULL pointers to
strdup()
etc. which will result in nothing but a segfault there's not a big issue with not enforcing stuff.我认为你无法在 ISO C 中区分这些情况。但是既然你已经提到你使用 GCC,那么可能有一些有用的内置伪函数。它们的名称均以
__builtin_
开头,因此您应该仔细阅读 GCC 文档中的该列表。http://www.google.com/search?q=gcc+builtin
好吧,我刚刚通读了整个内置部分,但没有找到任何东西。所以我觉得这确实是不可能的。
出于好奇,您想对宏做什么?
I think you cannot distinguish these cases in ISO C. But since you already mentioned that you use GCC there may be some helpful built-in pseudo functions. Their names all start with
__builtin_
, so you should read through that list in the GCC documentation.http://www.google.com/search?q=gcc+builtin
Well, I just read through the whole built-ins section, and I didn't find anything. So I think it is really not possible.
What are you trying to do anyway with the macro, just out of curiosity?