Instantiating and deleting objects versus the malloc family

I did not want to hijack Temporary store variable

@RayLivingston, can you please explain? Is the use of instantiating an object and deleting it not the same as the use of the malloc family etc. and free? And has the same risks?

Sure, you can abuse new and delete. But the vast majority of "newbies" and a shocking number of alleged "experts" routinely write code using malloc() and free() that are recipes for disaster, by failing to check the return value of malloc(), failing to ever call free(), and others. With c++, there really is no reason to resort to such archaic, low level methods. It CAN be done safely using malloc and free, but why not simply learn the better, modern ways instead. And it will force you to learn classes, structs and other object methods, which are absolutely worth learning, as, used properly, you will create better, more reliable, more maintainable code.

'new' allocates the memory and builds the object using the class's constructor. It also returns a pointer of the proper type. 'malloc()' only allocates the memory, and it's return value must be cast to the appropriate type.

new / delete is also used by smart pointers that reduce the chance of memory leaks by de-allocating the memory when their lifetime ends.

Thanks for the replies

I'm not sure what a constructor will return if one runs out of memory, I assume NULL in which case that should be tested anyway. And calling delete can be forgotten as easily calling free. The only advantage might be that the destructor is automatically called if the object goes out of scope (as indicated by @gfvalvo).

That's a fair point. But I have lived with that for many years :wink:

@gfvalvo

would it be possible to have a good arduino example of how to use those if you know any?

been using 'malloc' occationally but not really familiar with how to use 'new' and 'delete' correctly!

you never too old to learn something new! :wink:

@sterretje, @sherzaad:
First, it's absolutely just as easy to forget to invoke 'delete' as it is free(). The recommended defense against that is to handle with raw pointers inside a class so that you can put the call to 'delete' in the class's destructor.

I suppose you could do that with malloc() / free() also. So, it could be argued that the only real advantages of new / delete are (decreasing importance, IMO):

  • Invokes the class's constructor.

  • Returns pointer of correct type.

  • It's the correct, "C++ Way" to do it.

An extension of the "handle raw pointers in a class" idea is the smart pointer concept. Here's A Tutorial

As a further aside:

I intentionally used the term "lifetime" rather than "scope" as there's an important difference:

void Function1() {
  uint32_t localVariable;

  // localVariable is IN SCOPE here
  Function2(); // <----- call Function2
  // localVariable is IN SCOPE here
  
} // localVariable's LIFETIME ends here

void Function2(){
  // localVariable is still "alive", but is OUT OF SCOPE here
}
1 Like

Your example made it clear, thanks for that.

Normally that would be an exception. The AVR core changes the behaviour to return NULL.

1 Like

I suspected that, thanks.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.