I have written a function that concatenates strings by using HeapAlloc() and HeapFree() and I want to check the return of these functions. However, if allocation fails, I must try again to allocate until it works. How to do that ?
I link this question with this one.
void add_to_buffer(LPTSTR* buffer, LPCTSTR msg) {
// Determine new size
int newSize = 0;
// Allocate new buffer
if (*buffer == NULL)
newSize = _tcsclen(msg) + 1;
else
newSize = _tcslen(*buffer) + _tcsclen(msg) + 1;
// Do the copy and concat
if (*buffer == NULL)
{
*buffer = (LPTSTR)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, newSize * sizeof(*buffer));
_tcsncpy_s(*buffer, newSize, msg, _tcsclen(msg));
}
else
{
*buffer = (LPTSTR)HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *buffer, newSize * sizeof(*buffer));
_tcsncat_s(*buffer, newSize, msg, _tcsclen(msg));
}
}