Is it good to check pointer with 0xDD or 0xCC or 0xFD, or 0xCD IF its not NULL? I am writing plugin for Unity3D, which supports C with some features of C++. So new is not allowed in here.
char** m_strArry;
void FreeChar(char* a_data)
{
free(a_data);
a_data = NULL;
}
void Test_PointerReference2()
{
m_strArry = (char**)malloc(3);
char* l_str1 = (char*)malloc(5);
strcpy_s(l_str1, 5, "Test");
char* l_str2 = (char*)malloc(7);
strcpy_s(l_str2, 7, "String");
char* l_str3 = (char*)malloc(5);
strcpy_s(l_str3, 5, "Here");
m_strArry[0] = l_str1;
m_strArry[1] = l_str2;
m_strArry[2] = l_str3;
FreeChar(l_str2);
for (int l_index = 0; l_index < 3; l_index++)
{
char* l_data = m_strArry[l_index]; //ISSUE: FOR l_index=1(OR l_str2) l_data is a valid address with garbage data.
if(l_data == NULL)
printf_s("\nIndex %d is NULL", l_index);
else
printf_s("\nIndex %d = '%s'", l_index, l_data);
}
}
So i fixed the above issue by sending pointer-reference to FreeChar(). i.e.,
void FreeChar(char*& a_data)
{
free(a_data);
a_data = NULL;
}
with above code, i am getting pointer value of m_strArry[1] OR l_str2 as NULL.
But in some of my cases, i am getting poitner value as 0xDD (i.e., The memory locations filled are Released heap memory). I read on this link that 0xDD, 0xCC, 0xFD, and 0xCD are reserved address.
So, Is it good (or good practice) to check pointer with 0xDD or 0xCC or 0xFD, or 0xCD besides checking for NULL? If i shouldn't check for any reason, then what is the solution to this issue?