Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

drarem

delete[] and aliasing questions

This topic is 5478 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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 (); // allocation

delete [] o_ptr->ptr; // deallocation

delete o_ptr; // deallocation


you could do this by defining a destructor, i think:

obj::~obj ()
{
if (ptr)
{
delete [] ptr;
ptr = (int *)0;
}
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Look up references (AKA aliases, now there''s a pun ).

______________________________________________________________
The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ
MySite
______________________________________________________________

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!