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

Sdílet
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

Komentáře • 97

  • @CppNuts
    @CppNuts  Před 5 lety +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 Před 5 lety +5

    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.

  • @druidclash9161
    @druidclash9161 Před 5 lety +4

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

  • @prasadbarude3766
    @prasadbarude3766 Před 6 lety +10

    Respect to ur knowledge!

  • @treyquattro
    @treyquattro Před 5 lety +4

    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

    • @CppNuts
      @CppNuts  Před 5 lety +1

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

    • @treyquattro
      @treyquattro Před 5 lety +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 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.

    • @CppNuts
      @CppNuts  Před 5 lety

      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.

  • @Pranav-pl7eg
    @Pranav-pl7eg Před rokem

    Best Channel for learning cpp
    Thank you so much

  • @9ShivamSharma
    @9ShivamSharma Před 7 lety +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  Před 7 lety

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

  • @shivakaramadi9444
    @shivakaramadi9444 Před 5 lety +1

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

  • @kartikpodugu
    @kartikpodugu Před 10 měsíci

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

  • @treyquattro
    @treyquattro Před 3 lety

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

  • @rkj1488
    @rkj1488 Před 6 lety +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 Před 5 lety +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 Před 5 lety

      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  Před 5 lety +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.

  • @alliswell6587
    @alliswell6587 Před 5 lety +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  Před 5 lety +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.

  • @chengjiwei
    @chengjiwei Před 5 lety +3

    You must have studied compiler. nice series, thanks

    • @CppNuts
      @CppNuts  Před 5 lety +1

      Yes in B.Tech i did.

  • @kapkaush5825
    @kapkaush5825 Před rokem

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

  • @naveen307_4
    @naveen307_4 Před 7 lety +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  Před 7 lety +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!!

    • @naveen307_4
      @naveen307_4 Před 7 lety +1

      thank you :)

    • @CppNuts
      @CppNuts  Před 7 lety

      You are most welcome.

  • @salmanasifs
    @salmanasifs Před 5 lety

    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?

  • @jakesmith6853
    @jakesmith6853 Před 6 lety +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  Před 6 lety +1

      Now I think it is over allocation technique.

    • @jakesmith6853
      @jakesmith6853 Před 6 lety +1

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

    • @CppNuts
      @CppNuts  Před 6 lety +1

      Thanks man!!!

  • @ketanvijaykar7782
    @ketanvijaykar7782 Před 3 lety

    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 ?

  • @ketanlalcheta4558
    @ketanlalcheta4558 Před 4 lety +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  Před 4 lety +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.

  • @lysdexic9129
    @lysdexic9129 Před 7 lety +1

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

  • @saloniaggarwal7259
    @saloniaggarwal7259 Před 4 lety

    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 Před 3 lety

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

  • @93Mosfet
    @93Mosfet Před 5 lety +1

    You are awesome! Thank you! very good video

  • @fuluk
    @fuluk Před 6 lety +1

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

    • @fuluk
      @fuluk Před 6 lety

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

    • @CppNuts
      @CppNuts  Před 6 lety +1

      AFAIK almost every compiler uses over allocation method.

  • @aj-uo3uh
    @aj-uo3uh Před 2 lety

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

  • @saikiran644
    @saikiran644 Před 6 lety

    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.

  • @keerthanraobj2149
    @keerthanraobj2149 Před 3 lety

    Awesome explanation sir

  • @prithvimadyastha8407
    @prithvimadyastha8407 Před 4 lety

    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  Před 4 lety

      didn't get the question.

    • @anshulnair5590
      @anshulnair5590 Před 3 lety

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

  • @adityajain8122
    @adityajain8122 Před 5 lety

    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?

  • @kunalprasad722
    @kunalprasad722 Před 4 lety

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

  • @faisalsyed2165
    @faisalsyed2165 Před 6 lety +1

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

    • @CppNuts
      @CppNuts  Před 6 lety

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

    • @jagdishagrawal9389
      @jagdishagrawal9389 Před 5 lety

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

    • @flutterwind7686
      @flutterwind7686 Před 4 lety +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.

  • @ravibandaru8244
    @ravibandaru8244 Před 3 lety

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

  • @srinivas2808
    @srinivas2808 Před 4 lety

    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

  • @sksingh5926
    @sksingh5926 Před 5 lety

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

    • @aby_yadav
      @aby_yadav Před rokem

      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.

  • @hirendpatelhiren1096
    @hirendpatelhiren1096 Před 6 lety +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  Před 6 lety +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.

  • @vivekpandey4473
    @vivekpandey4473 Před 5 lety

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

  • @itsmejinto92
    @itsmejinto92 Před 3 lety

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

  • @snehashishpaul2740
    @snehashishpaul2740 Před 7 lety +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  Před 7 lety

      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 Před 7 lety +1

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

  • @sanya3181
    @sanya3181 Před 2 lety

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

  • @theradhikagupta
    @theradhikagupta Před 5 lety +1

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

    • @CppNuts
      @CppNuts  Před 5 lety

      Hi Radhika, for now plz go for this video. It is really good explanation.
      czcams.com/video/KOMl2p49rvo/video.html

  • @codestorywithMIK
    @codestorywithMIK Před 7 lety +1

    Loved it

  • @abhishekrai4677
    @abhishekrai4677 Před 3 lety

    Super....

  • @ankitkaushal26
    @ankitkaushal26 Před 6 lety +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  Před 6 lety +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. :)

    • @ankitkaushal26
      @ankitkaushal26 Před 6 lety

      yeah, I am waiting

    • @jagdishagrawal9389
      @jagdishagrawal9389 Před 5 lety

      @@CppNuts Is this second video already released?

    • @rameshkhokhani6848
      @rameshkhokhani6848 Před 4 lety

      @@CppNuts Hi is new video uploaded?

  • @gauravendrasingh5829
    @gauravendrasingh5829 Před 7 lety

    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.

  • @ashwinidikonda707
    @ashwinidikonda707 Před 5 lety +1

    Sir please explain again in simple language

    • @CppNuts
      @CppNuts  Před 5 lety

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

  • @shailjasoni3064
    @shailjasoni3064 Před 4 lety

    It is not clear to me.

  • @satishbabu7254
    @satishbabu7254 Před 8 měsíci

    Explanation is not good

  • @pawanadhikari110
    @pawanadhikari110 Před 5 lety +1

    No one used this crap in modern c++

    • @CppNuts
      @CppNuts  Před 5 lety

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

    • @pawanadhikari110
      @pawanadhikari110 Před 5 lety

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