How do I use _malloca instead of _alloca in Win32 C++ project?

  alloca, bstr, c++, malloc

I’m updating an old C++ DLL project. For one of the exported functions there’s

BSTR __stdcall j2cs( const long lJulian, int bDMY, BSTR sDelim ) {
    USES_CONVERSION;
    int iDay, iMonth;
    long lYear;
    char chDate[20];
    char chInt[10];
    char * cDelim = W2A( sDelim );

The W2A macro is defined as

#define W2A(lpw) (
    ((_lpw = lpw) == NULL) ? NULL : (
        (_convert = (static_cast<int>(wcslen(_lpw))+1), 
        (_convert>INT_MAX/2) ? NULL : 
        ATLW2AHELPER((LPSTR) alloca(_convert*sizeof(WCHAR)), _lpw, _convert*sizeof(WCHAR), _acp))))

Visual Studio 2019 flags the W2A macro with the following compiler warning

Warning C6255   _alloca indicates failure by raising a stack overflow exception.  Consider using _malloca instead.  

How would I make the suggested change to the W2A macro? Or should I just ignore the warning?

Source: Windows Questions C++

LEAVE A COMMENT