poniendo NULL String en %s en String().sprintf()

I am using C++ Builder XE4 on Windows7 Professional (32bit).

I found the difference in the following code.

String lbl = L"";

String msg1 = String().sprintf(L"[%s]", lbl);
ShowMessage(msg1); // [(null)]

String msg2 = String().sprintf(L"[%s]", lbl.c_str());
ShowMessage(msg2); // []

I expected both of msg1 and msg2 were "[]". But actually, msg1 is "[(null)]". Is the msg1 correct in terms of language specification? Or is this "undefined" behavior?

Another question is how I should take care of this kind of mistake (letting "lbl" not "lbl.c_str()"). Because both of them shows no error in compiling, I will not notice this kind of error in the compling time.

I hope to know some prevention tips for this kind of mistake if there is any.

preguntado el 27 de noviembre de 13 a las 02:11

1 Respuestas

C++ Builder 32 bit compiler doesn't warn you about this mistake. All compilers based on LLVM (like bcc64 and gcc) show below error in this case:

cannot pass object of non-trivial type 'String' (aka 'System::UnicodeString') through variadic method; call will abort at runtime

If you compile the source by C++ Builder 64bit (based on LLVM) you will see this error.

respondido 27 nov., 13:05

Thank you very much for your reply. It's good to know that there is some environment the mistake becomes clear. - sietedenueve

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.