new & delete question
-
Hi, If I have in my *.h: double** m_GLdouble; and in my *.cpp: m_GLdouble = new double*[m_Size * 12]; when it's time to delete, do I have to do? for(int i = 0 ; i < m_Size * 12 ; i++) { delete m_GLdouble[i]; } delete m_GLdouble; ->or<- delete [] m_GLdouble; m_GLdouble = NULL; Also is there a way to return a pointer such a way that the receiving function cannot modifie the returned object? Something like: Object* myClass::myReadFunction() { return m_Object; } void otherClass::otherfunction() { Object* pObj; pObj = m_myClassObj->myReadFunction(); //Is there a way to make pObj impossible to modify? } hope you understand Thanks Everything's beautiful if you look at it long enough...
-
Hi, If I have in my *.h: double** m_GLdouble; and in my *.cpp: m_GLdouble = new double*[m_Size * 12]; when it's time to delete, do I have to do? for(int i = 0 ; i < m_Size * 12 ; i++) { delete m_GLdouble[i]; } delete m_GLdouble; ->or<- delete [] m_GLdouble; m_GLdouble = NULL; Also is there a way to return a pointer such a way that the receiving function cannot modifie the returned object? Something like: Object* myClass::myReadFunction() { return m_Object; } void otherClass::otherfunction() { Object* pObj; pObj = m_myClassObj->myReadFunction(); //Is there a way to make pObj impossible to modify? } hope you understand Thanks Everything's beautiful if you look at it long enough...
The way have allocated memory to the pointers, I suppose that one delete statement should be sufficient. Reg. your second question, may be you can consider using const Object obj; obj=&m_myClassObj->myReadFunction(); Harsha ---------------------------------- http://www.ece.arizona.edu/~hpg ----------------------------------
-
Hi, If I have in my *.h: double** m_GLdouble; and in my *.cpp: m_GLdouble = new double*[m_Size * 12]; when it's time to delete, do I have to do? for(int i = 0 ; i < m_Size * 12 ; i++) { delete m_GLdouble[i]; } delete m_GLdouble; ->or<- delete [] m_GLdouble; m_GLdouble = NULL; Also is there a way to return a pointer such a way that the receiving function cannot modifie the returned object? Something like: Object* myClass::myReadFunction() { return m_Object; } void otherClass::otherfunction() { Object* pObj; pObj = m_myClassObj->myReadFunction(); //Is there a way to make pObj impossible to modify? } hope you understand Thanks Everything's beautiful if you look at it long enough...
For every new, you'll need a matching delete. So if your allocation looked like:
double **m_GLdouble; m_GLdouble = new double*[m_Size * 12];
the deallocation would be:delete [] m_GLdouble;
But if you also had allocations for each of those pointers:for (int x = 0; x < (m_Size * 12); x++)
{
= new double;
}you'd have to delete those before deleting m_GLdouble itself. The deallocation would be:
for (x = 0; x < (m_Size * 12); x++)
{
delete m_GLdouble[x];
}
delete [] m_GLdouble;