-1

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?

James M
  • 18,506
  • 3
  • 48
  • 56
Secret
  • 2,627
  • 7
  • 32
  • 46
  • 3
    What exactly do you mean when you say the compiler fails? Does it give a compiler error, does it crash, does it produce incorrect code? – Grizzly Mar 31 '13 at 23:21
  • @Grizzly it crashes with execution fail ( compilers fails at execution not my program ) – Secret Mar 31 '13 at 23:32
  • @OlegOrlov Please add the message you're getting. – Drew Dormann Mar 31 '13 at 23:46
  • I'm not familiar with the functions being used, but it does look like you're trying to convert into a buffer which is too small. If the first argument to `_itoa_s` is an integer, then you're only allocating `sizeof int` bytes (typically 4), but you're telling `_itoa_s` that there are 10. – James Kanze Mar 31 '13 at 23:52

1 Answers1

4

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);