|
Boost : |
From: Darin Adler (darin_at_[hidden])
Date: 2001-10-16 10:10:42
on 10/16/01 6:13 AM, williamkempf_at_[hidden] wrote:
> This much I understood ;). What didn't make a lot of sense to me was
> that on Win32 platforms unsigned long (DWORD) and unsigned int are
> the same size. So the first warning seemed to be overly pedantic.
The warning is to help with portability. Since DWORD is unsigned long, the
fix is to change the format string to say %lX instead of %X.
> I was also unsure how to handle the second warning since there's no
> gaurantee that a pointer can be converted to an integral type of a
> given size. The %p flag would work, but would produce a string
> different from what I wanted. That's a minor issue, I guess, since
> the string produced would still be unique, it would just be longer
> which is only a minor optimization.
If a pointer is not the same size as an integral type, and you want to do
the conversion anyway, then you should use reinterpret_cast. Just passing
the wrong type and letting printf "handle" the conversion is much worse,
since the printf family of calls rely on using the format string to know the
size of parameter to read. That's the reason for the warning.
I personally would use ostringstream for something like this to avoid these
pitfalls. But I think it's pretty easy to fix it without doing that:
char name[41];
std::sprintf(name,
"2AC1A572DB6944B0A65C38C4140AF2F4%08lX%08X",
GetCurrentProcessId(),
reinterpret_cast<unsigned>(&flag));
Since this is Windows-specific code, portability is not really an issue.
By the way, without the "08" I think you already weren't getting what you
wanted, since a process ID of 12 would turn into "C" rather than "0000000C".
-- Darin
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk