Overloading new And delete Operator In C++

SdĂ­let
VloĹžit
  • čas přidĂĄn 15. 10. 2018
  • JOIN ME
    -----
    CZcams 🎬 / @cppnuts
    Patreon 🚀 / cppnuts
    COMPLETE PLAYLIST
    ------------
    C++ Tutorial For Beginners: • Introduction To C++
    STL (Standard Template Library): • STL In C++
    ThreadIng In C++: • Multithreading In C++
    Data Structures: • Data Structure
    Algorithms: • Binary Search
    Design Patterns: • Factory Design Pattern...
    Smart Pointers: • Smart Pointer In C++
    C++14: • Digit Separator In C++
    C++17: • std string_view in C++...
    C++ All Type Casts: • static_cast In C++
    INTERVIEW PLAYLIST
    ------------
    C++ Interview Q&A: • Structural Padding & P...
    C++ Interview Q&A For Experienced: • How delete[] Knows How...
    Linked List Interview Questions: • Find Kth Node From Bac...
    BST Interview Questions: • Search Element In Bina...
    Array Interview Questions: • Reverse An Array
    String Interview Questions: • Check String Is Palind...
    Bit Manipulation Questions: • Find Set Bit In Intege...
    Binary Tree Interview Question: • Invert Binary Tree
    Sorting Algorithms: • Bubble Sort
    C++ MCQ: • Video
    C MCQ: • What printf returns af...
    C Interview Questions: • Designated Initializat...
    QUICK SHORT VIDEOS
    -------------
    C++ Short : • C++ Short Videos
    C Short : • Shorts C Programming MCQ
    In this video we will learn about overloading operator new and operator delete in C++ programming language. Basically overloading new and delete operator gives developer more flexibility for heap allocation.
    There are few points to note as follows :
    1 There are two different types of overloading.
    a. Local Overloading (class/struct)
    b. Global Overloading
    2. Syntax for Overloading operator new
    void* operator new (size_t size);
    3. Syntax for Overloading operator delete
    void operator delete(void*);
    4. Both new and delete operator are static members functions. So they don't have access to this pointer (we don't have to write static keyword it is automatically static).
    5. Don't forget to overload for array version of both (new/delete).
    6. We can have multiple overloaded new/delete operator functions.
    #cpp #programming #tutorial #computerscience #softwareengineering

Komentáře • 60

  • @sudhirpratapsingh5730
    @sudhirpratapsingh5730 Před 4 lety +1

    Thanks man, you saved my day .. nice explanation.

  • @RahulBhadana_cse
    @RahulBhadana_cse Před 5 lety +7

    Thank you very much for uploading this video.👍

    • @CppNuts
      @CppNuts  Před 5 lety

      You are welcome dude!!

  • @hardiksavsaviya659
    @hardiksavsaviya659 Před 2 lety +1

    Nicely you have explained, covered major points.
    I have few points which I want to add here to make your content more beautiful.
    1. usually when we create a dynamic memory for a user-defined data type using a new operator constructor is automatically called but when dynamic memory is allocated using malloc it dose not call to the constructor. but with your example after used malloc constructor is called.
    Reason: malloc is called inside new. first new is executed by the compiler and due to the new operator compiler its self call to constructor. This means the new operator only does memory creation, but the new keyword informs the compiler to call the constructor for the user-defined data type.
    2. if we overload the delete operator inside the class then we declare as void operator delete( void *p, size_t sz);
    here the second argument says what size of memory you are going to deallocate

  • @bharathupadhya
    @bharathupadhya Před 4 lety +2

    very nice video bro..thanks for creating these videos..

  • @yogendragangwar9795
    @yogendragangwar9795 Před 5 lety +4

    Your videos are very help full for interview, Excellent explanation

    • @CppNuts
      @CppNuts  Před 5 lety +1

      Thanks for your comment!!
      It helps a lot!! 😀

  • @sajidriyaz2933
    @sajidriyaz2933 Před 4 lety +1

    Beautifully explained.

    • @CppNuts
      @CppNuts  Před 4 lety

      Glad it was helpful!

  • @checheromo7669
    @checheromo7669 Před 5 lety

    Thanks for this video

  • @rajeshgowda6884
    @rajeshgowda6884 Před 5 lety +1

    Very good explanation..superb

  • @sumedh_3457
    @sumedh_3457 Před 4 lety +1

    Thanks a lot for the video

  • @rahul-patil
    @rahul-patil Před 3 lety +2

    11:23 When operator new and operator new[] (Array version) are doing the same task when there is a need to overload the new operator with array version?
    Thanks for a clear explanation.

  • @shashikant.jadhav29
    @shashikant.jadhav29 Před 5 lety +1

    very good explanation...

  • @amritkanoi8804
    @amritkanoi8804 Před 3 lety

    11:23, you need to overload explicitly new[ ] when you are creating an array of non-primitive type objects

  • @rahulr9301
    @rahulr9301 Před 2 lety

    excellent video brother!!!!...Thanks much. i have one doubt can we return NULL from overloaded new operator??

  • @ravivemisetti248
    @ravivemisetti248 Před 2 lety

    Thanks for the videos, I have few doubts
    1. While overloading new, default cnstor not called, in case of delete it is called default destructor.
    2. If I added virtual fun it is giving error. Please clarify if possible

  • @vikramm4967
    @vikramm4967 Před 4 lety

    only the first index is assigned as the given val in case of array, how to assign each and evey index of the array?

  • @vvpChannel3112
    @vvpChannel3112 Před 2 lety

    Hi Rupesh sir..
    Can we allocate memory using malloc and deallocate using delete ?

  • @vima9046
    @vima9046 Před 2 lety

    Hello! I am used to using c# and i am new in c++, can you (or someone here) explain me why after the constructor "Text(x=0)" you put ":x{x} {}"? What does this syntax mean?

  • @KNT_19
    @KNT_19 Před 2 lety

    Basic question. Why to overload new operator? If we have an user defined class, then why to overload new operator for that class? In any way, user defined class's constructor will be called and in constructor we do all initializations.
    Can you give sample code(for user defined class) where actual usage of overloading new is required.

  • @shubhamshakya4741
    @shubhamshakya4741 Před 4 lety

    Please sir tell me a book of C++ or a website from where i can solve programs for practice??

  • @ramkrushnashelke5894
    @ramkrushnashelke5894 Před 3 lety

    How can i use both placement new and overload new operator?

  •  Před 4 lety

    Sir we are not casting void pointer to class. Than how it's working

  • @aashishgoyal1436
    @aashishgoyal1436 Před 5 lety +1

    thanks a lot

    • @CppNuts
      @CppNuts  Před 5 lety +1

      Thanks for your comment Aashish Goyal, and welcome to my channel, keep learning!!

    • @aashishgoyal1436
      @aashishgoyal1436 Před 5 lety +1

      @@CppNuts Thanks a lot Rupesh Sir. Ur videos have helped me a lot during c++ interviews

    • @CppNuts
      @CppNuts  Před 5 lety

      Great...

  • @feraudyh
    @feraudyh Před 3 lety +1

    Can I use an overloaded delete/new pair to check I am not doing a double delete?
    One idea that comes to mind is to store the set of deleted pointers every time I do a delete, and use this to check if I am not trying to delete twice.
    However if I have a set of classes designed by others that do deletes, I am probably not overloading the delete that this library calls.

    • @CppNuts
      @CppNuts  Před 3 lety

      Smart pointers?

    • @feraudyh
      @feraudyh Před 3 lety

      @@CppNuts I've got legacy code. I don't know much about the strategy of upgrading to smart pointers.

  • @AbhijeeetKumarSrivastavakshiva

    what happens If I allocate with int *prt new int[n]; and delete ptr??
    Will the n memory blocks be deleted or only one block will be deleted?

  • @avishekdutta1901
    @avishekdutta1901 Před 4 lety +1

    *static_cast=val;
    what is the use of the two '*' symbol here... Plzz explain!!!!!!

    • @CppNuts
      @CppNuts  Před 4 lety

      Why int*p only int* is needed.

    • @hsaidinsan6345
      @hsaidinsan6345 Před 4 lety +3

      Avishek Dutta
      This is because static_cast(p) will cast p to a pointer to int, and if you need to access its content (dereference it) you need to use * again, so it will be like this
      *(static_cast(p))= val;

    • @pradeepsandaruwan6118
      @pradeepsandaruwan6118 Před 3 lety +1

      @@CppNuts
      Sir, there is a error when deleting the dynamically allocated memory in a class, when delete is in the destructor in the same class eventhough we use the copy constructor?

  • @sumitkumar-gn1oe
    @sumitkumar-gn1oe Před 4 lety

    iostream inside new function is not suggested

  • @yasirarafat9946
    @yasirarafat9946 Před 2 lety

    How can I get full course ?

  • @narayanareddybattula5465
    @narayanareddybattula5465 Před 3 lety +1

    #include
    using namespace std;
    void* operator new[] (size_t size)
    {
    cout

  • @bhoomitraders1599
    @bhoomitraders1599 Před 4 lety +1

    can you write the program without malloc and free functions....!!!!

    • @CppNuts
      @CppNuts  Před 4 lety

      Meaning??

    • @bhoomitraders1599
      @bhoomitraders1599 Před 4 lety

      @@CppNuts how malloc and free works in our programs?

    • @aniketahir5578
      @aniketahir5578 Před 3 lety

      then you should know system calls

    • @bhoomitraders1599
      @bhoomitraders1599 Před 3 lety

      Yes I want to know Bout this. Could you please make a video with system calls

    • @thejask2765
      @thejask2765 Před 3 lety

      @@CppNuts I thought new() & delete() or malloc() & free() has to be used correspondingly to allocate and deallocate memory from heap. But this following program surprises me. Can you explain how can this possible with utmost clarity and without using generalised statements like this, here, that, there , it , this pointer , which pointer , that pointer etc. Here goes program segment.
      // CPP program to demonstrate
      // Overloading new and delete operator
      // for a specific class
      #include
      #include
      using namespace std;
      class student
      {
      string name;
      int age;
      public:
      student()
      {
      coutname = name;
      this->age = age;
      }
      void display()
      {
      cout

  • @umairalvi7382
    @umairalvi7382 Před 4 lety +1

    Nothing happens magically there is a reason behind.

    • @CppNuts
      @CppNuts  Před 4 lety

      For what?

    • @umairalvi7382
      @umairalvi7382 Před 4 lety +2

      @@CppNuts like you told the new operator calls the constructor magically.But anyway nice tutorial.However i have a query
      In the line:- Test *t=new Test();
      The overloaded new will return the void pointer but the type of *t is Test.How it is possible and why it is not throwing any error ?

    • @srinu571
      @srinu571 Před 4 lety

      @@umairalvi7382 my question is also same.. if you find answer please comment it

    • @aniketahir5578
      @aniketahir5578 Před 3 lety

      @umair alvi . when u store void pointer into any pointer container or pointer variable, it gets implicitely typecasted into the type of pointer variable.

    • @umairalvi7382
      @umairalvi7382 Před 3 lety

      @@aniketahir5578 try to do this with malloc() you will get an error.
      Int *p=malloc(sizeof(int));
      This will give you an error in c++ but not in C.In C implicit type casting is done here.