1

Let's suppose I've to create a class with this structure

 class A
 {
     int a;
     B* b;
 }

I read that it's a good thing deallocate the variable used in the deconstructor.

~A
{
   delete b;
}

but how I have to do for a variable of type int, double, primitive type in general? What's the best way or a good programming rule?

Paul R
  • 208,748
  • 37
  • 389
  • 560
user2896152
  • 762
  • 1
  • 9
  • 32
  • 1
    what do you mean "deallocate primitive type"? Why do you think you need to do anything with it at all? – UnholySheep Sep 08 '17 at 09:19
  • 2
    You could use [smart pointers](https://stackoverflow.com/questions/106508/) rather than managing memory manually. – flau Sep 08 '17 at 09:20
  • 4
    I recommend you follow [the rule of zero](http://en.cppreference.com/w/cpp/language/rule_of_three#Rule_of_zero), so you don't have to manage memory manually. – Some programmer dude Sep 08 '17 at 09:21
  • 2
    As for your problem, you `delete` what you `new`, and `delete[]` what you `new[]`. That's about it. In your example, nothing needs to be done with `A::a` since you haven't explicitly allocated memory for it. – Some programmer dude Sep 08 '17 at 09:22
  • 1
    The general rule for built-in types (`int`, `double`, etc) that are non-static class members is that destructors need do nothing to release them. They are part of the object itself, start to exist when the object does, and cease to exist with the object. It is only explicitly managed resources (e.g. pointers that are initialised using operator `new`) that need to be released (e.g. using operator `delete`). And, BTW, there is no such thing in C++ as a deconstructor. – Peter Sep 08 '17 at 10:26
  • A corollary to @Someprogrammerdude's advice: if you `new` nothing, you `delete` nothing (similarly never using `new[]` and `delete[]`, `malloc` and `free` etc). Then all the memory is managed by the rules of the language – Caleth Sep 08 '17 at 10:37

4 Answers4

4

You don't need to delete basic types.

But not just for basic types. For anything that you didn't allocate with new you don't have to call delete on. Even for pointers.

Abhishek Aryan
  • 19,936
  • 8
  • 46
  • 65
0

You may allocate primitives types on the heap (new ...) but usually this useless.

The general rule is that you may use new with any type (int, double, or your own types), and you must issue a delete for each variable allocated on the heap with new.


However as you are asking for best practices you should read articles or tutorials on smart pointers (std::shared_ptr, std::unique_ptr) which help you avoid mistakes when using heap-allocated objects. You may also search for articles talking about lifetime and borrowing.

Finally if you want to know what are the recommend practices to write good and safe C++ you definitely should read the isocpp Guidelines : http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines. They describe in simple words what you should do or shouldn't do, why and how.

AMDG
  • 955
  • 1
  • 9
  • 25
0

In c++ for every new there should be a delete.

When new is called you are allocating on the heap and you are responsible for deallocating it. If no new is used, the memory is allocated on the stack and will be deallocated upon going our of scope.

Please read this and this. There are plenty of information on this topic.

Griffin
  • 716
  • 7
  • 25
0

I assume that B is another class. You just declared the pointer. But not instantiated yet. You have not allocated any memory to that pointer.So initialize it to null.

 B* b = NULL;

Now before deleting it in the deconstructor check whether you have allocated any memory or not.

if(b){delete b; b = NULL;}

In the above code if you allocate any memory then only it will deallocate. for eg: if you did

*b = new b

now some memory is allocated for this particular pointer. So it will deallocate properly. You don't need to deallocate unless until you allocate some memory to pointer. Hope this will help you.

dfour
  • 1,376
  • 1
  • 12
  • 16
SOORAJ M
  • 21
  • 7