Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

printf question with a const char* variable

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.

like image 267
michael Avatar asked Mar 05 '10 21:03

michael


People also ask

What is const char* C?

In C programming language, *p represents the value stored in a pointer and p represents the address of the value, is referred as a pointer. const char* and char const* says that the pointer can point to a constant char and value of char pointed by this pointer cannot be changed.

What's the difference between const char* p char* const p and const char* const p?

NOTE: There is no difference between const char *p and char const *p as both are pointer to a const char and position of '*'(asterik) is also same. 2. char *const ptr : This is a constant pointer to non-constant character. You cannot change the pointer p, but can change the value pointed by ptr.

How do I print a const char?

const char *newLine = "\n"; printf('Content: %c\n', *newLine);

What does const mean in C?

What does const mean? C and C++ If you declare a variable to be a const, you're telling the compiler that it's a read-only variable and that it won't be changed throughout its existance. A values that's passed in as a parameter to a function, for example, will be left alone until the function exits.


2 Answers

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());
like image 165
Mark Ransom Avatar answered Oct 18 '22 18:10

Mark Ransom


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.

like image 26
MSN Avatar answered Oct 18 '22 17:10

MSN