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?

vitaut
  • 49,672
  • 25
  • 199
  • 336
Rella
  • 65,003
  • 109
  • 363
  • 636

11 Answers11

34
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
free((void*)(newVideSample->buffer));

//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 :)

Amarghosh
  • 58,710
  • 11
  • 92
  • 121
  • fives me C2664 on free(newVideSample->buffer); line. something like it cannot convert const unsigned char into void * – Rella Nov 09 '10 at 14:16
  • 2
    This deserves a -1 - for use of `free` without knowing where the memory came from, and for not recommending this be done in destructor if pointer is set. Not downvoting, because no other answer even mentions this. – Steve Townsend Nov 09 '10 at 16:46
  • @Kabumbus I thought casting will happen automatically - cast buffer to (void*) and you'll be good to go. Also, use free/delete on the `buffer` only if you allocated it - if, as shown in the question, the buffer was initialized with a pointer that was already allocated (and used even after newVideSample is deleted), you need not and should not free/delete it – Amarghosh Nov 10 '10 at 05:37
  • most likely it should be delete [] for the buffer. but once again don't know. – Chubsdad Nov 16 '10 at 07:33
7
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.

aschepler
  • 70,891
  • 9
  • 107
  • 161
  • 1
    Yes by default there is no need to "new" or "delete" objects. Use it only if you really know why you need it. – Nikko Nov 09 '10 at 13:52
3

You're looking for the delete keyword:

delete newVideoSample;
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
3

delete newVideoSample;

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

std::auto_ptr<VideoSample> newVideoSample(new VideoSample);
vitaut
  • 49,672
  • 25
  • 199
  • 336
3

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.

robev
  • 1,909
  • 3
  • 23
  • 32
2

You created an object of Videosample , so you just need to use use delete..

VideoSample * newVideoSample = new VideoSample;
delete newVideoSample;
A P
  • 2,131
  • 2
  • 24
  • 36
Satyam
  • 1,672
  • 3
  • 20
  • 34
2

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

delete newVideoSample;
Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
2

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

Abyx
  • 12,345
  • 5
  • 44
  • 76
2

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.

Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
2

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.

dripfeed
  • 387
  • 1
  • 10
1

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 :P

djTeller
  • 515
  • 2
  • 5
  • 14
  • 1
    "don't delete if your about to exit process, it's pointless" **No.** Why was this at +2? At the very least, to explicitly `delete` is self-documenting and polite. More so, what if the OS omits to delete the object or one of _its_ resources? Also it ensures you avoid false positives in memory-leak analysers, etc. Really, `delete`ing isn't difficult, so just do it always. If you start excusing omission in situations that [Dr Evil fingers] """shouldn't""" matter, you're asking for trouble when you forget to `delete` in a situation where it _really does_. http://stackoverflow.com/q/677812/2757035 – underscore_d Feb 16 '16 at 19:01