C测试变量是否在只读部分中 [英] C test if variable is in read-only section
问题描述
我想编写一个看起来像这样的低级日志记录功能:
I'd like to write a low-level logging function that would look like:
DO_DBG("some string", val1, val2)
出于性能原因,我要执行的操作是将指针存储在字符串中,而不是字符串的副本中.假定该字符串是只读文字.为了防止人们不得不调试调试器,如果编译器可以抱怨DO_DBG
的第一个参数在代码与文本的可写部分中,等等,那就很好了.我想知道是否存在执行此操作的机制. (我使用的是gcc 4.9.1,ld 2.24).
What I want it to do, is to store the pointer to the string rather than a copy of the string, for performance reasons. This assumes that the string is a read-only literal. To prevent people having to debug the debugger, it would be nice if the compiler could complain if the first parameter of DO_DBG
was in a writable section of code vs text, etc. I'm wondering if a mechanism to do that exists. (I'm using gcc 4.9.1, ld 2.24).
推荐答案
您可以根据需要使用自动文字字符串连接:
You could use the automatic literal string concatenation to your advantage:
#define DO_DBG(s, a, b) _DO_DBG("" s, a, b)
并将您的真实宏实现为_DO_DBG
.
And implement your real macro as _DO_DBG
.
这篇关于C测试变量是否在只读部分中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!