Had to actually stop and think about why I love this particular programming channel so much and it hit me. You don't talk philosophy, just computing, and it's so unbelievably refreshing to hear someone not spend hours lecturing me on why I'm a terrible programmer and just get right down to business. Wonderfully informing as always, keep up the good work!
A long time back, I debugged a buffer overflow that was corrupting a vtable pointer. Oh boy, that was a mess. Basically, execution would jump seemingly randomly and I’d end up seeing a nonsensical and corrupted backtrace. It’s fun when your program crashes inside a function that you know for sure is never called.
Hahaha! Oh wow, yeah, that would be a problem! Those bugs that seem to just do random things are soooo hard to fix!! Major props for finding that one :)
I think modern technologies such as ASAN would be able to find these types of errors, although you did mention it was a while back, so ASAN might have not existed.
@@inferior2884, Sadly, ASAN only catches overflows where you smash through the end or beginning of a buffer. If you write hundreds or thousands of bytes after the end of your buffer (depending how big of a red zone you have ASAN configured for), it won’t be detectable.
The fact that methods receive the invisible this pointer internally shows a concept which I started to understand by coding in Python, where you have to specify the self parameter in method heads explicitly. It also shows that C is not that far away from an object oriented programming language, you can do the basic stuff with structs and functions that take a struct as their first argument, just like the this pointer. Thanks for the great video!
fully on the money mate!.. I think that (although maybe someone who knows a little more than I can chime in to confirm here), but I think the only difference that C++ OOP would provide over C OOP (using struct pointers as you mentioned) is exceptions. Technically there is probably a bit more again (like constructors/destructors etc.), but I think you can manually 'hard-code' something equivalent but to guarantee the destructor to be called during an exception I thought was something that C++ could achieve with C not..... -- I'm sure I've either read or been told this in the past somewhere but I've never explored how that is achieved in practise. I imagine that exception handling would be done through catching either software/hardware interrupts and preparing a set of functions to call for the 'message handler' that would be invoked during the particular trap for that particular exception call?? Jeez the more I write the more I realise I don't know shit.... now that I've written it all, even though it's a pointless monologue It seems a waste to delete 😆😆
Wrote a compiler for a little language when I was in university. The language was made specifically for the class, and changed as new ideas were taught in the class. All of this lines up pretty much how I expected. If you want to learn how memory is handled in something like C++ write a compiler by hand for a small language and it all becomes pretty clear. There are lots of books you can find to follow on this stuff, but i had the benefit of an awesome teacher.
It's a moment to remember when you finally realize that C++ is just C with one scoop of syntactic sugar and ten scoops of bureaucracy trying to fix that first scoop.
The first C++ compiler actually just generated C code. To be fair though, there are a lot of things C++ adds which are pretty much impossible to do in C. But object oriented programming is not one of them. There are plenty of oo libraries in C.
@@jbird4478 What I wonder, is would it be worth learning C++ since I've already learned and use C? The things you gain - how useful are they actually in real world applications?
@@Sauvenil Yes. It is probably the most used language next to C, so it is useful. My advice would be to stay clear from anyone that teaches "C++ design principles" or "the right way" to program in C++ though. C++ is most useful as a procedural language like C, but with added features. It's also easy to learn if you already know C by just incorporating one C++ concept at a time in a program. For instance, if you find you have quite some duplicate code you could try to write that as C++ templates instead. Or replace some struct with a class. That way you'll learn C++ without falling in the trap of these over-designed ideas like OOP. And some things - like operator overloading - you can just completely ignore. There are a few small compatibility issues between C and C++ (like casting void*) but you'll resolve those quickly enough.
Heh. Reminds me when I was first tasked with modifying a consted variable in my introductory C++ class. People looked at me like I was a homicidal maniac when I presented my inline assembly solution and proclaimed, "well, if you want me to ignore the compiler, nothing better than a bit of assembly!" Apparently, I was only meant to const_cast that poor devil. 😂
What's fun is that modifying a const variable is by definition undefined behavior. So, the compiler can happily ignore anything that modifies a const_cast variable. Meanwhile, the assembly solution might not even be pointing to a real memory address since the variable was optimized out. Segfault due to out of bounds write, or write to read only memory.
Furthermore. Once you introduce undefined behavior, the entire program run has undefined behavior, even the part LEADING to the occurrence of undefined behavior. The assembly version would definitively get a real memory address, because some way or the other you would provide a pointer, but that pointer can as you pointed out point to ro memory. And of couse, all other uses of that const object could still use an inline copy of the value and hence not be affected by that neat little assembly trick.
@@arthurmoore9488 That is false. The compiler cannot ignore anything that modifies a const_cast'ed variable. The value cannot be optimized out. Memory is not saved by using const variables. What you say is true of constant expressions (i.e. constexpr), not const variables. Modifying const variables is not undefined behaviour. The C++ spec is pretty clear about what const does and doesn't do. It is purely a compile-time construct used to help the programmer catch programming mistakes. const does not in any way impact code generation and runtime behaviour.
@@DrSaav-my5ym What does knowing assembly have to do with knowing C++? They are completely different. C++ is far more complex than assembly. Sure the later requires you to understand your CPU's architecture and functionality, but assembly is not complex.
"Wait, it's just pointers?" "Always has been" Funny you mention the alignment table at 5:07. I've actually been wondering this: On a CPU that has strict alignment rules (like 68000, ARM, or most RISC CPUs) can you get more bang for your buck by doing this with your structs: int foo; char a; char b; short y; int bar; instead of: char a; int foo; char b; short y; int bar;
I faced this problem years ago with "complex message" sources that were to compile to both a Windows DLL (for an 'unpacked' VB GUI with VB 'records') and a 'packed struct' QNX database app... Yes, floating doubles, longs, and ints to the top of a struct, with ragged chars and char arrays gathered at the bottom DID work just fine. No padding required. The declaration of the structs' elements appeared 'jumbled' in the source code, but cpus don't care where the bits are stored; that;s a human hang-up...
Yes, you should get more out of it, especially if you have like 10,000 instances of the struct in an array, since you'll have less wasted bytes per cache line.
Recently, I was able to take a struct from about 180 bytes to 128 bytes by reshuffling 32-bit fields vs 64-bit fields and using a union for one block of 4 64-bit pointers. Also, just today, I found I could sneak a field into another for free due to alignment padding.
Yeah but isn't some of this also "Compiler Vender Implementation defined?" MSVC vs GCC vs Clang vs Intel? The language standard is vague as it doesn't force one to comply to a strict way of doing things. It's a guideline that needs to be followed so that the same written source code produces the same expected or desired results. In other words, they don't care how you do it as long as it provides the expected results. I'm just wondering if this could be different between various compiler vendors? Also doesn't the calling conventions have an effect too?
im learning so much internally about programming as a whole. ive never really understood what happens in the background, only what ive been taught in school, all horseshit. thank you, you're a great teach
The most important thing you can do with an indispensable employee is fire them immediately. At the end of the day, the inputs and outputs and business logic are known so anything can be rewritten by a competent developer.
It's also very useful when you're stuck with a mission critical binary for which the source code is long gone. Pull it apart with HexRays, hack in what you need, and link it by hand. C++ is gorgeously stupid when it comes to linking order, so you can convince it to fudge anything for you before main() is even called.
@@bkucenski The most important thing in business is fiscal responsibility. Firing an indispensable employee gains you one salary. Rewriting and debugging someone's 20 years career can easily cost you 10 times that... because, as it turns out, the program itself quite often IS "the documentation" of all the decisions and discoveries of special cases that has happened over it's development time.
Circa 1990 I was writing 65816 assembly for the Apple IIGS and was really just getting interested in C++, which was just becoming mainstream. I was using MASM, which has a very robust macro facility and wrote a set of macros to create vtables and make virtual function calls so that I could do object oriented programming in assembly language. It was so much fun!
I reached most of your older videos at least twice a couple of weeks ago. I was craving that Creel and I can finally scratch it with this brand new video!
I went to school for visual C++ over a decade ago. _Definitely a bit rusty_ The main thing that stuck with me is my teacher complaining about my loops not terminating. _I can modify code, but can't write from scratch anymore_ Never even messed with memory directly, I could imagine his face if I did. I'm of a mindset that I gotta break something to figure out how it works, and this seems a good starting point to get back into coding.
Definitely recommend learning one component at a time, if you miss something that can make even the simplest thing impossible. Javidx9 is a great RUclipsr for learning C++ ruclips.net/user/javidx9 He makes simple games in the windows console, like Tetris.
"my loops not terminating" so basically you mean that you were getting inside of a loop, with no way of getting out? So it's just that you coded a loop that make no sense and will never be true?
@@nicolaskeroack7860 I had strict conditions in them where they would only work as intended. It was more like I repurposed them into instances, where the technical close for the loop would be the end of the page.
I am a complete hobbyist gamedev so I learned enough of programming just so I can work with the engines and being able to read people's code but years ago I had a thirst to really learn how computers actually work so I went on a journey to learn about architecture, assembly language, C language, C++, C# etc. And I had quite a bit of "Aha!" moments one of which was "Hey its all about pointers and data and hoping around data?"Also another aha was realizing you actually working in tandem with compiler, that's the main thing in programming, that's what a language is actually, compiler puts it together in a set way the designers of that language intended and it gets translated to machine code in the end. Absolutely loved how C was just enough step up from assembly that it didn't obscure true nature of programming. As I learned about the community I never could understand how people favored some languages over the others, still cannot to this day hear somebody say "This is better than that..." Like wtf do you even know it all works?? Its all the same under the hood but of course you are going to use the one its supported in whatever framework you work in. One time I watched this one programmer video how they would hire people coming with CS degrees but no work experience and some didn't know the difference between stack and heap... Sure developing some non demanding android app you don't need to know anything about that even how OOP works but if you are developing anything that requires above average performance you will need to know how it actually all works under the hood.
Well of course it's all the same under the hood, it still makes sense to prefer some languages over others, there's syntax, readability, and frameworks like you mention, etc.
I amaze myself that I understand this video perfectly having learned all this stuff over 25 years ago. (Although I haven't used it very much.) Great video. Well done. Gut gemacht.
Thanks for this video. In the firs tpart of the video, it shown me that I was thinking right about struct in C. The fact that you should be able to cast a struct and change it's attributes as you will. :) Even though it's C++ with OO, struct are kind of OO but in C. It works pretty the same.
Thank you so much for making the video! I before was modding games, so I knew that before. But I never would have thought of creating custom VTables. Before I was modifying the addresses of the VTable, but this allows for even better stuff, like function swapping for only one instance (for ex. Localplayer). I would like to see a part 2 that covers virtual inheritance and dynamic casting :D
I've programmed in C, C++, Java, a couple assembly languages, Visual Basic, C#, Racket, some other languages, and Python. It wasn't until I got to Python and saw the "self" parameter at the start of all member functions that I had an epiphany about how OOP is done behind the scenes. This video did 2 things in addition to that epiphany. 1: It confirmed my epiphany. 2: When you showed polymorphism, it showed me that the designers who came up with OOP must have been geniuses to purposely design in polymorphism behind the scenes.
Well yeah. If you want to understand a programming language or some feature of it. It is a good idea to disassemble some code you made. That allows you to see what is going on concretely.
Vtables were explained to me before but now I understand that the vtables are per-class and not per-instance, I couldn't understand that before, so thank you for putting that in and insisting on it!
I had always thought of C as a strictly typed language until I read the book Expert C Programming: Deep C Secrets and it pointed out it was loosely typed and having more experience I realized, that yeah, you can always get to the bytes and do what you want.
This is an excellent introduction! Would be really cool to see an advanced version of this that examines the implementation of multiple inheritance, virtual inheritance, and dynamic_cast in the presence of the other two.
14:50 "Dog Fish" made my day! :-) I really enjoyed this under the hood look at things, it makes things make more sense. I can follow pointer manipulation and intent far easier than posh words. :-)
The fact that the first parameter of a member function is the this pointer is useful to know if one wants to pass stateful free functions. You use bind_front on that member function and its instance, thereby creating a free function with a state that you can pass along to other functions who'd be agnostic to the fact there's a class behind it.
It's still better than no typing at all or optional typing. Languages like Python and JS are nightmares to code in when compared with typed languages. I didn't realize that until I started using C# and C to program things. Having the peace of mind of knowing that if something compiles, it'll run, unless you did something careless with memory management is priceless as a more advanced programmer. Hopefully, the game dev industry moves towards Rust at some point so that us game devs can be even more at ease while coding.
I thought this was gonna be a boring old news video about C++ magic, but I really liked that demo where you created a function in assembly alongside the C++ and how you dug to find the address value. Nice stuff!
Another amazing video Chris man, love messing under the hood with oop for funzies, another quick way I use for getting those variable addresses, open up memory viewer in debug and type &variable_name in address bar that will throw up the memory address of var and it's contents. Great content as always mate, noice one.
Great stuff! No bs and to the point, nice. I think the higher level the language is, more tricks it uses - objective c is also very interesting in this manner (class/instance structure, dynamic linking)
well damn, that just shattered most courses where I was told that those OOP practices helped with security inside a program lol, pointers seem obvious but I really thought at least some form of protection from outside writes was implemented
Another fun point not mentioned is that single inheritance of member variables is implemented like struct DerivedStruct{BaseStruct base; int derivedVar;}; Thus any DerivedStruct pointer simultaneously points to a BaseStruct, and can be passed to functions that take a BaseStruct pointer. And if BaseStruct has a vtable pointer as its first member, then so does DerivedStruct. You just point it to DerivedStruct's vtable, and then virtual calls from inside BaseStruct functions will actually jump to DerivedStruct functions. Very easy to do in regular C. C++ just automates a lot of it.
i found this channel randomly not long ago and i cant figure out why i find his videos so comfy. I feel its to do with how its just like hes talking and having fun. What a cozy fella
This was very interesting! Although I knew about this "magic" I didn't know how exactly it looks in memory etc. Maybe some deep dive into the templates next?
Great idea! I did record bits on templates and constructors and destructors, but I cut it out coz it was too long! Maybe I can put it in another vid at some point. Cheers for the suggestion and cheers for watching :)
Visual Studio C++ compiler allows you to see the class memory layout at compile time using the compiler switch /d1reportAllClassLayout. The real mess starts when you have multiple layers of inheritance with non pure virtual classes. You get a nice interleave of function pointers and data that may or may not be aligned depending on user #pragma. It's crucial to understand this for reverse engineering applications from compiled binaries.
This is good. Other things that can be covered are: How is multiple inheritance implemented How is virtual inheritance implemented How is static_cast, reinterpret_cast, const_cast & dynamic_cast implemented How are member data pointers and member function pointers implemented dynamic_cast can do cross casting in an multiple inheritance hieriearchy Compilers can optimise dynamic_cast, so that if they see you are casting from Derived to Base, they can call static_cast instead. Static_cast is sometimes optimised as a no-op as the compiler realises it does not have to do anything. The fact that if you call Derived classes virtual functions non-virtually, the compiler can optimise these function calls so they are called non-virtually The RVO (return value optimisation) and NRVO (named return value optimisation) The Empty Base optimisation Move constructors and Move assignment operators Returning to the subject title: "Object Oriented Programming is a Dirty Rotten Low Down Trick". No. But you can misuse Object Orientated programming: 1) Modelling a situation with inappropriate inheritance e.g. I have a Bird class. All Birds fly. Oh, what to I do about Penguins? 2) Breaking encapsulation
This is fascinating, and incredibly evil! Theoretically you can use this to build add-on or plugin packages that have control over functions and data that it has no business interacting with. Not authorized to touch core functionality at your business? Who cares! Video game allows for mods but no modifications to the engine? Sorry UE4, we now have an unstable and horribly awful workaround! Want to call a class function without a member? No problem! Basically: "Thanks, I hate it!"
Theoretically, it shouldn't work, memory protection should keep data away from code that doesn't have permission to access it. But operating systems just do protection on a per-program basis, so within a program it's your playground. IRL is it much of a problem? If you're writing a program, and you have access to some functions, you're "trusted" enough to access even the ones you weren't explicitly given access to. A C program can access anything in it's own memory space. You're not "supposed" to but the machine won't stop you. I wonder if this "weird ugly internal names" trick would let you mess about with the inner parts of a game engine you bought to make a product around? Maybe run it through a decompiler and see if those names crop up, or at least useful memory addresses. You might use the same trick, find the address of something you're allowed to have, then add a few to the pointer and see what's there. In fact you might run a loop where a pointer accesses the entire memory space and dumps it during program execution. Just to see what you find. I bet those weirdo ugly names have a format you could look out for. That's assuming all of that stuff doesn't get ripped out at compile time, when Unreal compile the version of the engine they ship to customers. Still, it happens that sometimes people leave the debugging info in. Not every time but sometimes enough to be useful.
Awesome video, thanks for sharing! Regarding getting the address of a variable that you discuss around 10:30, you can use the "immediate" window and type "&a", that will give you the address and data. Another trick is using the watch window and enter an expression like "&b,bb", which will show the address of b in binary format.
Huh? C++ is still that simple. We just make the compiler do more and more work. Take: ``` std::array someArray = ...; for(auto &val: someArray) {...} ``` That converts to something like: ``` { int * val = &someArray; while(val != someArray+5) { ... val++; } } ``` Compilers don't really do this, but it's pretty close. You can see how using std::array and a range based for loop can actually be faster than using a loop counter and accessing the array by index.
Awesome video! Really demystifies OOP. Does this mean that upcasting is type punning? Can you use type punning to downcast?? (assuming classes are the same size)
You certainly could! Not recommended of course, to cast from parent to child, but there's nothing in C++ stopping us from treating any block of RAM as anything we like! Well, nothing but the compiler trying to warn us that we're doing something stupid! Hahaha, cheers for watching :)
@@WhatsACreel - There are cases in my engine where an abstract list of TParent items (for example, nodes in the scenegraph) might be cast to multiple different TChild descendants based upon their class (IE identify and then cast a TParent to a TPortal for another render pass) - is that the kind of thing you refer to when you say it's not recommended? What pitfalls are there with this kind of access pattern?
@@JohnnyWednesday It's just a rule of thumb to not do it, but when you know you need to, then you need to. And there are several kinds of instances where it's the only way to get things to run fast.
I remember the first time I mucked about with classes, I'm a C/Assembly programmer, I thought objects were gimmicky namespace hacks, basically programmatic alternative to macros that would serve the same purpose (this was my first impression, my current understanding has obviously evolved), in my view the only 'real' valid application of OO principles is if there's a meaningful layer of abstraction between the programmer and the underlying assembly, and I don't mean libraries or frameworks, so then platforms like Java or .Net/C# -- I also don't like how colleges seem to favor OO over procedural and functional styles, going so far as to teach OO based data structs and algorithms, which the students may be shocked to find aren't as directly applicable as they might first assume, such as in functional programming languages or in compute/space critical applications where everything must be as flat/contiguous as possible -- -- anyway, really enjoy videos on higher level concepts from these kinds of perspectives!
I'm currently in college, we get a course into OO (Java), Declarative (specifically Data driven T-SQL) and Functional (free language choice) programming each. They much rather give us the toolsets to know which is which and the benefits and points of attention for all of them, rather than deep diving into a single language for 3 years. There's also a specific major we can opt in for specifically focus more on data-analysis, web-development or embedded development. So luckily there does seem to be a shift in course-structure to move away from fixed OO.
As someone who makes my living dealing with bad code, and has dealt with more than one Physics PHD's code, I actually appreciate things being taught as OO and wish those PHDs knew it... It turns out that when they are trained to treat everything as CSV files where each column is a separate 1D array that leads to really bad programming practices. What's fun is that pandas actually does that under the hood using numpy, but because it exposes the entire table in terms of a DataFrame, it's so much easier to work with. The key takeaway should be that just because the low level does something efficient, like hidden `this` to a name mangled function, doesn't mean that we should be constrained to doing it by hand. Better to teach students the basics, then go back and explain all the "magic" under the hood to make it work efficiently.
12:28 Geez mate, have furry pron reference memes become so popular that even you reference them? 🤣 I guess you are a man of much and many a culture. Thanks for the vid, great as always!
I'm not 100% who the target audience is for a talk like this but an interesting motivating exercise might be going in the other direction and showing one would implement object-oriented programming in C++ if keywords `struct` and `class` didn't exist.
this video is a must watch if you are into game hacking and also help you reverse engineer routine and i guess, with this knowledge, we can implement those oop feature in C if we wanted to
That vtable override trick is what i had to do for overriding some methods in precompiled proprietary DLL to make it interract with data on network rather than just accessing file localy.
There some extra fun with multiple inheritance of class with a vtable that end up with 2 vtableptr. And in case of virtual inheritance, the offset to the subtype being stored in the vtable. Lot of fun edge case that are not meant to be messed with and could blow up when playing with them. ^^
14:14 That's the cool thing about it. You can f.e. store an array of base class objects and call a function that is overridden in each unique child class without that child class. Or you can also easily override certain methods, knowing that your custom class will do special things at common events. In Unreal Engine f.e. there is an Actor base class and you can inherit from it and override events like BeginPlay, BeginDestroy, Tick and so on.
Can you use the vtable pointer change for a self programming code? Like get one or two tables with default functions. At runtime write your acustomed vtable in RAM, point the original table to it and get a custom behavior?
We learned: OO is simply records with functions + polymorphism. The visibility is just a "compiler" feature. I guess you could cast to a record type too ;)
Ummm... no. OOP is a program documentation strategy. Your compiler strips all of it from your code because your CPU does not have an instruction set that knows anything about objects. Records and naming for polymorphism is one way to implement OOP in a language that doesn't offer it as syntactic sugar.
Had to actually stop and think about why I love this particular programming channel so much and it hit me. You don't talk philosophy, just computing, and it's so unbelievably refreshing to hear someone not spend hours lecturing me on why I'm a terrible programmer and just get right down to business. Wonderfully informing as always, keep up the good work!
He's jolly and that's very rare in this field.
oh, I personally love people telling me why I'm a horrible programmer as well as a bit of philosophy. people really have different tastes, don't they?
Who is this philosophical programmer and where can I find him
A long time back, I debugged a buffer overflow that was corrupting a vtable pointer. Oh boy, that was a mess. Basically, execution would jump seemingly randomly and I’d end up seeing a nonsensical and corrupted backtrace.
It’s fun when your program crashes inside a function that you know for sure is never called.
Hahaha! Oh wow, yeah, that would be a problem! Those bugs that seem to just do random things are soooo hard to fix!! Major props for finding that one :)
I think modern technologies such as ASAN would be able to find these types of errors, although you did mention it was a while back, so ASAN might have not existed.
@@inferior2884, Sadly, ASAN only catches overflows where you smash through the end or beginning of a buffer. If you write hundreds or thousands of bytes after the end of your buffer (depending how big of a red zone you have ASAN configured for), it won’t be detectable.
This is definitely my least favourite part about C/C++. When things go wrong finding the first thing that went wrong is a nightmare.
oh dang I think I have ptsd of that
The fact that methods receive the invisible this pointer internally shows a concept which I started to understand by coding in Python, where you have to specify the self parameter in method heads explicitly. It also shows that C is not that far away from an object oriented programming language, you can do the basic stuff with structs and functions that take a struct as their first argument, just like the this pointer. Thanks for the great video!
It's sooo close to C! I guess the original name, "C with Classes", is pretty much what it still is today! Cheers for watching :)
fully on the money mate!.. I think that (although maybe someone who knows a little more than I can chime in to confirm here), but I think the only difference that C++ OOP would provide over C OOP (using struct pointers as you mentioned) is exceptions. Technically there is probably a bit more again (like constructors/destructors etc.), but I think you can manually 'hard-code' something equivalent but to guarantee the destructor to be called during an exception I thought was something that C++ could achieve with C not..... -- I'm sure I've either read or been told this in the past somewhere but I've never explored how that is achieved in practise. I imagine that exception handling would be done through catching either software/hardware interrupts and preparing a set of functions to call for the 'message handler' that would be invoked during the particular trap for that particular exception call?? Jeez the more I write the more I realise I don't know shit.... now that I've written it all, even though it's a pointless monologue It seems a waste to delete 😆😆
C++ was first implemented as a compilation step for C
You can do everything in C that you can do in C++, it just takes ten times the lines of code, and you get none of the benefits.
@@graealex because C++ has no real benefits. ;-)
Wrote a compiler for a little language when I was in university. The language was made specifically for the class, and changed as new ideas were taught in the class. All of this lines up pretty much how I expected. If you want to learn how memory is handled in something like C++ write a compiler by hand for a small language and it all becomes pretty clear. There are lots of books you can find to follow on this stuff, but i had the benefit of an awesome teacher.
We too studied compilers in one class. Man, what a fun class that was.
Man y'all had awesome unis. In my uni we learn Figma instead of Graphics Programming in the "Computer Graphics" class 🤦♂️
It's a moment to remember when you finally realize that C++ is just C with one scoop of syntactic sugar and ten scoops of bureaucracy trying to fix that first scoop.
I still prefer C++, though.
Ha! Me too :)
The first C++ compiler actually just generated C code. To be fair though, there are a lot of things C++ adds which are pretty much impossible to do in C. But object oriented programming is not one of them. There are plenty of oo libraries in C.
@@jbird4478 What I wonder, is would it be worth learning C++ since I've already learned and use C? The things you gain - how useful are they actually in real world applications?
@@Sauvenil Yes. It is probably the most used language next to C, so it is useful. My advice would be to stay clear from anyone that teaches "C++ design principles" or "the right way" to program in C++ though. C++ is most useful as a procedural language like C, but with added features. It's also easy to learn if you already know C by just incorporating one C++ concept at a time in a program. For instance, if you find you have quite some duplicate code you could try to write that as C++ templates instead. Or replace some struct with a class. That way you'll learn C++ without falling in the trap of these over-designed ideas like OOP. And some things - like operator overloading - you can just completely ignore. There are a few small compatibility issues between C and C++ (like casting void*) but you'll resolve those quickly enough.
I think that it makes perfect sense that an object-oriented paradigm would be implemented using the simplest and fasted code that gets the job done.
Heh. Reminds me when I was first tasked with modifying a consted variable in my introductory C++ class. People looked at me like I was a homicidal maniac when I presented my inline assembly solution and proclaimed, "well, if you want me to ignore the compiler, nothing better than a bit of assembly!" Apparently, I was only meant to const_cast that poor devil. 😂
What's fun is that modifying a const variable is by definition undefined behavior. So, the compiler can happily ignore anything that modifies a const_cast variable. Meanwhile, the assembly solution might not even be pointing to a real memory address since the variable was optimized out. Segfault due to out of bounds write, or write to read only memory.
Furthermore. Once you introduce undefined behavior, the entire program run has undefined behavior, even the part LEADING to the occurrence of undefined behavior.
The assembly version would definitively get a real memory address, because some way or the other you would provide a pointer, but that pointer can as you pointed out point to ro memory. And of couse, all other uses of that const object could still use an inline copy of the value and hence not be affected by that neat little assembly trick.
@@arthurmoore9488 That is false. The compiler cannot ignore anything that modifies a const_cast'ed variable. The value cannot be optimized out. Memory is not saved by using const variables. What you say is true of constant expressions (i.e. constexpr), not const variables.
Modifying const variables is not undefined behaviour. The C++ spec is pretty clear about what const does and doesn't do. It is purely a compile-time construct used to help the programmer catch programming mistakes. const does not in any way impact code generation and runtime behaviour.
wait......so why the hell were you in a intro to C++ class if you already freaking knew assembly, wtf?
@@DrSaav-my5ym What does knowing assembly have to do with knowing C++? They are completely different. C++ is far more complex than assembly. Sure the later requires you to understand your CPU's architecture and functionality, but assembly is not complex.
"Wait, it's just pointers?"
"Always has been"
Funny you mention the alignment table at 5:07. I've actually been wondering this: On a CPU that has strict alignment rules (like 68000, ARM, or most RISC CPUs) can you get more bang for your buck by doing this with your structs:
int foo;
char a;
char b;
short y;
int bar;
instead of:
char a;
int foo;
char b;
short y;
int bar;
That's a great question! I reckon that would work a treat! Definitely worth a try anyway. Thanks for watching :)
I faced this problem years ago with "complex message" sources that were to compile to both a Windows DLL (for an 'unpacked' VB GUI with VB 'records') and a 'packed struct' QNX database app...
Yes, floating doubles, longs, and ints to the top of a struct, with ragged chars and char arrays gathered at the bottom DID work just fine. No padding required. The declaration of the structs' elements appeared 'jumbled' in the source code, but cpus don't care where the bits are stored; that;s a human hang-up...
Yes, you should get more out of it, especially if you have like 10,000 instances of the struct in an array, since you'll have less wasted bytes per cache line.
Recently, I was able to take a struct from about 180 bytes to 128 bytes by reshuffling 32-bit fields vs 64-bit fields and using a union for one block of 4 64-bit pointers.
Also, just today, I found I could sneak a field into another for free due to alignment padding.
Yeah but isn't some of this also "Compiler Vender Implementation defined?" MSVC vs GCC vs Clang vs Intel? The language standard is vague as it doesn't force one to comply to a strict way of doing things. It's a guideline that needs to be followed so that the same written source code produces the same expected or desired results. In other words, they don't care how you do it as long as it provides the expected results. I'm just wondering if this could be different between various compiler vendors? Also doesn't the calling conventions have an effect too?
I love this guy's videos. He is revealing all the magic computers have these days.
im learning so much internally about programming as a whole. ive never really understood what happens in the background, only what ive been taught in school, all horseshit. thank you, you're a great teach
Now THAT'S what I call Forbidden C++
move over javidx9 and your gotos, we've found the real chest of forbidden c++
The real world use case for code like this is job security.
The most important thing you can do with an indispensable employee is fire them immediately. At the end of the day, the inputs and outputs and business logic are known so anything can be rewritten by a competent developer.
It's also very useful when you're stuck with a mission critical binary for which the source code is long gone. Pull it apart with HexRays, hack in what you need, and link it by hand. C++ is gorgeously stupid when it comes to linking order, so you can convince it to fudge anything for you before main() is even called.
@@bkucenski The most important thing in business is fiscal responsibility. Firing an indispensable employee gains you one salary. Rewriting and debugging someone's 20 years career can easily cost you 10 times that... because, as it turns out, the program itself quite often IS "the documentation" of all the decisions and discoveries of special cases that has happened over it's development time.
Circa 1990 I was writing 65816 assembly for the Apple IIGS and was really just getting interested in C++, which was just becoming mainstream. I was using MASM, which has a very robust macro facility and wrote a set of macros to create vtables and make virtual function calls so that I could do object oriented programming in assembly language. It was so much fun!
This has got to be the most Aussie programming channel on the internet. Incredible.
Good to actually see why classes were design for: let the compiler deal with the Vtable. Thanks!
Awesome video. I never knew the vtable pointer was placed at the beginning of the class, but thinking of it now it makes a lot of sense
Cheers mate, it is interesting how simple it all is under the hood! Thanks for watching :)
Not always the beginning
Creel, it's a real pleasure to watch your videos and partake in your years of (especially low level) language and implementation details. Thank you.
I reached most of your older videos at least twice a couple of weeks ago. I was craving that Creel and I can finally scratch it with this brand new video!
I went to school for visual C++ over a decade ago. _Definitely a bit rusty_
The main thing that stuck with me is my teacher complaining about my loops not terminating. _I can modify code, but can't write from scratch anymore_
Never even messed with memory directly, I could imagine his face if I did.
I'm of a mindset that I gotta break something to figure out how it works, and this seems a good starting point to get back into coding.
Ye
Definitely recommend learning one component at a time, if you miss something that can make even the simplest thing impossible. Javidx9 is a great RUclipsr for learning C++ ruclips.net/user/javidx9 He makes simple games in the windows console, like Tetris.
@@BoardGameMaker4108 I second that. javidx9 is great!
"my loops not terminating" so basically you mean that you were getting inside of a loop, with no way of getting out? So it's just that you coded a loop that make no sense and will never be true?
@@nicolaskeroack7860 I had strict conditions in them where they would only work as intended. It was more like I repurposed them into instances, where the technical close for the loop would be the end of the page.
I am a complete hobbyist gamedev so I learned enough of programming just so I can work with the engines and being able to read people's code but years ago I had a thirst to really learn how computers actually work so I went on a journey to learn about architecture, assembly language, C language, C++, C# etc. And I had quite a bit of "Aha!" moments one of which was "Hey its all about pointers and data and hoping around data?"Also another aha was realizing you actually working in tandem with compiler, that's the main thing in programming, that's what a language is actually, compiler puts it together in a set way the designers of that language intended and it gets translated to machine code in the end. Absolutely loved how C was just enough step up from assembly that it didn't obscure true nature of programming. As I learned about the community I never could understand how people favored some languages over the others, still cannot to this day hear somebody say "This is better than that..." Like wtf do you even know it all works?? Its all the same under the hood but of course you are going to use the one its supported in whatever framework you work in. One time I watched this one programmer video how they would hire people coming with CS degrees but no work experience and some didn't know the difference between stack and heap... Sure developing some non demanding android app you don't need to know anything about that even how OOP works but if you are developing anything that requires above average performance you will need to know how it actually all works under the hood.
Well of course it's all the same under the hood, it still makes sense to prefer some languages over others, there's syntax, readability, and frameworks like you mention, etc.
I amaze myself that I understand this video perfectly having learned all this stuff over 25 years ago. (Although I haven't used it very much.) Great video. Well done. Gut gemacht.
Thanks for this video. In the firs tpart of the video, it shown me that I was thinking right about struct in C. The fact that you should be able to cast a struct and change it's attributes as you will. :) Even though it's C++ with OO, struct are kind of OO but in C. It works pretty the same.
Very good video! It really shows how simple C++ needs to be to retain its speed compared to C and assembly.
currently in the process of researching how oo works so I can apply it to my own programming language, this video helped a ton, keep it up!
how did it go
Thank you so much for making the video! I before was modding games, so I knew that before. But I never would have thought of creating custom VTables. Before I was modifying the addresses of the VTable, but this allows for even better stuff, like function swapping for only one instance (for ex. Localplayer). I would like to see a part 2 that covers virtual inheritance and dynamic casting :D
It's good to see how much you're enjoying MacGyvering the language mechanisms!
I've programmed in C, C++, Java, a couple assembly languages, Visual Basic, C#, Racket, some other languages, and Python. It wasn't until I got to Python and saw the "self" parameter at the start of all member functions that I had an epiphany about how OOP is done behind the scenes.
This video did 2 things in addition to that epiphany. 1: It confirmed my epiphany. 2: When you showed polymorphism, it showed me that the designers who came up with OOP must have been geniuses to purposely design in polymorphism behind the scenes.
Well yeah. If you want to understand a programming language or some feature of it. It is a good idea to disassemble some code you made. That allows you to see what is going on concretely.
This brings me so back to the 90s :)
All good examples, why C and C++ are great. Thanks for the overview. ;-)
Vtables were explained to me before but now I understand that the vtables are per-class and not per-instance, I couldn't understand that before, so thank you for putting that in and insisting on it!
Nice to see you again, man
You too Mr. Coughing :)
You never fail to amaze me Creel, thanks for your time.
Thanks Creel, this is an extremely good and interesting video.
I had always thought of C as a strictly typed language until I read the book Expert C Programming: Deep C Secrets and it pointed out it was loosely typed and having more experience I realized, that yeah, you can always get to the bytes and do what you want.
Yeah, that one was an eye opener to me too. And the book that finally made me understand paged virtual memory.
Waiting for this man once coming out as a teacher in my university. That are what I wanted to hear and study on our lectures. (KPI Ukraine)
This is an excellent introduction! Would be really cool to see an advanced version of this that examines the implementation of multiple inheritance, virtual inheritance, and dynamic_cast in the presence of the other two.
What a superb reference to a Norwegian masterpiece :) That's put a smile on my face for the rest of day.
The content was excellent too btw :D
Spoiler: ruclips.net/video/jofNR_WkoCE/видео.html&ab_channel=discoveryplusNorge
14:50 "Dog Fish" made my day! :-) I really enjoyed this under the hood look at things, it makes things make more sense. I can follow pointer manipulation and intent far easier than posh words. :-)
A very entertaining, educational and overall fun video
Many thanks for making it
Great video! For me, it always helps to understand how the language works under the hood.
This was absolutely amazing to watch:)
thanks a gazillion times and keep up the great job :)
You just keep blowing my mind
The fact that the first parameter of a member function is the this pointer is useful to know if one wants to pass stateful free functions. You use bind_front on that member function and its instance, thereby creating a free function with a state that you can pass along to other functions who'd be agnostic to the fact there's a class behind it.
It's still better than no typing at all or optional typing. Languages like Python and JS are nightmares to code in when compared with typed languages. I didn't realize that until I started using C# and C to program things. Having the peace of mind of knowing that if something compiles, it'll run, unless you did something careless with memory management is priceless as a more advanced programmer. Hopefully, the game dev industry moves towards Rust at some point so that us game devs can be even more at ease while coding.
"There's better ways to call three functions." Ha! This was interesting and good fun, thanks!
I thought this was gonna be a boring old news video about C++ magic, but I really liked that demo where you created a function in assembly alongside the C++ and how you dug to find the address value. Nice stuff!
And commenting to early… the vtable thing is awesome. The vtable pointer is in data segment that is writable… mind-blown!
The Creel has returned... nice!
Another amazing video Chris man, love messing under the hood with oop for funzies, another quick way I use for getting those variable addresses, open up memory viewer in debug and type &variable_name in address bar that will throw up the memory address of var and it's contents. Great content as always mate, noice one.
Creel's throwing down the gauntlet!
Nah, great stuff as always man, cheers!
So the title should actually be: How to Break C++ OOP with Dirty Rotten Low Down tricks? Great video though, loved it!
I love your humour and the educational value you provide 😊😊
first day I that learned about pointers I finally understood what the "self" parameter was for
Great stuff! No bs and to the point, nice. I think the higher level the language is, more tricks it uses - objective c is also very interesting in this manner (class/instance structure, dynamic linking)
I feel like I'm getting in on some juicy secrets.
It's definitely fun to look at how C++ does things! Marvelous language!! Cheers for watching :)
That was so awesome! Thanks for the video Chris. Cheers :)
well damn, that just shattered most courses where I was told that those OOP practices helped with security inside a program lol, pointers seem obvious but I really thought at least some form of protection from outside writes was implemented
I really love the way you explain that stuff! Thanks for spreading the knowledge in such an accessible way! :)
Another fun point not mentioned is that single inheritance of member variables is implemented like struct DerivedStruct{BaseStruct base; int derivedVar;}; Thus any DerivedStruct pointer simultaneously points to a BaseStruct, and can be passed to functions that take a BaseStruct pointer. And if BaseStruct has a vtable pointer as its first member, then so does DerivedStruct. You just point it to DerivedStruct's vtable, and then virtual calls from inside BaseStruct functions will actually jump to DerivedStruct functions. Very easy to do in regular C. C++ just automates a lot of it.
The master himself, from the land of sunshine and snakes!
And echidnas! I saw one yesterday hahaha! Sooooo cool! Thanks for watching :)
i found this channel randomly not long ago and i cant figure out why i find his videos so comfy.
I feel its to do with how its just like hes talking and having fun.
What a cozy fella
gay
I had Dr. Brady for Microprocessor Systems. He's a huge fan of talking about weather if I do say so myself, and also hating on Windows 😂
This was very interesting! Although I knew about this "magic" I didn't know how exactly it looks in memory etc. Maybe some deep dive into the templates next?
Great idea! I did record bits on templates and constructors and destructors, but I cut it out coz it was too long! Maybe I can put it in another vid at some point. Cheers for the suggestion and cheers for watching :)
@@WhatsACreel can't wait to see said video then ;) cheers!
Seconded
@@WhatsACreel There's no such thing as 'too long'
Amazing channel, you produce awesome quality content
Super interesting video! Thank you for making those videos
The awesome and frightening power of C pointers.
Visual Studio C++ compiler allows you to see the class memory layout at compile time using the compiler switch /d1reportAllClassLayout. The real mess starts when you have multiple layers of inheritance with non pure virtual classes. You get a nice interleave of function pointers and data that may or may not be aligned depending on user #pragma. It's crucial to understand this for reverse engineering applications from compiled binaries.
Your teaching ways are really awesome !
This is a fantastic video!
This is good. Other things that can be covered are:
How is multiple inheritance implemented
How is virtual inheritance implemented
How is static_cast, reinterpret_cast, const_cast & dynamic_cast implemented
How are member data pointers and member function pointers implemented
dynamic_cast can do cross casting in an multiple inheritance hieriearchy
Compilers can optimise dynamic_cast, so that if they see you are casting from Derived to Base, they can call static_cast instead.
Static_cast is sometimes optimised as a no-op as the compiler realises it does not have to do anything.
The fact that if you call Derived classes virtual functions non-virtually, the compiler can optimise these function calls so they are called non-virtually
The RVO (return value optimisation) and NRVO (named return value optimisation)
The Empty Base optimisation
Move constructors and Move assignment operators
Returning to the subject title: "Object Oriented Programming is a Dirty Rotten Low Down Trick".
No. But you can misuse Object Orientated programming:
1) Modelling a situation with inappropriate inheritance e.g. I have a Bird class. All Birds fly. Oh, what to I do about Penguins?
2) Breaking encapsulation
Amazing video, interesting topics to watch as always.
This is fascinating, and incredibly evil! Theoretically you can use this to build add-on or plugin packages that have control over functions and data that it has no business interacting with.
Not authorized to touch core functionality at your business? Who cares! Video game allows for mods but no modifications to the engine? Sorry UE4, we now have an unstable and horribly awful workaround! Want to call a class function without a member? No problem!
Basically: "Thanks, I hate it!"
Theoretically, it shouldn't work, memory protection should keep data away from code that doesn't have permission to access it. But operating systems just do protection on a per-program basis, so within a program it's your playground.
IRL is it much of a problem? If you're writing a program, and you have access to some functions, you're "trusted" enough to access even the ones you weren't explicitly given access to. A C program can access anything in it's own memory space. You're not "supposed" to but the machine won't stop you.
I wonder if this "weird ugly internal names" trick would let you mess about with the inner parts of a game engine you bought to make a product around? Maybe run it through a decompiler and see if those names crop up, or at least useful memory addresses. You might use the same trick, find the address of something you're allowed to have, then add a few to the pointer and see what's there. In fact you might run a loop where a pointer accesses the entire memory space and dumps it during program execution. Just to see what you find. I bet those weirdo ugly names have a format you could look out for.
That's assuming all of that stuff doesn't get ripped out at compile time, when Unreal compile the version of the engine they ship to customers. Still, it happens that sometimes people leave the debugging info in. Not every time but sometimes enough to be useful.
Awesome video, thanks for sharing!
Regarding getting the address of a variable that you discuss around 10:30, you can use the "immediate" window and type "&a", that will give you the address and data.
Another trick is using the watch window and enter an expression like "&b,bb", which will show the address of b in binary format.
Fascinating how straight-forward the implementation is. Stark contrast to "modern c++".
Huh? C++ is still that simple. We just make the compiler do more and more work. Take:
```
std::array someArray = ...;
for(auto &val: someArray) {...}
```
That converts to something like:
```
{
int * val = &someArray;
while(val != someArray+5) {
...
val++;
}
}
```
Compilers don't really do this, but it's pretty close. You can see how using std::array and a range based for loop can actually be faster than using a loop counter and accessing the array by index.
how to say you are a C++ noob in a single sentence.
@@prydzen Why are you even offended by that?
@@andrew.r.lukasik who is offended? are you?
@@prydzen I hope you're still young, mate.
Great video, thanks a lot!
Cool vid!
I'd love to see more of this.
Did anyone actually think there was some kind of special, magical way the CPU understands objects? I always thought it was just a weird abstraction.
Facial expressions remind me of Jack Sparrow XD Interesting video!
Mindblowing video thanks
I just found your channel. I am also a Creel and do software dev, cool!
Awesome video! Really demystifies OOP. Does this mean that upcasting is type punning? Can you use type punning to downcast?? (assuming classes are the same size)
You certainly could! Not recommended of course, to cast from parent to child, but there's nothing in C++ stopping us from treating any block of RAM as anything we like! Well, nothing but the compiler trying to warn us that we're doing something stupid!
Hahaha, cheers for watching :)
@@WhatsACreel - There are cases in my engine where an abstract list of TParent items (for example, nodes in the scenegraph) might be cast to multiple different TChild descendants based upon their class (IE identify and then cast a TParent to a TPortal for another render pass) - is that the kind of thing you refer to when you say it's not recommended?
What pitfalls are there with this kind of access pattern?
@@JohnnyWednesday It's just a rule of thumb to not do it, but when you know you need to, then you need to. And there are several kinds of instances where it's the only way to get things to run fast.
I remember the first time I mucked about with classes, I'm a C/Assembly programmer, I thought objects were gimmicky namespace hacks, basically programmatic alternative to macros that would serve the same purpose (this was my first impression, my current understanding has obviously evolved), in my view the only 'real' valid application of OO principles is if there's a meaningful layer of abstraction between the programmer and the underlying assembly, and I don't mean libraries or frameworks, so then platforms like Java or .Net/C# -- I also don't like how colleges seem to favor OO over procedural and functional styles, going so far as to teach OO based data structs and algorithms, which the students may be shocked to find aren't as directly applicable as they might first assume, such as in functional programming languages or in compute/space critical applications where everything must be as flat/contiguous as possible --
-- anyway, really enjoy videos on higher level concepts from these kinds of perspectives!
I'm currently in college, we get a course into OO (Java), Declarative (specifically Data driven T-SQL) and Functional (free language choice) programming each. They much rather give us the toolsets to know which is which and the benefits and points of attention for all of them, rather than deep diving into a single language for 3 years. There's also a specific major we can opt in for specifically focus more on data-analysis, web-development or embedded development. So luckily there does seem to be a shift in course-structure to move away from fixed OO.
As someone who makes my living dealing with bad code, and has dealt with more than one Physics PHD's code, I actually appreciate things being taught as OO and wish those PHDs knew it...
It turns out that when they are trained to treat everything as CSV files where each column is a separate 1D array that leads to really bad programming practices. What's fun is that pandas actually does that under the hood using numpy, but because it exposes the entire table in terms of a DataFrame, it's so much easier to work with.
The key takeaway should be that just because the low level does something efficient, like hidden `this` to a name mangled function, doesn't mean that we should be constrained to doing it by hand. Better to teach students the basics, then go back and explain all the "magic" under the hood to make it work efficiently.
@@arthurmoore9488 a bit of an aside, but I think there's a fair case for the argument that we all make a living dealing with bad code lol
Dude you are brilliant
Awesome video!
12:28 Geez mate, have furry pron reference memes become so popular that even you reference them? 🤣
I guess you are a man of much and many a culture. Thanks for the vid, great as always!
@21:00 Would Function1 have access to private members of *this_ptr?
Very interesting Video!
Cheers, thanks for watching :)
I'm not 100% who the target audience is for a talk like this but an interesting motivating exercise might be going in the other direction and showing one would implement object-oriented programming in C++ if keywords `struct` and `class` didn't exist.
Your illustration skills are impressive 24:48
this video is a must watch if you are into game hacking and also help you reverse engineer routine and i guess, with this knowledge, we can implement those oop feature in C if we wanted to
Great video 👍
That vtable override trick is what i had to do for overriding some methods in precompiled proprietary DLL to make it interract with data on network rather than just accessing file localy.
There some extra fun with multiple inheritance of class with a vtable that end up with 2 vtableptr.
And in case of virtual inheritance, the offset to the subtype being stored in the vtable.
Lot of fun edge case that are not meant to be messed with and could blow up when playing with them. ^^
14:14 That's the cool thing about it. You can f.e. store an array of base class objects and call a function that is overridden in each unique child class without that child class. Or you can also easily override certain methods, knowing that your custom class will do special things at common events. In Unreal Engine f.e. there is an Actor base class and you can inherit from it and override events like BeginPlay, BeginDestroy, Tick and so on.
Yes, it is so cool that every hacker will thank you for giving them a named pointer to one of their malicious functions. ;-)
Can you use the vtable pointer change for a self programming code? Like get one or two tables with default functions. At runtime write your acustomed vtable in RAM, point the original table to it and get a custom behavior?
Thank you for video.
CREEL is the GOAT
We learned: OO is simply records with functions + polymorphism. The visibility is just a "compiler" feature. I guess you could cast to a record type too ;)
Ummm... no. OOP is a program documentation strategy. Your compiler strips all of it from your code because your CPU does not have an instruction set that knows anything about objects. Records and naming for polymorphism is one way to implement OOP in a language that doesn't offer it as syntactic sugar.
I love how because the green screen cuts our your arms it makes it look like you're using the computer with your mind lmao
Fun fact: each declaration of a class with a virtual method within a separate translation unit emits its own vtable! Do with that what you will.
Can't we use mprotect to change the VTable? I am using it to change my functions on runtime.