P
pereges
I usually initialize all my data structure pointers to null. Then if
an error occurs I first print an error message, and then call a
killall function that releases the memory. eg :
typedef struct mesh
{
int ntri, int nvert;
triangle *tri; /* triangle array */
vector *vert; /* vertex array */
}mesh;
mesh *m_ptr;
void killall()
{
killmesh();
...
...
}
void killmesh(void)
{
if (m_ptr != NULL)
{
if (m_ptr->vert != NULL)
{
free(m_ptr->vert);
}
if (m_ptr->tri != NULL)
{
free(m_ptr->tri);
}
free(m_ptr);
}
}
If the value of the pointer after freeing the memory block pointed by
it is indeterminate, then I might as well make m_ptr->vert , m_ptr-
an error occurs I first print an error message, and then call a
killall function that releases the memory. eg :
typedef struct mesh
{
int ntri, int nvert;
triangle *tri; /* triangle array */
vector *vert; /* vertex array */
}mesh;
mesh *m_ptr;
void killall()
{
killmesh();
...
...
}
void killmesh(void)
{
if (m_ptr != NULL)
{
if (m_ptr->vert != NULL)
{
free(m_ptr->vert);
}
if (m_ptr->tri != NULL)
{
free(m_ptr->tri);
}
free(m_ptr);
}
}
If the value of the pointer after freeing the memory block pointed by
it is indeterminate, then I might as well make m_ptr->vert , m_ptr-