Search code examples
c++visual-c++compiler-optimizationitoa

_itoa_s kills C++ optimizing compiler all time, why?


I have a code on which the optimizing compiler always fails, with each launch.

char* GetWinSockVersion()
{
    char *tmpData = (char*)malloc(sizeof(wsaData.wVersion));
    _itoa_s<wsaData.wVersion>(wsaData.wVersion, tmpData, 10);
    return tmpData;
}

It does fail with the _itoa_s. I'm interested, why does it fail all times?


Solution

  • You aren't using the function properly.

    _itoa_s requires 4 arguments.

    This function is meant to be used with c, for c++ you have stream.

    Your code should be more like ( assuming wsaData.wVersion is a number )

    char *tmpData = (char*)malloc(sizeof( char ) * 80 );
    _itoa_s(wsaData.wVersion , tmpData , 80 , 10);
    
    //_itoa_s(number to convert , target string, size of target string, number base);