[Solved] C++ how to delete a structure?

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?

Enquirer: Rella


Solution #1:

delete newVideSample;

This won’t free any memory you allocated to newVideoSample->buffer though – you have to free it explicitly before deleting.

//Free newVideSample->buffer if it was allocated using malloc

//if it was created with new, use `delete` to free it
delete newVideSample->buffer;

//Now you can safely delete without leaking any memory
delete newVideSample;

Normally this kind of freeing is written in the destructor of the class so that it’ll be called automatically when you delete the dynamically created object.

Thanks @steve for mentioning it 🙂

Respondent: Amarghosh

Solution #2:

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.

Respondent: aschepler

Solution #3:

You’re looking for the delete keyword:

delete newVideoSample;
Respondent: SLaks

Solution #4:

delete newVideoSample;

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

std::auto_ptr<VideoSample> newVideoSample(new VideoSample);
Respondent: vitaut

Solution #5:

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

delete newVideoSample;
Respondent: Justin Niessner

Solution #6:

delete newVideoSample .
In C++ struct is the same as class but with default public fields.

Respondent: Abyx

Solution #7:

Use delete

VideoSample * newVideoSample = new VideoSample;
//.. stuffs

delete newVideoSample;

There is also an overload i.e delete[]

VideoSample * newVideoSample = new VideoSample[n];
//.. stuffs

delete [] newVideoSample;

In Modern C++ it is always recommended to use smart pointers. You may want to use boost::shared_ptr<T> from the boost library.

Respondent: Prasoon Saurav

Solution #8:

If you intended VideoSample to free its buffer member then VideoSample is a fragile class. It has no way of knowing whether buf was created in the heap using new[] or malloc, or is the address of a variable on the stack.

Respondent: dripfeed

Solution #9:

In C++ a structure is the exact same as a class except everything is public by default, where a class is private by default. So a structure can have a destructor and is freed with delete.

Respondent: robev

Solution #10:

To Allocate ->
VideoSample * newVideoSample = new VideoSample;

To Delete ->
delete newVideoSample;

If you deleting the object in the same context, you better just allocate it on the stack.
If you deleting it outside the context don’t forget to pass a reference.

And most important, don’t delete if your about to exit process, it’s pointless 😛

Respondent: djTeller

Solution #11:

**You create object of Videosample , so you just use delete..

VideoSample * newVideoSample = new VideoSample;
delete newVideoSample;**

Respondent: Satyam

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.