带有 const char* 变量的 printf 问题
我陷入了 printf 问题。如果我能在这里得到一些帮助,我将不胜感激: 在下面的代码中,我可以看到字体系列在第一个 printf() 中正确移位, 但如果我将其设置为变量,我只会得到一个空字符串。如何将其放入变量中并具有正确的值?我只是不想到处输入“font.family().family().string().utf8().data()”?
我用同样的方法做到了这一点:
void myMethod() {
const char* fontFamily = font.family().family().string().utf8().data();
// get displayed correctly
printf ("drawText1 %s \n", font.family().family().string().utf8().data());
// get an empty string
printf ("drawText2 %s \n", fontFamily);
}
'data()' 的签名是
class CString {
public:
CString() { }
CString(const char*);
CString(const char*, unsigned length);
CString(CStringBuffer* buffer) : m_buffer(buffer) { }
static CString newUninitialized(size_t length, char*& characterBuffer);
const char* data() const;
//...
}
utf8() 的签名是
class String {
CString utf8() const;
}
谢谢。
I am stuck in a printf problem. I would appreciate if I can get some help here:
In the below code, I can see the font family get displaced correctly in first printf(),
but if I set it to variable, i only get an empty string. How can I put it in a variable and have the right values? I just don't want to type 'font.family().family().string().utf8().data()' everywhere?
I did this in the same method:
void myMethod() {
const char* fontFamily = font.family().family().string().utf8().data();
// get displayed correctly
printf ("drawText1 %s \n", font.family().family().string().utf8().data());
// get an empty string
printf ("drawText2 %s \n", fontFamily);
}
And the signature of 'data()' is
class CString {
public:
CString() { }
CString(const char*);
CString(const char*, unsigned length);
CString(CStringBuffer* buffer) : m_buffer(buffer) { }
static CString newUninitialized(size_t length, char*& characterBuffer);
const char* data() const;
//...
}
The signature of utf8() is
class String {
CString utf8() const;
}
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
font.family().family().string().utf8().data()
链中的某些内容正在返回一个临时对象。在第一个printf
中,临时对象不会超出范围,直到printf
返回。在第二个 printf 中,在进行指针分配后临时已被销毁,并且指针现在无效。您正在看到“未定义行为”的典型示例。有两种方法可以解决这个问题。要么在临时数据被销毁之前复制数据,要么引用临时数据。只要类有复制运算符,复制可能是最简单、最清晰的。假设
utf8()
生成临时CString
,这将是Something in the chain of
font.family().family().string().utf8().data()
is returning a temporary object. In your firstprintf
, the temporary object doesn't go out of scope until theprintf
returns. In the secondprintf
, the temporary has been destroyed after the pointer assignment was made, and the pointer is now invalid. You're seeing a classic example of "undefined behavior".There are two ways to fix this. Either make a copy of the data before the temporary gets destroyed, or make a reference to the temporary. The copy is probably easiest and clearest, as long as the class has a copy operator. Assuming that
utf8()
generates a temporaryCString
, this would be您正在缓存一个驻留在
utf8()
返回的临时指针中的指针(正如 Mark 和 Neil 所争论的那样)。您必须将fontFamily
更改为CString
或const CString &
以保留utf8()
的结果代码> 在范围内。You are caching a pointer that resides in the temporary returned by
utf8()
(as Mark and Neil have argued about). You'll have to changefontFamily
to either aCString
orconst CString &
to keep the result fromutf8()
in scope.对
data()
的调用(假设在 std::string 上调用)不一定返回以 null 结尾的字符串。您几乎肯定需要c_str()
,它的定义就是这样做的。The call to
data()
(assuming it is called on a std::string) does not necessarily return a null-terminated string. You almost certainly wantc_str()
, which is defined to do so.