Overloading new And delete Operator In C++

  Рет қаралды 23,699

CppNuts

CppNuts

Күн бұрын

Пікірлер: 61
@hardiksavsaviya659
@hardiksavsaviya659 3 жыл бұрын
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
@RahulBhadana_cse
@RahulBhadana_cse 6 жыл бұрын
Thank you very much for uploading this video.👍
@CppNuts
@CppNuts 6 жыл бұрын
You are welcome dude!!
@yogendragangwar9795
@yogendragangwar9795 6 жыл бұрын
Your videos are very help full for interview, Excellent explanation
@CppNuts
@CppNuts 6 жыл бұрын
Thanks for your comment!! It helps a lot!! 😀
@sajidriyaz2933
@sajidriyaz2933 4 жыл бұрын
Beautifully explained.
@CppNuts
@CppNuts 4 жыл бұрын
Glad it was helpful!
@sudhirpratapsingh5730
@sudhirpratapsingh5730 5 жыл бұрын
Thanks man, you saved my day .. nice explanation.
@rahul-patil
@rahul-patil 3 жыл бұрын
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.
@bharathupadhya
@bharathupadhya 5 жыл бұрын
very nice video bro..thanks for creating these videos..
@amritkanoi8804
@amritkanoi8804 3 жыл бұрын
11:23, you need to overload explicitly new[ ] when you are creating an array of non-primitive type objects
@sumedh_3457
@sumedh_3457 4 жыл бұрын
Thanks a lot for the video
@CppNuts
@CppNuts 4 жыл бұрын
My pleasure
@KNT_19
@KNT_19 3 жыл бұрын
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.
@rajeshgowda6884
@rajeshgowda6884 6 жыл бұрын
Very good explanation..superb
@CppNuts
@CppNuts 6 жыл бұрын
Thanks man!!
@feraudyh
@feraudyh 4 жыл бұрын
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 4 жыл бұрын
Smart pointers?
@feraudyh
@feraudyh 4 жыл бұрын
@@CppNuts I've got legacy code. I don't know much about the strategy of upgrading to smart pointers.
@rahulr9301
@rahulr9301 2 жыл бұрын
excellent video brother!!!!...Thanks much. i have one doubt can we return NULL from overloaded new operator??
@avishekdutta1901
@avishekdutta1901 4 жыл бұрын
*static_cast=val; what is the use of the two '*' symbol here... Plzz explain!!!!!!
@CppNuts
@CppNuts 4 жыл бұрын
Why int*p only int* is needed.
@hsaidinsan6345
@hsaidinsan6345 4 жыл бұрын
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 4 жыл бұрын
@@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?
@ravivemisetti248
@ravivemisetti248 3 жыл бұрын
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
@checheromo7669
@checheromo7669 6 жыл бұрын
Thanks for this video
@shashikant.jadhav29
@shashikant.jadhav29 6 жыл бұрын
very good explanation...
@CppNuts
@CppNuts 6 жыл бұрын
Thanks man!!
@vvpChannel3112
@vvpChannel3112 3 жыл бұрын
Hi Rupesh sir.. Can we allocate memory using malloc and deallocate using delete ?
@vikramm4967
@vikramm4967 4 жыл бұрын
only the first index is assigned as the given val in case of array, how to assign each and evey index of the array?
@vima9046
@vima9046 2 жыл бұрын
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?
@shubhamshakya4741
@shubhamshakya4741 4 жыл бұрын
Please sir tell me a book of C++ or a website from where i can solve programs for practice??
@aashishgoyal1436
@aashishgoyal1436 6 жыл бұрын
thanks a lot
@CppNuts
@CppNuts 6 жыл бұрын
Thanks for your comment Aashish Goyal, and welcome to my channel, keep learning!!
@aashishgoyal1436
@aashishgoyal1436 6 жыл бұрын
@@CppNuts Thanks a lot Rupesh Sir. Ur videos have helped me a lot during c++ interviews
@CppNuts
@CppNuts 6 жыл бұрын
Great...
@ramkrushnashelke5894
@ramkrushnashelke5894 4 жыл бұрын
How can i use both placement new and overload new operator?
5 жыл бұрын
Sir we are not casting void pointer to class. Than how it's working
@AbhijeeetKumarSrivastavakshiva
@AbhijeeetKumarSrivastavakshiva 3 жыл бұрын
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?
@CppNuts
@CppNuts 3 жыл бұрын
Yes it should delete only one.
@AbhijeeetKumarSrivastavakshiva
@AbhijeeetKumarSrivastavakshiva 3 жыл бұрын
@@CppNuts Thanks for your response :)
@yasirarafat9946
@yasirarafat9946 3 жыл бұрын
How can I get full course ?
@CppNuts
@CppNuts 3 жыл бұрын
Full course on?
@bhoomitraders1599
@bhoomitraders1599 5 жыл бұрын
can you write the program without malloc and free functions....!!!!
@CppNuts
@CppNuts 5 жыл бұрын
Meaning??
@bhoomitraders1599
@bhoomitraders1599 5 жыл бұрын
@@CppNuts how malloc and free works in our programs?
@aniketahir101
@aniketahir101 4 жыл бұрын
then you should know system calls
@bhoomitraders1599
@bhoomitraders1599 4 жыл бұрын
Yes I want to know Bout this. Could you please make a video with system calls
@thejask2765
@thejask2765 4 жыл бұрын
@@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
@sumitkumar-gn1oe
@sumitkumar-gn1oe 4 жыл бұрын
iostream inside new function is not suggested
@aryanagrawal3719
@aryanagrawal3719 Ай бұрын
Well malloc() is not used . Operator new is used.
@narayanareddybattula5465
@narayanareddybattula5465 4 жыл бұрын
#include using namespace std; void* operator new[] (size_t size) { cout
@umairalvi7382
@umairalvi7382 5 жыл бұрын
Nothing happens magically there is a reason behind.
@CppNuts
@CppNuts 5 жыл бұрын
For what?
@umairalvi7382
@umairalvi7382 5 жыл бұрын
@@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 5 жыл бұрын
@@umairalvi7382 my question is also same.. if you find answer please comment it
@aniketahir101
@aniketahir101 4 жыл бұрын
@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 4 жыл бұрын
@@aniketahir101 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.
Alternative Operators In C++
2:59
CppNuts
Рет қаралды 3,4 М.
Overloading Function Template In C++
19:32
CppNuts
Рет қаралды 14 М.
24 Часа в БОУЛИНГЕ !
27:03
A4
Рет қаралды 7 МЛН
ВЛОГ ДИАНА В ТУРЦИИ
1:31:22
Lady Diana VLOG
Рет қаралды 1,2 МЛН
Assignment Operator Overloading In C++
16:26
CppNuts
Рет қаралды 39 М.
Assignment Operator Overloading In C++
13:14
CppNuts
Рет қаралды 14 М.
Bit Fields In C & C++
20:36
CppNuts
Рет қаралды 22 М.
AI Is Making You An Illiterate Programmer
27:22
ThePrimeTime
Рет қаралды 224 М.
DeepSeek’s AI LIE? The $5.6M Hoax That Shook the Market!
12:54
sin(x+y)°=?
11:15
Mathematopia
Рет қаралды 51
DEEPSEEK Vs CHATGPT There Is A  Clear Winner !!
15:53
Rick Aqua
Рет қаралды 16 М.
MALAHIT DSP2  &  MALACHITE SDR , V5
12:45
HAMTech RADIO SCANNER M0FXB CB DRONE HOBBY Diary
Рет қаралды 99
24 Часа в БОУЛИНГЕ !
27:03
A4
Рет қаралды 7 МЛН