带有 const char* 变量的 printf 问题

发布于 2024-08-24 05:37:13 字数 953 浏览 7 评论 0原文

我陷入了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

是伱的 2024-08-31 05:37:13

font.family().family().string().utf8().data() 链中的某些内容正在返回一个临时对象。在第一个 printf 中,临时对象不会超出范围,直到 printf 返回。在第二个 printf 中,在进行指针分配后临时已被销毁,并且指针现在无效。您正在看到“未定义行为”的典型示例。

有两种方法可以解决这个问题。要么在临时数据被销毁之前复制数据,要么引用临时数据。只要类有复制运算符,复制可能是最简单、最清晰的。假设 utf8() 生成临时 CString,这将是

CString fontFamily = font.family().family().string().utf8();
printf ("drawText2 %s \n", fontFamily.data());

Something in the chain of font.family().family().string().utf8().data() is returning a temporary object. In your first printf, the temporary object doesn't go out of scope until the printf returns. In the second printf, 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 temporary CString, this would be

CString fontFamily = font.family().family().string().utf8();
printf ("drawText2 %s \n", fontFamily.data());
泡沫很甜 2024-08-31 05:37:13

您正在缓存一个驻留在 utf8() 返回的临时指针中的指针(正如 Mark 和 Neil 所争论的那样)。您必须将 fontFamily 更改为 CStringconst 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 change fontFamily to either a CString or const CString & to keep the result from utf8() in scope.

小傻瓜 2024-08-31 05:37:13

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 want c_str(), which is defined to do so.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文