in this video we will study about reinterpret cast in c process and we will see how is it different from other types of cast and when should we use it so its syntax is same as other typecasts so we need to provide the new type and then expression so this reinterpret cast allows any pointer to be converted into any other pointer so here there is one exception we cannot convert a cost pointer to a non-cost pointer we will see that later in the video and it also allows conversion between integer type and any pointer type and vice versa so let's see an example so let's say we have an integer a and it's equal to 70 and we have an integer pointer let's say p1 and it's pointing to this integer a and then we said that we can convert between different types of pointers so let's say we have a character pointer pc and we do reinterpret cast on that p1 and let's print it out and run it so it prints f so we know that uh for capital a the sky value is 65 similarly for b 66 c 67 and for f it will be 70 capital f so it correctly converts this integer into character so it treats this point x this pointer and converts it to character pointer now we said that we can convert between integer and pointers so let's take an example for that we have here integer 70 and let's say we want to convert it to a pointer p2 and we do reinterpret cast and we do int star and instead of another pointer we are here passing an integer and now let's print out this p2 and let's say see what it it prints 0x46 and has hexadecimal so if we convert it to decimal it will be 6 plus 16 times 4 which is 64. so 64 plus 6 is 70. so it correctly converts an integer to a pointer and now let's uh try the other way around so let's uh print out this same value and this time we will do reinterpret cast and convert it to int and we will pass this p2 so p2 is an address or a pointer and we are converting into integer and printing it so it says we will lose information so let's check what is the address of pointer on the system size of int star and again we will print size of int let's also print a bigger size so that we will we can use that let's say log and let's run it and let's comment out it and try again so it pointer has size of 8 bytes the second version one is integer it is four bytes and third is long so let's use the long hair instead of integer let's remove that so now we get back 70. so we converted an address to integer now let's move on so this type casting is inherently unsafe because we saw we can convert from care start into star and star to care star into pointer and so on and we can also convert from one class pointer to some different different class which is not related to this class at all and so we can this reinterpret cast allows such conversions so that's why it's very unsafe and the result of interpret cast cannot be safely used for anything other than being cast back to its original type other uses are at best non-portable so this these are also not portable so let's see the example of this class conversion where we will take two non-related classes so let's say we have class a and we have class b and let's add a few members to them so let's say it has x1 and x2 and then we define the constructor let's say x 1 is 10 x 2 is 20 and that's all and then we write a function to print a which will print x1 equal to x1 and x2 is equal to x2 similarly we can define for class b let's say it's it has just a care and it's c and here it's print b and it will be c is equal to c and it has just one member variable now let's create two objects one a so a we have already defined and then one b and then we can create a pointer pa and then we convert this b object so we are passing the address of b which is of type class b and we are casting it to a pointer and similarly we do for pointer to b and here we pass address of a object and let's try to print that it has print a and b will have print b and let's run it so we see that it messes up the values we had so here a should be having 10 and 20 and b should be having a capital a but here it prints x 165 and x2 is 0 so this 65 is possibly this a because a has a sky value of 65 and this x2 is some random value 0 and c is again some random value whereas if we do a dot print a and b dot print b then it will correctly print so here we see that it correctly prints 10 20 and a so this messes up the values but it permits such type of conversion converting a pointer of class a to class b and these are totally unrelated now let's convert them back correctly and so now uh let's say we have another pointers pa1 and here pv was actually pointing to an object of type a so let's pass pb here so now pv will be correctly converted to a pointer similarly b pointer pv1 and we do reinterpret cast and convert it to b pointer and here p a was actually pointing to a b object and now again do p a one print a and p b1 print b and now it should print correctly so we see that for this part we had wrongly casted them a into b and b into a so they messed up the values and here we have cast them back to their correct pointers so here a star is pointing to pb which was originally pointing to a so now they are correct and they are correctly printing the values so if we cast them back then it will be correct usage otherwise it will be dangerous to use them so these are very unsafe and now we cannot cast away the const or volatile or unaligned attributes so we saw that we can convert into star to care star but we cannot do the conversion if one of them is cost and we are converting them back to non-cost so for example the same example here ap is a cost pointer to a and we try to do care star on this so it will fail because it cannot remove the const attribute here but this will pass because this is cost care star and we are changing a cost install to it so let's see this in the example so let's say we have a const int pointer p3 and it's pointing to this variable even and then we have a care pointer pcc and we do interpret cast on that so it will not compile it will say that uh from const into carestar casts away the qualifiers but let's change it to cost so here also we need to change it to cost and now it correctly converts it back to cost care star so we cannot convert a const pointer to a non-const pointer so that is not allowed similarly for other attributes like uh volatile and unaligned attributes now let's see another example so it can be useful the interpret cast can be useful when working with bits so let's say we have a structure struct s so it has two ins one care and one bool so it will have four bytes of for i1 and another 4 bytes for i2 and then 1 byte for this and 1 byte for bool so one byte here for c and one byte for b and it will also occupy this space due to structural padding so let's see an example of how we can understand our interpret cast works in such cases so let's define the same structure struct s and it has int i1 into i2 c and bool b so total it will have 12 bytes now let's instantiate that s dot i 1 equal to 10 s dot i 2 is equal to 20 s dot c is equal to a and s dot b is equal to true now we can uh create a int pointer so we have endpointer pointer to structure and we can cast this structure to that endpointer so we pass the address of s so now what will happen so we have this or we can see here so we have this this structure in memory total it has 12 bytes so now if we have an integer pointer so now this integer pointer will be pointing here and it will just think that it has control over this part of the memory and we see that this part of the memory was occupied by this integer i1 so if we we have this in in pointer ps and if we print it it should print the value of i1 so let's uh run it and let's print ps so we see that it prints 10 and we had assigned i1 s10 now we can do ps plus plus so ps is still integer pointer so when we do ps plus plus this pointer moves here because it thinks it's integer pointer so we it will advance by whatever is the size of integer so now it comes here and it's pointing to the beginning of i2 and it's still an integer pointer so it thinks that it has control over this part of the memory it's pointing to this part so when we print now it should print the value of i2 so let's again print out ps so it should print 20. so we see that it prints 20 but if we do plus plus again it will come here which will be the beginning of this character so we can do plus plus and then if we print it it will print this whatever bit patterns are stored in this complete part here this thing but we don't want that just first one byte is occupied by c and next one by bull and the others are not of concern to us so after advancing it here ps we need to cast it back to a character pointer so let's advance this ps and then cast it to a character pointer and then print it so it correctly prints a and now if we advance it since it's a character pointer it will advance by one byte and it will reach the beginning of b then we can after advancing we can cast it to a bool pointer and print the value of b so let's do that so we will again advance this psc and then let's create a pointer psb which will be a bool pointer and then print it so it should print one because we had added true here uh sorry it should be bool so it prints one here because we had added true so if we do it false then and run the same code it prints zero for false so this way we have uh working in bits and we are trying we understand what's the memory structure and then using the reinterpret cast to access the different elements so i hope you liked the video thanks for watching
Get free YouTube transcripts with timestamps, translation, and download options.
Transcript content is sourced from YouTube's auto-generated captions or AI transcription. All video content belongs to the original creators. Terms of Service · DMCA Contact