The following code worked in an MFC dll, but when I moved it to a non-MFC DLL, the realloc function returns NULL.
[tt]void MD::nullstr()
{
if (((char*)data)[len-1] != 0) {
// at this location, data points to a valid address
// and len == 9
data = realloc(data,len+1);
// at this location, data == NULL
((char*)data)[len] = 0;
// the preceding line causes a memory fault, of course.
len++;
}
}[/tt]
I don't understand why the realloc fails-- data points to valid memory, and len is equal to 9. I simply want to change the allocation size to 10, and store the new pointer in data.
This worked in an MFC DLL, but not in a normal DLL.
[tt]void MD::nullstr()
{
if (((char*)data)[len-1] != 0) {
// at this location, data points to a valid address
// and len == 9
data = realloc(data,len+1);
// at this location, data == NULL
((char*)data)[len] = 0;
// the preceding line causes a memory fault, of course.
len++;
}
}[/tt]
I don't understand why the realloc fails-- data points to valid memory, and len is equal to 9. I simply want to change the allocation size to 10, and store the new pointer in data.
This worked in an MFC DLL, but not in a normal DLL.