around 12:00 timestamp, when you called the destructor at each index for de-allocating bp object then only the memory occupied by a bp object is cleared. And in the end you just deleted the array then is it deleting the whole n indexes? If yes then how?
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!!
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?
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.
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!!
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 RUclips 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 RUclips'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.
Heh, I'm back re-watching your old content (thanks to RUclips 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.)
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?
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?
@@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.
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 ?
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?
@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?
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
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.
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.
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.
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.
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.
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.
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. :)
@@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.
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!!
around 12:00 timestamp, when you called the destructor at each index for de-allocating bp object then only the memory occupied by a bp object is cleared. And in the end you just deleted the array then is it deleting the whole n indexes? If yes then how?
Best Channel for learning cpp
Thank you so much
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 [ ]?
thank you so much. First time I learnt about delete[] concept..
You are welcome!!
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?
Is this replacement of code done by the precompiler or the compiler?
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.
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.
This is great. First time, i encountered 'placement new'
this series is good but the amount of ads is obnoxious. RUclips 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 RUclips 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 RUclips'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.
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!!!
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.
Heh, I'm back re-watching your old content (thanks to RUclips 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.)
Awesome explanation sir
Thanks for liking
You are awesome! Thank you! very good video
Thanks man..
Why adding function to a class/structure don't increase its size but adding instance variable increase it
You must have studied compiler. nice series, thanks
Yes in B.Tech i did.
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".
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.
can you explain the use of operator delete[] (bp)?
(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.
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 ?
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".
excellent job of describing the process, I'm sure this technique is applicable in application code.
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?
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
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
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.
How this works, when incrementing the pointer and using delete? int *p = new int; p++; delete p;
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.
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.
ruclips.net/video/KOMl2p49rvo/видео.html
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.
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.
How in line 23 size_t n = * (size_t) ((char*)bp -WORDSIZE); is 10. Should not it be 0.
Sir please explain again in simple language
please try to re-visit the video with little more focus.
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
Loved it
Glad you loved it :D
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?
Super....
Thanks man..
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.