c++:字符串常量的地址
我可以在 g++ 中以字符串常量形式获取对象的地址吗?示例:
struct s { } x;
如果 &x == 1234
,那么我的代码中需要 "1234"
。
编辑:
通过字符串常量,我的意思是我在编译或链接时需要该常量字符串。我需要将它嵌入到内联汇编代码中,如下所示:
template < typename U >
struct T {
static int x;
void f () {
asm (".word " some-expression-containing-(&x));
}
};
我不知道如何使用预处理器宏构造损坏的名称,所以我问了这个问题。
该解决方案不需要可移植,g++ 就足够了。
不过,地址本身在编译或链接时是已知的,因为它可以检查汇编输出并将损坏的名称放入内联汇编指令中。
Can I get the address of an object as a string constant in g++? Example:
struct s { } x;
If &x == 1234
, then I need "1234"
in my code.
EDIT:
By string constant I meant that I need that constant string at compile- or link-time. I need to embed it in inline assembly code like this:
template < typename U >
struct T {
static int x;
void f () {
asm (".word " some-expression-containing-(&x));
}
};
I don't know a way to construct the mangled name with a preprocessor macro, so I asked this question.
The solution doesn't need to be portable, g++ is sufficient.
The address itself is known at compile- or link-time though, as it would work to examine the assembly output and put in the mangled name into the inline assembly instruction.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我想知道你为什么需要那个......但你可以这样做:
ss.str()
现在包含你想要的字符串编辑:如果你想在编译时执行此操作...呃...我敢假设这是不可能的?
I wonder why would you need that... but you can do this:
ss.str()
now contains your desired stringedit: If you want to do this compile-time... erm... I'll dare assume it's impossible?
这对我有用 g++。
This works for me in g++.
您可以这样做
(它仅适用于文件范围变量符号,因为只有它们可能获得符号表条目)。它不是常量表达式,但在程序完全链接之前,地址无论如何都是未知的。
You can do
(it will work only for file-scope variables symbols as only they potentially get a symbol table entry). It's not constant expression, but the address is anyway not known until the program is fully linked.
在 C 中,你可以这样做,但是,在 C 中,当你使用 char * 作为字符串时,你需要这样的东西:
打印字符串所在的内存范围,而不是的起始地址。
In C, you could do this, but, in C, as you are using
char *
's as your strings, you need something like this:That prints the range of memory the string is in, istead of the starting adress.
错误输出操作数约束缺少
=
是因为在operator outm
之前没有操作符=
。取=m
The error output operand constraint lacks
=
is because haven't operator=
before of the operator outm
. Take=m