How delete[] Knows How Much To Deallocate In C++?

Поделиться
HTML-код
  • Опубликовано: 13 янв 2025

Комментарии • 98

  • @CppNuts
    @CppNuts  5 лет назад +6

    Hi everyone, Don't forget to hit LIKE and SUBSCRIBE button for more videos like this!!
    And this will help me a-lot.

  • @aditya.ishan27
    @aditya.ishan27 6 лет назад +10

    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.

    • @CppNuts
      @CppNuts  6 лет назад +1

      Thanks man!!

  • @druidclash9161
    @druidclash9161 6 лет назад +5

    But your explanation and idea of lessons is really spectacular. Bloody spectacular:)

  • @prasadbarude3766
    @prasadbarude3766 6 лет назад +12

    Respect to ur knowledge!

  • @AkhileshSingh-y4v
    @AkhileshSingh-y4v Месяц назад

    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?

  • @Pranav-pl7eg
    @Pranav-pl7eg 2 года назад

    Best Channel for learning cpp
    Thank you so much

  • @kapkaush5825
    @kapkaush5825 2 года назад

    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 [ ]?

  • @shivakaramadi9444
    @shivakaramadi9444 5 лет назад +1

    thank you so much. First time I learnt about delete[] concept..

    • @CppNuts
      @CppNuts  5 лет назад

      You are welcome!!

  • @naveen__ns200
    @naveen__ns200 7 лет назад +2

    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.

    • @CppNuts
      @CppNuts  7 лет назад +3

      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!!

    • @naveen__ns200
      @naveen__ns200 7 лет назад +1

      thank you :)

    • @CppNuts
      @CppNuts  7 лет назад

      You are most welcome.

  • @salmanasifs
    @salmanasifs 5 лет назад

    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?

  • @aj-uo3uh
    @aj-uo3uh 3 года назад

    Is this replacement of code done by the precompiler or the compiler?

  • @alliswell6587
    @alliswell6587 5 лет назад +3

    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

    • @CppNuts
      @CppNuts  5 лет назад +6

      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.

  • @9ShivamSharma
    @9ShivamSharma 7 лет назад +1

    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!!

    • @CppNuts
      @CppNuts  7 лет назад

      +Shivam Sharma size_t is 4 byte, its size is the size of your processor like 32 or 64.

  • @kartikpodugu
    @kartikpodugu Год назад

    This is great. First time, i encountered 'placement new'

  • @treyquattro
    @treyquattro 6 лет назад +4

    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

    • @CppNuts
      @CppNuts  6 лет назад +2

      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!!

    • @treyquattro
      @treyquattro 6 лет назад +1

      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.

    • @CppNuts
      @CppNuts  6 лет назад

      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.

  • @jakesmith6853
    @jakesmith6853 6 лет назад +1

    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?!

    • @CppNuts
      @CppNuts  6 лет назад +1

      Now I think it is over allocation technique.

    • @jakesmith6853
      @jakesmith6853 6 лет назад +1

      Thanks again, will share about your channel with my friends, hope to see more from you. Keep rocking.

    • @CppNuts
      @CppNuts  6 лет назад +1

      Thanks man!!!

  • @fuluk
    @fuluk 6 лет назад +1

    How does the compiler decide which method(associative array method or overallocation method) to use while implementing this delete [] concept?

    • @fuluk
      @fuluk 6 лет назад

      Is there any version dependance in implementing above like C++11 and next will use a certain concept etc.

    • @CppNuts
      @CppNuts  6 лет назад +1

      AFAIK almost every compiler uses over allocation method.

  • @treyquattro
    @treyquattro 4 года назад

    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.)

  • @keerthanraobj2149
    @keerthanraobj2149 3 года назад

    Awesome explanation sir

    • @CppNuts
      @CppNuts  3 года назад +1

      Thanks for liking

  • @93Mosfet
    @93Mosfet 5 лет назад +1

    You are awesome! Thank you! very good video

  • @kunalprasad722
    @kunalprasad722 4 года назад

    Why adding function to a class/structure don't increase its size but adding instance variable increase it

  • @chengjiwei
    @chengjiwei 5 лет назад +3

    You must have studied compiler. nice series, thanks

    • @CppNuts
      @CppNuts  5 лет назад +1

      Yes in B.Tech i did.

  • @saloniaggarwal7259
    @saloniaggarwal7259 4 года назад

    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?

    • @ishantsethi1
      @ishantsethi1 3 года назад

      yes, compiler does this by name mangling concept where it stores info in symbol table about the overloaded function. Please google "name mangling".

  • @ketanlalcheta4558
    @ketanlalcheta4558 4 года назад +1

    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?

    • @CppNuts
      @CppNuts  4 года назад +1

      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.

  • @vivekpandey4473
    @vivekpandey4473 5 лет назад

    can you explain the use of operator delete[] (bp)?

  • @faisalsyed2165
    @faisalsyed2165 6 лет назад +1

    (please explain the below
    operator delete[]((char*)bp - WORDSIZE); // What this line explains. Please elaborate.
    Thanks for detailed info.

    • @CppNuts
      @CppNuts  6 лет назад

      This is internal function call which deletes the memory created by dynamic memory allocation. (this will actually delete array of memory)

    • @jagdishagrawal9389
      @jagdishagrawal9389 6 лет назад

      @@CppNuts A follow up question on this. How does compiler knows what is the length of the memory allocated?

    • @flutterwind7686
      @flutterwind7686 4 года назад +1

      @@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.

  • @ketanvijaykar7782
    @ketanvijaykar7782 3 года назад

    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 ?

  • @prithvimadyastha8407
    @prithvimadyastha8407 5 лет назад

    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?

    • @CppNuts
      @CppNuts  5 лет назад

      didn't get the question.

    • @anshulnair5590
      @anshulnair5590 3 года назад

      @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".

  • @lysdexic9129
    @lysdexic9129 7 лет назад +1

    excellent job of describing the process, I'm sure this technique is applicable in application code.

  • @adityajain8122
    @adityajain8122 5 лет назад

    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?

  • @srinivas2808
    @srinivas2808 5 лет назад

    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

  • @itsmejinto92
    @itsmejinto92 3 года назад

    what is happening in " operator delete[](bp) " ?

  • @snehashishpaul2740
    @snehashishpaul2740 7 лет назад +1

    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.

    • @CppNuts
      @CppNuts  7 лет назад

      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.

    • @snehashishpaul2740
      @snehashishpaul2740 7 лет назад +1

      Yes, true. Didn't think that way. Thank you

  • @sksingh5926
    @sksingh5926 5 лет назад

    Hi, CppNuts
    I am a little bit confused. Can you please explain the meaning of *(size_t*)

    • @aby_yadav
      @aby_yadav Год назад

      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.

  • @ravibandaru8244
    @ravibandaru8244 3 года назад

    How this works, when incrementing the pointer and using delete? int *p = new int; p++; delete p;

  • @hirendpatelhiren1096
    @hirendpatelhiren1096 6 лет назад +1

    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...

    • @CppNuts
      @CppNuts  6 лет назад +1

      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.

  • @theradhikagupta
    @theradhikagupta 6 лет назад +1

    I need to understand PIMPL idiom of c++ on urgent basis sir..please help

    • @CppNuts
      @CppNuts  6 лет назад

      Hi Radhika, for now plz go for this video. It is really good explanation.
      ruclips.net/video/KOMl2p49rvo/видео.html

  • @saikiran644
    @saikiran644 7 лет назад

    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.

  • @rkj1488
    @rkj1488 7 лет назад +8

    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

    • @uthalputhal1477
      @uthalputhal1477 6 лет назад +3

      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.

    • @BhupendraYadav-li4ts
      @BhupendraYadav-li4ts 5 лет назад

      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??

    • @CppNuts
      @CppNuts  5 лет назад +3

      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.

  • @sanya3181
    @sanya3181 2 года назад

    How in line 23 size_t n = * (size_t) ((char*)bp -WORDSIZE); is 10. Should not it be 0.

  • @ashwinidikonda707
    @ashwinidikonda707 5 лет назад +1

    Sir please explain again in simple language

    • @CppNuts
      @CppNuts  5 лет назад

      please try to re-visit the video with little more focus.

  • @gauravendrasingh5829
    @gauravendrasingh5829 7 лет назад

    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.

  • @codestorywithMIK
    @codestorywithMIK 7 лет назад +1

    Loved it

    • @CppNuts
      @CppNuts  7 лет назад

      Glad you loved it :D

  • @ankitkaushal09
    @ankitkaushal09 6 лет назад +1

    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

    • @CppNuts
      @CppNuts  6 лет назад +2

      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. :)

    • @ankitkaushal09
      @ankitkaushal09 6 лет назад

      yeah, I am waiting

    • @jagdishagrawal9389
      @jagdishagrawal9389 6 лет назад

      @@CppNuts Is this second video already released?

    • @rameshkhokhani6848
      @rameshkhokhani6848 4 года назад

      @@CppNuts Hi is new video uploaded?

  • @abhishekrai4677
    @abhishekrai4677 3 года назад

    Super....

    • @CppNuts
      @CppNuts  3 года назад

      Thanks man..

  • @shailjasoni3064
    @shailjasoni3064 4 года назад

    It is not clear to me.

  • @satishbabu7254
    @satishbabu7254 Год назад

    Explanation is not good

  • @pawanadhikari110
    @pawanadhikari110 5 лет назад +1

    No one used this crap in modern c++

    • @CppNuts
      @CppNuts  5 лет назад

      Actually you don't need to know how it works but it is very famous in interview questions.

    • @pawanadhikari110
      @pawanadhikari110 5 лет назад

      @@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.