Boost logo

Boost :

From: helmut.zeisel_at_[hidden]
Date: 2001-10-16 02:44:29


--- In boost_at_y..., Darin Adler <darin_at_b...> wrote:
> on 10/15/01 1:57 PM, williamkempf_at_h... at williamkempf_at_h...
> wrote:
>

> > ../src\once.cpp:61: warning: unsigned int format, DWORD arg (arg
3)
> > ../src\once.cpp:61: warning: unsigned int format, pointer arg
(arg
4)
>
> These are warning about values passed to a printf-family function
that do
> not match the format in the printf string.
>

std::ostringstream has been designed to avoid these problems.
Instead of

        char name[41];
        std::sprintf(name,
                     "2AC1A572DB6944B0A65C38C4140AF2F4%X%X",
                     GetCurrentProcessId(),
                     &flag);
        HANDLE mutex = CreateMutex(NULL, FALSE, name);

you could include <sstream> and use

        std::ostringstream name;
        name << "2AC1A572DB6944B0A65C38C4140AF2F4"
             << std::hex << GetCurrentProcessId()
             << &flag;
        HANDLE mutex = CreateMutex(NULL, FALSE, name.str().c_str());

This is type safe and avoids potential problems with buffer overflows;
in particular, it should also work on future windows version
were the return type of GetCurrentProcessId() might change
(IA64?)

OTOH you have an additional dependecy on <sstream>
and a (negligible?) performance penalty
for the dynamic memory allocation in std::ostringstream.

For <sstream> and GCC 2.95,
cf. http://gcc.gnu.org/faq.html#2.95sstream

Helmut


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk