Overloading new And delete Operator In C++
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
Thanks man, you saved my day .. nice explanation.
Thank you very much for uploading this video.đ
You are welcome dude!!
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
very nice video bro..thanks for creating these videos..
Your videos are very help full for interview, Excellent explanation
Thanks for your comment!!
It helps a lot!! đ
Beautifully explained.
Glad it was helpful!
Thanks for this video
Very good explanation..superb
Thanks man!!
Thanks a lot for the video
My pleasure
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.
very good explanation...
Thanks man!!
11:23, you need to overload explicitly new[ ] when you are creating an array of non-primitive type objects
excellent video brother!!!!...Thanks much. i have one doubt can we return NULL from overloaded new operator??
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
only the first index is assigned as the given val in case of array, how to assign each and evey index of the array?
Hi Rupesh sir..
Can we allocate memory using malloc and deallocate using delete ?
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?
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.
Please sir tell me a book of C++ or a website from where i can solve programs for practice??
How can i use both placement new and overload new operator?
Sir we are not casting void pointer to class. Than how it's working
thanks a lot
Thanks for your comment Aashish Goyal, and welcome to my channel, keep learning!!
@@CppNuts Thanks a lot Rupesh Sir. Ur videos have helped me a lot during c++ interviews
Great...
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.
Smart pointers?
@@CppNuts I've got legacy code. I don't know much about the strategy of upgrading to smart pointers.
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?
Yes it should delete only one.
@@CppNuts Thanks for your response :)
*static_cast=val;
what is the use of the two '*' symbol here... Plzz explain!!!!!!
Why int*p only int* is needed.
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;
@@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?
iostream inside new function is not suggested
How can I get full course ?
Full course on?
#include
using namespace std;
void* operator new[] (size_t size)
{
cout
can you write the program without malloc and free functions....!!!!
Meaning??
@@CppNuts how malloc and free works in our programs?
then you should know system calls
Yes I want to know Bout this. Could you please make a video with system calls
@@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
Nothing happens magically there is a reason behind.
For what?
@@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 ?
@@umairalvi7382 my question is also same.. if you find answer please comment it
@umair alvi . when u store void pointer into any pointer container or pointer variable, it gets implicitely typecasted into the type of pointer variable.
@@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.