[Qt-interest] Undefined behavior in QString::toStdWString() ?
Thiago Macieira
thiago.macieira at trolltech.com
Fri Dec 12 08:10:06 CET 2008
Robert Hairgrove wrote:
>Thank you for your reply. I just tried doing something similar to what
>is done in the function QString::toStdWString, i.e.:
>
>=== code starts ===
>#include <iostream>
>#include <ostream>
>#include <string>
>
>int main()
>{
> std::string s;
> std::string::iterator it = s.end();
> char * x = &(*(it));
> std::cout << reinterpret_cast<unsigned int>(x) << std::endl;
>}
>=== code ends ===
>
>Everything compiles and seems to run normally, regardless of whether I
>use begin() or end(). However, I just wonder how safe this is?
Very, unless the implementation is broken.
The line: char *x = &(*(it));
is actually: char *x = &(it.operator*());
and std::string::operator*() is supposed to return char&. The & we added
turns it back into a pointer (i.e., char *). That means we haven't
dereferenced anything yet.
--
Thiago Macieira - thiago.macieira (AT) nokia.com
Senior Software Engineer - Nokia, Qt Software
Qt Software is hiring - ask me
Sandakerveien 116, NO-0402 Oslo, Norway
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20081212/4191fb97/attachment.bin
More information about the Qt-interest-old
mailing list