How delete[] Knows How Much To Deallocate In C++?
Vložit
- čas přidán 28. 08. 2024
- 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
The point is when we say delete[] to deallocate memory from given array then how does delete[] get to know that how much it needs to deallocate, because we don't tell the size of array RIGHT??
Watch this video and i am sure you will enjoy it a-lot.
#cpp #interviewquestions #programming #tutorial #coaching #beginners #computerscience #softwareengineering
Hi everyone, Don't forget to hit LIKE and SUBSCRIBE button for more videos like this!!
And this will help me a-lot.
I am really passionate about understanding deep concepts of C++ and how it works internally. You are really a great help, keep doing what you love.
Thanks man!!
But your explanation and idea of lessons is really spectacular. Bloody spectacular:)
Respect to ur knowledge!
Thanks dude!!
this series is good but the amount of ads is obnoxious. CZcams ads are horrible at the best of times without there being one every three minutes
Hi Trey Quattro,
CREATING ONE VIDEO TAKES:
a. Thinking about videos.
b. Preparing for videos (making notes so i don't miss anything).
c. Shooting videos (with so many reshoots because of noise i have to deal with)
d. Editing Videos (If video is of 10 min, it takes at least 50 - 60 min)
e. Replying to comments (trying to solve doubts in comments is hard and takes time too).
So it is very much time taking process and in return CZcams gives very little amount (really very small). And all it takes for you is to wait for 5 sec and press skip button and in return you get free knowledge (I think this is really a good deal).
If you still feel it is bad, then i am sorry for the pain and thanks for watching!!
I understand the effort that goes into making videos: I've done it too; that's just the way it is, if you want to put your work out. This video is 12:24 in length. There are 7 ad spots on the timeline, at least on the version I'm seeing. One or two ads, at the ends, is OK, but too many turns people off. Part of the problem is CZcams's algorithm, or lack of one, that injects ads at the most inappropriate points, and with no regard to flow or interrupting important sections. I understand the desire to monetize content, but there's a lot of competition on this platform that doesn't include excessive ad interruptions. It's a turn off, simply, and I tend to move on when it feels I'm being exploited to have to wait for ads which in my opinion are useless anyway: who has ever bought something because of an online ad? It's the perennial issue with sales: get more money from a few big sales, or many smaller ones? Good luck finding the sweet spot.
Yes it is correct that waiting for ads to complete makes feel bad but not all the time you will get these ads some time you will get banner ads you can close them without any issue.
Hardly 2-3 ads will come which you may have to wait for 5 sec to skip.
Best Channel for learning cpp
Thank you so much
In the first method please explain this " *(size_t*)temp = n; " and
in the second method please explain this " size_t i; "
in detail .
Thanks , this was great!!
+Shivam Sharma size_t is 4 byte, its size is the size of your processor like 32 or 64.
thank you so much. First time I learnt about delete[] concept..
You are welcome!!
This is great. First time, i encountered 'placement new'
Heh, I'm back re-watching your old content (thanks to CZcams algorithm). Guess what? Number of ads has gone way down!
I noticed something - when you fetch the size from the associative array (hash table/hash set/unordered map/map) you forgot to erase the entry. I know it's just an example, but ... (Also: allocating WORDSIZE and then storing a size_t should be the same types, in case it changes on a future version or different compiler - e.g. you port to a 16-bit word size environment, but size_t is still 32-bit. Same with char* - should be uint8_t* just in case char size changes to 16-bits (Unicode default) e.g. Unlikely, but these little picky details are what can trip one up in production code.)
Why do we in India only have to focus on interview questions? Seems to me everything we do is only to get a job or make money. One reason our country is lagging behing so much in R&D. Makes me really sad looking at this poor attitude towards things
Problem is not India , problem is this @CppNuts guy. He should have made videos naming "important concepts of c++" and not "interview questions of c++". If concepts are clear any interview can be cracked. And btw what do you do @Rahul Jethwani ? Its easy to write a comment.
In a country with population almost 130 crores one needs a job first to survive, then only he can explore. Not everyone has a strong enough background to go towards his/her passion. So you tell me whats's wrong in putting the name Interview questions??
Actually if you see search keywords people look for interview questions not important questions, so its just demand and supply.
People are not the problem in India its the population, make it less and everything will fall in place.
why we are typecasting "" operator new[] (WORDSIZE + n * sizeof(Base))"" to char*
why we need to typecast here
and why we need char* in the above line
If you want to perform calculation on bytes then type casting it to char ptr will help because if u increment it by one it will increase the address by one byte and we can calculate things easily.
You must have studied compiler. nice series, thanks
Yes in B.Tech i did.
Just confirming is it rightly written before "return 1" you have used delete[ ] to delete base address only. As the complete topic was to describe delete [ ]?
During delete[] bp;
how compiler knows which class destructor to be called? in this example (p+n) ->~Base(); is called. Suppose program has 2 classes. WordSize will have only information of size of memory, not of which class information.
Hi Naveen Rai good observation.
Example:
Mango man;
Orange org;
man = org;
it is not going to compile because both type is different or you have not given rule for that right?
in same way the compiler is knowing the type of delete[] dp; as it knows about "org" and "man".
Compiler has list(symbol table) of variables/objects along with their types.
Hope helped!!
thank you :)
You are most welcome.
1)So we have to give the original pointer to delete , so that it'll be deleted correctly ( like if we increment or decrement it ,and then give to delete will it cause an error )?
2) can some other pointer be declared of same type as bp and made to point to same location as bp and given to delete will it delete correctly?
Thanks for the video, can you please explain which method will be chosen by the compiler ? is it randomly chosen or it can be mentioned?!
Now I think it is over allocation technique.
Thanks again, will share about your channel with my friends, hope to see more from you. Keep rocking.
Thanks man!!!
I think it still doesn't answer the question. It tells us how the size is stored and how the destructors are called. But at the end it is still calling delete [] only. Can you please explain how at the end calling delete [] does what is required ?
Hi
Base* bp = new Base;
This would be deleted by delete
Now I am confused about Base* bp = new Base[10];
It shouldn't be a double pointer like Base** bp?
new Base[10] would still return pointer of the first memory location. So no need of double pointer. you are not taking address of pointer.
excellent job of describing the process, I'm sure this technique is applicable in application code.
Thank you so much for the videos I learned alot from them..
I was asked an interview question.. How compiler manages function overloading and how correct function gets executed.
As C doesn't support function overloading, is there any other way to implement the concept in C?
yes, compiler does this by name mangling concept where it stores info in symbol table about the overloaded function. Please google "name mangling".
You are awesome! Thank you! very good video
Thanks man..
How does the compiler decide which method(associative array method or overallocation method) to use while implementing this delete [] concept?
Is there any version dependance in implementing above like C++11 and next will use a certain concept etc.
AFAIK almost every compiler uses over allocation method.
Is this replacement of code done by the precompiler or the compiler?
That's a great explanation.
Also I would like to add something to it.
I think this might not be the case with delete[] always.
The compiler will store the value before the actual memory pointer returned, only in case if it is required to call destructor. And in such cases the actual free is called with the byte inclusive.
But in cases of primitive types and in cases where there is trivial destructor, it doesn't do that, in fact there is necessity to do that.
Awesome explanation sir
Thanks for liking
Thank you for the awesome concept.. I have a doubt on 2nd type. After deleting bp (line 24) are not we removing the entry from associateArray? Is associateArray a vector? Is it not growing? what is it's size?
didn't get the question.
@prithvi Madyastha No, it is not a vector , i think its an Associative Dictionary Container like std::map(not map exactly , could be operating system specific container), and looks up the pointer in it with a O(1) complexity and gets the value of "n".
In the over allocation part of explanation, last line //operator delete[]( (char*)bp - WORDSIZE); i didn't understand.
Again how come the compiler would know how much memory it have to deallocate?
Why adding function to a class/structure don't increase its size but adding instance variable increase it
(please explain the below
operator delete[]((char*)bp - WORDSIZE); // What this line explains. Please elaborate.
Thanks for detailed info.
This is internal function call which deletes the memory created by dynamic memory allocation. (this will actually delete array of memory)
@@CppNuts A follow up question on this. How does compiler knows what is the length of the memory allocated?
@@jagdishagrawal9389 I don't want to sound rude, but that is the point of the video. The compiler either stores the length of the array at the beginning of the memory, or it keeps track of it in a platform specific associative array.
How this works, when incrementing the pointer and using delete? int *p = new int; p++; delete p;
Hi
can someone explain this question.
How can we restrict dynamic allocation of objects of a class using new? this question was there in geeksforgeeks i did not understand it.
can someone help me to understand this.
thank you
Hi, CppNuts
I am a little bit confused. Can you please explain the meaning of *(size_t*)
It's nothing, just typecasting pointer to size_t* and deref with *, so it will become like 4byte size_t object to assign int n.
If you type cast to char* you could store char not int.
can u please ...let us know ..how free() know how much deallocate ?is this same as c++?but there is concept like destructor in c...
As I said in this video there are two techniques so it will use any technique. And if you are confused with destructor part then, there is no need to call destructor it will directly free those many bytes how much was requested.
can you explain the use of operator delete[] (bp)?
what is happening in " operator delete[](bp) " ?
in line 21, why you are not using *tmp, it was holding address (from lets say 0-43). instead of subtracting 4 from *p? Please explain if it could have led to any error.
here you see directly that above array is created and below it is being deleted but lets assume you created array in some function but you are deleting that array in some other function, there you wont have access to tmp variable, there you have your original pointer which is p(actually i should have kept the pointer name similar but i kept p instead of bp, so if you will keep bp in place of p every where you will understand it, and the second function where you are suppose to delete this bp pointer there you don't have access to tmp).
I hope it will help to understand.
Yes, true. Didn't think that way. Thank you
How in line 23 size_t n = * (size_t) ((char*)bp -WORDSIZE); is 10. Should not it be 0.
I need to understand PIMPL idiom of c++ on urgent basis sir..please help
Hi Radhika, for now plz go for this video. It is really good explanation.
czcams.com/video/KOMl2p49rvo/video.html
Loved it
Glad you loved it :D
Super....
Thanks man..
second last line
operator delete[] ((char *)p - WORDSIZE);
You again used delete[] which you were explaining. How this delete[] will get to know how much to release
Thanks for pointing out this..
Actually it is operator delete [] which is different then simply delete []. I think i should reshoot this video again. thanks again and yessss this is the actual function call which release the memory, i would explain in detail wait for sometime. :)
yeah, I am waiting
@@CppNuts Is this second video already released?
@@CppNuts Hi is new video uploaded?
how you calculated size_t n , i don't understand
p is pointing to wordsize+temp
now we are subtracting wordsize again, so it should reach at start of array.
thanks
Sir please explain again in simple language
please try to re-visit the video with little more focus.
It is not clear to me.
Explanation is not good
No one used this crap in modern c++
Actually you don't need to know how it works but it is very famous in interview questions.
@@CppNuts , in my career with C++ , I gave few interviews (in Europe), they ask you more STL , threads, and design patterns instead of these low level things, because these kinds of codes are more used in legacy c++ or low level things , but I admit knowing low level things are good for your development.