delete[] and aliasing questions

Started by
3 comments, last by drarem 20 years, 8 months ago
1) Can I just do a delete obj3D or do I need to delete[] the members first, then delete the whole object? 2) Is it possible to alias so I don''t end up with something like: instead of.. object->face->obj[3].vx have.. of[3].vx ?
struct VECTOR {
  float *vx,*vy,*vz;  // xyz verticies

  float *cu,*cv;      // uv texture coords

  float *nx,*ny,*nz;  // xyz normals

};

struct OBJ3D {
  int objStyle;
  VECTOR *obj;
};

clsObject::~clsObject() {
   delete obj3D;
}

int clsObject::init_obj3D() {
    obj3D = new OBJ3D;  // create single allocation

    obj3D->objStyle = GL_TRIANGLES;
    obj3D->obj->vx = new int[32];
    obj3D->obj->vy = new int[32];
    obj3D->obj->vz = new int[32];
}
I fseek, therefore I fam.
I'll give you a beating like Rodney King who deserved it!=====================================Any and all ideas, theories, and text c2004,c2009 BrainDead Software. All Rights Reserved.
Advertisement
if you have an object:
struct obj{ int *ptr; obj () {  ptr = new int[32]; }}

you must delete the memory you allocated in this object. if you
don''t do, you have a memory leak:
obj *o_ptr = new obj (); // allocationdelete [] o_ptr->ptr;    // deallocationdelete o_ptr;            // deallocation

you could do this by defining a destructor, i think:
obj::~obj (){  if (ptr)  {   delete [] ptr;   ptr = (int *)0;  }}
You should give your struct a destructor to call delete[] on each of those pointers. That way your class can simply call delete on the struct and it will carry through to delete the pointers.
____________________________________________________________AAAAA: American Association Against Adobe AcrobatYou know you hate PDFs...
thanx As for aliases, I guess that''s what pointers are for

I fseek, therefore I fam.
I'll give you a beating like Rodney King who deserved it!=====================================Any and all ideas, theories, and text c2004,c2009 BrainDead Software. All Rights Reserved.
Look up references (AKA aliases, now there''s a pun ).

______________________________________________________________
The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ
MySite
______________________________________________________________
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________

This topic is closed to new replies.

Advertisement