reinterpret_cast 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
    In this video we will learn what is reinterpret_cast is used for and there are few points we should always remember about it as follows:
    1. reinterpret_cast can perform dangerous conversions because it can typecast any pointer to any other pointer.
    2. reinterpret_cast is used when you want to work with bits.
    3. The result of a reinterpret_cast cannot safely be used for anything other than being cast back to its original type.
    4. we should be very careful when using this cast.
    5. if we use this type of cast then it becomes non-portable product.
    #reinterpret_cast #cpp #interviewquestions #programming #computerscience #softwareengineering #tutorial

Komentáře • 74

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

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

  • @DlcEnergy
    @DlcEnergy Před 5 lety +13

    10:36 and this was how c++ was invented

  • @dcubrakovic1
    @dcubrakovic1 Před rokem +1

    This explanation is much better than many others, because you showed the physical locations and what is inside them. Great!

  • @_myron
    @_myron Před 5 lety +12

    When using this cast it is recommended to wear protective gear

    • @CppNuts
      @CppNuts  Před 4 lety +3

      Correct!! You should be knowing what are you doing.

  • @vaibhavgupta6u
    @vaibhavgupta6u Před 6 lety +7

    one of the toughest and confusing topics in C++ , thanks dude for making it simple ,but being a professional I am still waiting for the actual implementation in real time project like darlkLevis said .

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

      sure dude, but i am uploading basic c++ and sorting algorithms. So it may take little bit more time.

  • @talhahbhuiyan9390
    @talhahbhuiyan9390 Před 6 lety +4

    Thank you for this video. I finally understand the concept completely.

  • @haydengorringe9158
    @haydengorringe9158 Před 2 lety +1

    A very clear explanation! Thanks for the tutorial!

  • @glennmarkabalos6657
    @glennmarkabalos6657 Před 6 lety +3

    man! i really love your c++ tutorials!

  • @kaajalsharma1225
    @kaajalsharma1225 Před 5 lety +2

    thanks Sir. i was very afraid of this topic. now it’s pretty much clear to me..

    • @CppNuts
      @CppNuts  Před 5 lety

      Welcome, thanks for the comment.

  • @adityathakur1961
    @adityathakur1961 Před 3 lety

    This is the best explanation of this topic on YT

  • @k.r.6006
    @k.r.6006 Před rokem

    Thank you for explaining with diagrams........thank you for your great work

  • @drmostafamahammad
    @drmostafamahammad Před měsĂ­cem

    good work bro.. only one packing issue at 11:30.. there is an empty packing byte after the char.

  • @vpodobaev
    @vpodobaev Před 2 lety

    I like very much the way you say "ok?" in your videos! :) So nice and calming! :)) Like a doctor. :))))

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

      You are the first one who feels like this..
      Thanks man..

    • @vpodobaev
      @vpodobaev Před 2 lety

      @@CppNuts Thank you for your videos. I'm enhancing my C++ knowledge. (Learning new features). Your videos are very helpful! 👍🏻 Very detailed and professional. 👍🏻🇷🇺🤝🏼🇮🇳

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

      Thanks..

  • @ApoorvSaxena8
    @ApoorvSaxena8 Před 3 lety +1

    very helpful thanks

  • @user-rz1hv
    @user-rz1hv Před rokem

    "trust me" - I trust you with every cell of my body... 😄

  • @quant-prep2843
    @quant-prep2843 Před rokem

    bro this is great tutorial my friend

  • @Byynx
    @Byynx Před 11 měsĂ­ci

    That first case senario is very important to understand what casting is. Before i thought casting between 2 objs meant that memory space from now on belongs to a certain object, but now i see we are just saying to the compiler to trust us and use that space in memory to store data of the first obj, but we still can writte data of the 2nd obj we've used to cast to the same space in memory. Of course this is wrong and danger.

  • @soorajkulkarni1902
    @soorajkulkarni1902 Před 2 lety

    The example is very good one

  • @mba2ceo
    @mba2ceo Před rokem

    what is better ? f1.write(reinterpret_cast(&z),sizeof(z)); or f1.write(reinterpret_cast(&z),sizeof(z)); z are Stucts

  • @caleb7799
    @caleb7799 Před 2 lety

    This was very good.

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

    really awesome and easy to understand ,thanks a lot

    • @CppNuts
      @CppNuts  Před 5 lety

      Thanks for you comment dude!!

  • @TheSAMARINDIMA
    @TheSAMARINDIMA Před 2 lety

    You are the best! ^^

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

    Hello!
    So I've been looking up for some basic examples with reinterpret_cast and found this program:
    #include
    using namespace std;
    int main()
    {
    int x=65;
    int* p = &x;
    char* ch = reinterpret_cast(p);
    cout

    • @rivendzi
      @rivendzi Před rokem

      You definitely found your answer by now. But maybe it will help someone else - that's how std::cout is implemented to handle char pointers. To actually get address of char* using std::cout you have to cast the pointer to void pointer(preferably - static_cast) and then pass it to std::cout stream.

  • @veereshranjan3519
    @veereshranjan3519 Před měsĂ­cem

    Hi , I think this pointer will come in below syntax tothe class's member function implicitly:
    Banana const *this
    But you said it will come like
    Const banana* this

  • @zkwei2233
    @zkwei2233 Před 3 lety

    thx, this series of video are very detailed.

  • @PaulMetalistulNegru
    @PaulMetalistulNegru Před 6 lety +2

    One observation: this video is intended for beginners, so please use delete for the pointers in the first example.

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

      Thanks man, you are correct.

  • @codingdecoding7675
    @codingdecoding7675 Před 5 lety +2

    What is difference between reinterpret_cast and c-style cast?
    class test {
    int i;
    int ii;
    };
    test t;
    int *p = reinterpret_cast(&t);
    int *pp = (int *)(&t);
    Are both same?

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

      In c style cast first it will try to cast with static cast and if cant it will apply reinterpret cast. But with directly applying reinterpret_cast it will do reinterpret cast only.
      So bottom line is c_style cast actually choose among all cast but in using new style casting you explicitly tell what cast to use.

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

      @@CppNuts Thank you, this minor distinction hard to catch.

  • @rajeshpawar1696
    @rajeshpawar1696 Před 2 lety

    Sir I have one problem with reinterpret cast in my project. Can you help me to solve this error

  • @rajeshpawar1696
    @rajeshpawar1696 Před 2 lety

    Sir there is an error in this statement
    If(infile.read(reinterpret_cast(this)) >0,
    The error is in ">" operator.
    Please give me solution

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

    thankyou sir,,you are really awesome

    • @CppNuts
      @CppNuts  Před 6 lety

      You are welcome dude. :D

  • @vinaykumar-no2zh
    @vinaykumar-no2zh Před 4 lety

    What was the difference between static-cast and reinterpret cast

  • @Byynx
    @Byynx Před 11 měsĂ­ci

    But reinterpret_cast is still a tag thing right so the code can become more readable, or it allows casts that static_cast wouldn't allow it?

    • @Byynx
      @Byynx Před 11 měsĂ­ci

      Or reinterpret_cast it's like a C style cast that lets do everything.

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

    Thanks for the great video but what's the point of this still? Why or when would I need to access those struct values via intetpret cast? As it's safe to access them normally.

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

      +DarkLevis i will create video for your answer.
      OR i will comment here wait for some time.

    • @user-sz9wm4rm5c
      @user-sz9wm4rm5c Před 6 lety +2

      If you use shared memory for IPC, you really need this feature.

  • @atulanand1587
    @atulanand1587 Před 3 lety +1

    example 2 , will work differently in little endian and big endian machine.

  • @tarunbagade4287
    @tarunbagade4287 Před 3 lety

    great

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

    loved

  • @handover007
    @handover007 Před 4 lety

    Where does this keyword reinterpret_cast come from even without adding any header file . Also even without using the operator we can cast

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

    Hi May i know which compiler are you using.

    • @CppNuts
      @CppNuts  Před 6 lety

      Editor is Sublime text.
      compiler is g++6 Mingw.

  • @rahulr9301
    @rahulr9301 Před 3 lety

    Perfect bro!!!

  • @WowPlusWow
    @WowPlusWow Před 2 lety

    🙏🏼🙏🏼🙏🏼🙏🏼🙏🏼🙏🏼🙏🏼🙏🏼

  • @Hack_Neuron_To_DSA
    @Hack_Neuron_To_DSA Před 4 lety +1

    One of the advertisement wasn't legel or good to mention India. Bharat is the most woman respecting country. Yes woman crimes are here. Which happens all over the world. And for your kind knowledge check who is at top in woman abuse. And check for population based ratio of crime too. So dont say name fools

    • @Hack_Neuron_To_DSA
      @Hack_Neuron_To_DSA Před 4 lety

      Suggestions: i love your content but please remove these advertisement content and report them. These things can make your channel more repellable

  • @miroslawzn
    @miroslawzn Před 10 měsĂ­ci +1

    ok? ok. Do not ask "ok?" all the time - this is really anoying.

    • @CppNuts
      @CppNuts  Před 10 měsĂ­ci +1

      Yes.. these are very old videos of mine, now you won't find anything like this in new videos.