Alex Rivera | Logout

C++ how to delete a structure?

Asked 2010-11-09T13:47:49.387
22

Structure I created:

   struct VideoSample
  { 
      const unsigned char * buffer;
      int len;
  };

   VideoSample * newVideoSample = new VideoSample;
   newVideoSample->buffer = buf;
   newVideoSample->len = size;

       //...

How now to delete it now?

Edit
Report

3 Answers

8
delete newVideoSample;

But if the new and delete are in the same context, you're probably better off skipping them and just creating it on the stack instead:

VideoSample newVideoSample = {buf, size};

In that case, no cleanup is necessary.

answered 2010-11-09T13:49:59.297
3

delete newVideoSample;

However, consider using a smart pointer that will release the memory automatically, for example:

std::auto_ptr<VideoSample> newVideoSample(new VideoSample);
answered 2010-11-09T13:50:32.100
2

Unless I'm missing something, you just use delete:

delete newVideoSample;
answered 2010-11-09T13:49:43.433

Your Answer