What exactly is NULL?

Поділитися
Вставка
  • Опубліковано 29 лис 2022
  • Keep exploring at brilliant.org/TheCherno/ Get started for free, and hurry-the first 200 people get 20% off an annual premium subscription.
    Patreon ► / thecherno
    Instagram ► / thecherno
    Twitter ► / thecherno
    Discord ► / discord
    This video is sponsored by Brilliant.

КОМЕНТАРІ • 613

  • @TheCherno
    @TheCherno  Рік тому +105

    Hope you guys enjoyed the video - if you want more C++ videos let me know! Keep exploring at brilliant.org/TheCherno/ Get started for free, and hurry-the first 200 people get 20% off an annual premium subscription.

    • @HermanWillems
      @HermanWillems Рік тому

      Why do i never hear you talk about Rust. Are you literally afraid to mention it's name? And why did you never talked about that the inventor of null is ashamed that it exists and that a language should actually NOT have "null". And why don't you just tell us there are languages without null and why they do that and that null should actually not exist?

    • @TheMR-777
      @TheMR-777 Рік тому

      @@HermanWillems Calm down mate, calm down...

    • @HermanWillems
      @HermanWillems Рік тому +1

      @@TheMR-777 oh i dont mean things personal. Im dutch im quite direct. Sorry if it sounds personal its not. I just had some questions.

    • @TheMR-777
      @TheMR-777 Рік тому

      @@HermanWillems Oh alright :)
      It's fine.

    • @pastasawce
      @pastasawce Рік тому

      Yes more cpp videos obviously

  • @BM-jy6cb
    @BM-jy6cb Рік тому +394

    I'm an old-time hardware/assembly programmer who progressed to C in the late 80's, which I found natural. In the mid 90's, I made a move to C++ and somehow just lost confidence in my ability to understand what was actually going on under the hood. As more and more features were added, I avoided them as "black magic". Your series has rekindled my confidence that it's still just bits and bytes. Thank you!

    • @kayakMike1000
      @kayakMike1000 Рік тому +11

      Yeah, compilers and hardware do a lot of heavy lifting and black magic. I have been screwing around with cortex-m stuff, in my cortex-m3, memory address 0 is where the program counter starts and holds the interrupt table address. I think the MCU core is hardwired to that address on a reset, the first IRQ routine is just the reset handler. I think this core has a memory protection unit, so I could set up certain memory locations that cannot be read from by non privileged code... Which triggers an MPU fault, which is handled by the ISR routine...

    • @thefellowbreather
      @thefellowbreather Рік тому +4

      C++ does feel a little complex. I would recommend going for Rust. It is in every way a successor.

    • @criostasis
      @criostasis Рік тому +2

      If you can’t do, become an expert in what you know and teach.

    • @criostasis
      @criostasis Рік тому +1

      Also keep learning!!!

    • @iz5808
      @iz5808 Рік тому +1

      @@human-ft3wk as far as I can see for the people who really know what kind of instructions the compiler produces when it comes to arithmetic and memory stuff it is just way too boring and redundant to learn all those 'new' over-the-top features that make programming less fun. Might be wrong but that's my perception

  • @metubr
    @metubr Рік тому +208

    I miss the c++ series so much every one is a banger

    • @flflflflflfl
      @flflflflflfl Рік тому

      Why do you miss it? It's still there!

    • @elektra81516
      @elektra81516 Рік тому +3

      @@flflflflflfl Because he isn't continuing it

    • @flflflflflfl
      @flflflflflfl Рік тому +1

      @@elektra81516 do you "miss" half life because there was never a part 3?

    • @benjioffdsv
      @benjioffdsv Рік тому +4

      @@flflflflflfl well yes that’s the point

  • @0xDeadC0de1
    @0xDeadC0de1 Рік тому +347

    there was a day where compilers didn't care if you used an uninitialized pointer and would just do undefined behavior, made some things really difficult.

    • @aloluk
      @aloluk Рік тому +43

      Yep, i was waiting for him to explain what happens when you run it in release mode vs debug without initialising it, but it never happened. I wonder if he even knows being so young.
      For those wanting the answer, in release mode the variable isn't even auto initialised to 0, like it is in debug. So for his example you wouldn't even get an exception reading address 0x8 or 0x20, it will be a random value!!!!

    • @JakobKenda
      @JakobKenda Рік тому +28

      GCC still doesn't care. It does give a warning but only if you run it with -Wall. Same with Clang, I guess only MSVC gives an error.

    • @mananasi_ananas
      @mananasi_ananas Рік тому +25

      @@aloluk I can't imagine he does not know this. He's been a professional software developer for years at EA.

    • @acuifex
      @acuifex Рік тому +4

      @@aloluk 0x20 in release mode is most likely a compile time optimization of std::string. i remember strings having some really wild and weird things like negative offsets, short strings, etc

    • @not_ever
      @not_ever Рік тому +2

      @@JakobKenda I can't reproduce this in godbolt using x64 MSVC v19.33. I only get a warning "(14) : warning C4700: uninitialized local variable 'entity' used". Maybe Cherno is using the MSVC equivalent of Werror (whatever that is, I forgot) or some other flag?

  • @uMadBrudi
    @uMadBrudi Рік тому +146

    C++ isn't even my main language, but holy moly you learn so much from these deep dive videos it's insane.
    Keep up the amazing video quality Yan

    • @omgwtfafterparty
      @omgwtfafterparty Рік тому +1

      yeah, i come from the land of python and i just learnt huge loads of stuff that can help me improve my python classes

    • @TheGreatestWorldFox
      @TheGreatestWorldFox Рік тому

      Oh, I'm staying there for a while! Can you recommend any nice places to visit?

    • @FoxSlyme
      @FoxSlyme Рік тому +1

      C++ isn't my main language as well, my main one is Russian

    • @RinLovesYou
      @RinLovesYou Рік тому

      Understanding pointers is useful in a lot of languages!

  • @giantsue
    @giantsue Рік тому +38

    Dude seriously after 10 years of working in C++ you keep giving me so much input about stuff I didn't know before. keep going.

    • @jackytaly
      @jackytaly Рік тому +3

      you’ve made it 10 years without knowing what null is? props to you

    • @giantsue
      @giantsue Рік тому +11

      @@jackytaly that's not what I said :) I liked the part where he explained why member functions that don't access member variables can be called when an instance is null.

    • @jbird4478
      @jbird4478 Рік тому +5

      @@giantsue Still though... memory layout is pretty much a core concept that you ought to have learned very early on

    • @giantsue
      @giantsue Рік тому +6

      @@jbird4478 happy for you if you did, I didn't.

    • @epolpier
      @epolpier Рік тому +3

      @@giantsue It's hard to believe you are 10 years c++ programmer lacking so many basics...

  • @jbird4478
    @jbird4478 Рік тому +39

    I like how you explain the mechanism instead of just saying "do this and this", what many people on youtube do. Nothing new for me, but people can actually learn from this kind of content, so props to you.

    • @minneelyyyy8923
      @minneelyyyy8923 Рік тому +2

      yeah, i really was not expecting this deep of an explanation. while i did already know this it was still good to see.

  • @olo90
    @olo90 Рік тому +26

    12:33 "We don't care about self, at all." That got dark suddenly 🤣

    • @SealedKiller
      @SealedKiller Рік тому +1

      We don't care about self at all, this may as well be a static function because we don't even use self.
      Sounds like a life lesson xD
      But there's some truth to that sadly..

  • @mladen777
    @mladen777 Рік тому +48

    Thanks Cherno for these videos, you’re such an amazing teacher! I hope your C++ series will cover all stuff that is used in C++ programing at some point(if thats eaven possible). It’s already one of the best places for someone to learn C++ (apart from a person coding themselves) and I think it would be amazing for someone to be able to just open your playlist and find anything they need C++ oriented.

  • @ycombinator765
    @ycombinator765 Рік тому +6

    I swear to GOD that I was havning a conversation about NULL at exercism with a mentor, and came here to YT after having finished asking my question, and here the Cherno is, having upped a video about NULLs and yt recommending it after 3 mins of upload. I feel connected lol

  • @alecmather
    @alecmather Рік тому +10

    Dude I literally love your videos. Even just you showing how you can inspect the physical memory like that?? Never knew that and that's sooo cool.

  • @benjaminclehmann
    @benjaminclehmann Рік тому +2

    If you want to hear something cursed, C (not sure about C++) compilers are required to give null pointers the value of 0, but their memory representation (like many representations within C) is undefined. This means that setting pointers to 0 and comparing pointers to 0 will work as expected, but only if you do so through a correct type (i.e. T*).
    This means for example, that calloc-ing memory to hold a pointer (or a struct containing pointers) will not necessarily give you null pointers. Same with memset, or any other way of setting the raw bytes of its value. My reading of the spec suggests that using struct foo bar = { 0 } style initializations will give the correct pointer representation.
    This matters generally on some old, kinda funky architectures, often because they were word addressed archs that had sizeof(char*) > sizeof(int*), as they used extra bits to encode an offset within a word. I assume it was helpful to set the leading bits of these addresses to denote that this address was longer than usual. The Tendra compiler lets you set the representation of nullpointers to 0x55555555, as I believe it's a compiler designed for finding obscure platform incompatibilities like this. See www.tendra.org/trans.1/

  • @mananasi_ananas
    @mananasi_ananas Рік тому +4

    Might be interesting to mention that C++ does not care whether or not you want to access a nullptr. It compiles fine. There is a memory management unit (MMU) in your PC that the OS will configure, usually making the first page of memory inaccessible. This is what makes reading from null an access violation.
    I work in embedded software with C, and before the bootloader configures the MMU, 0 is a valid address. We actually use this area of memory to store data. This is because the bootloader runs on internal CPU memory which is very limited. After initializing the external RAM we use the lowest page to store data which would not fit in the internal RAM. This is fine because the application will never be able to access this data once the MMU is configured correctly.

    • @peepeefrog_
      @peepeefrog_ Рік тому

      I would love some insight on that topic;
      But programs run in the os, and live in virtual memory space, not the real memory space. Unlike microcontrollers without os

    • @mananasi_ananas
      @mananasi_ananas Рік тому

      @@peepeefrog_ I'm no expert on Windows, but I'd imagine it is the same way. We're not programming bare metal at my job, but we are using a real-time operating system on our microcontroller. Although for us all processes the "virtual memory" will map one-to-one to the physical memory. This allows any process to set GPIO pins with the adresses found in the datasheet for the microcontroller, for example.
      You could also see it as Windows not mapping the first page of virtual memory causing a violation when trying to access address 0.
      Maybe someone with more insight on Windows can chime in with more information.

  • @ivansirtautas2513
    @ivansirtautas2513 Рік тому +2

    I usually get kind of lost in your videos 'cause they a bit advanced for me. But this one was at an epiphany level. So clear and reachable. Thank you

  • @Albert-lr7ky
    @Albert-lr7ky Рік тому +16

    Great work Cherno! C++videos is a masterpiece, hope you can keep producing it !!!

  • @avivpk
    @avivpk Рік тому +3

    Still the best C++ series out there! Could you please make a video about files in C++ ?

  • @aaronwinter7911
    @aaronwinter7911 Рік тому +18

    My guess about 0x8: It just assumes that the entity value is at address 0x0. The first 8 bytes of Entity (0x0 through 0x7) are the pointer to the parent entity.
    For C++ 0x0 is a valid memory address, so it happily returns that bit in the heap, but since we are calling ->GetName() we are actually trying to access the `string m_Name`, which starts at 0x8, since the first 8 bytes of an Entity is the parent pointer.

    • @ZeroUm_
      @ZeroUm_ Рік тому +12

      I'd just correct the part you said it "assumed" the entity was at 0x0.
      The code explicitly set the object address to 0x0, no guesses were made.

  • @DTGplays
    @DTGplays Рік тому +1

    This series is precisely what they need to teach at Universities. Entertaining, simple but detailed, easy to follow, goes into what (at least my own) University will not go into. I had lost interest in programming due to my crappy Uni course, but thanks to Cherno, I am back and happier than ever to be programming. Thank you so kindly for making this series @The Cherno, I hope to see much more in the future!

  • @chadbramwell977
    @chadbramwell977 Рік тому +2

    Great presentation! I wrote an article a while ago called "how did I crash in that function?" that went through a very similar set of steps that you have here. If you haven't planned it yet, I'd suggest a follow-up for virtual/non-virtual calls and for nested member calls. Also for references since engineers can be easily convinced that a reference somehow protects you from nullptr violations.

  • @andrasvermes
    @andrasvermes Рік тому +7

    I was just looking into defining extension methods in C# and found the syntax a bit unusual. Now I totally understand why they are written the way they are. It's kinda like what you explained but in reverse. Really interesting! Thanks :))

  • @andrisszalai1261
    @andrisszalai1261 Рік тому +6

    9:21 For the record, that is only true for structs in C. In C++, structs can have functions(=methods) -- even virtual ones --, constructors, public, private and protected members, use inheritance, be templated… just like a class.

    • @master74200
      @master74200 Рік тому

      They can in C as well, but it's a bit... Weird.

    • @user-jh4dp6ns4o
      @user-jh4dp6ns4o Рік тому

      @@master74200??

    • @master74200
      @master74200 Рік тому

      @@user-jh4dp6ns4o Well in C there's no concept of public and private elements as is common in OOP, so that's not a thing in C, but you can have structs that consists of both "member elements" (thats the data you'd normally put in a struct) and also function _pointers_. That means you can allocate a struct with the various data structures you'd usually do, and also have some of the types be pointers to functions. If you look up what a C function pointer looks like (which can also be a parameter to a function), it probably makes more sense.

    • @user-jh4dp6ns4o
      @user-jh4dp6ns4o Рік тому

      @@master74200 well you still won't have actual methods this way since function pointers will point to static methods. That means that the function you are calling with obj.fn_pointer() won't have access to obj so can't modify any of its members (unless you explicitly provide obj as one of the function's arguments)

    • @master74200
      @master74200 Рік тому

      @@user-jh4dp6ns4o no no, there's a way to do it where you can call the fn ptr on the allocated struct itself and it automatically receives the a pointer to the struct as well. But honestly I don't see the big difference when every part of it is public anyway.

  • @williamrutherford553
    @williamrutherford553 Рік тому

    Love this video, because you never answer JUST the question from the beginning. I never even would've considered that stuff about "offsetof" and when you CAN use a nullptr, but now I do! I think it's SO important in a language like C++ to explain some of the rules, and then ALSO explain all the exceptions to the rule; those exceptions really help you understand the core idea, not a specific problem.

  • @alex_8704
    @alex_8704 Рік тому +6

    One of the best videos I've seen on C++. I especially liked the part that had covered the implementation of C++ classes in terms of C structs and C functions.

    • @siddharthchauhan1129
      @siddharthchauhan1129 Рік тому +1

      Me too… I never thought of it before and now it makes so much sense… like I get it now why python has self keyword.. may be it is interpreted language and can’t have compile time benefit hence they need to put this self keyword

    • @alex_8704
      @alex_8704 Рік тому +2

      @@siddharthchauhan1129 I agree. “self” in Python make the distinction between “member” methods and “static” methods more explicit. I mostly use C for math simulations and recently tried to implement somewhat similar to a class for a struct to hold and manipulate a dataframe in C. I mostly struggled to avoid putting this struct into the heap and retaining it in the stack to keep Python-like object.method() syntax instead of object->method() but so far failed

    • @igorthelight
      @igorthelight Рік тому +1

      @@alex_8704
      #define -> .
      Would that even work? xD

    • @alex_8704
      @alex_8704 Рік тому +1

      @@igorthelight In terms of "#define" my idea was rather to "#define . ->" , but it would have ruined all other dots in my code.

    • @igorthelight
      @igorthelight Рік тому +1

      @@alex_8704 True xD

  • @billywap
    @billywap Рік тому

    Always love these videos!!! Thanks for making another one

  • @gara8142
    @gara8142 Рік тому

    This was amazing, thanks for sharing. Knowing deep down how things ACTUALLY work makes a big difference.

  • @miguelguthridge
    @miguelguthridge Рік тому +5

    This is genuinely super interesting and gives lots of insights into how C++ works under the hood

  • @tbmj
    @tbmj Рік тому +1

    You absolute legend, you just saved a project I’ve had on the to-do list

  • @floatoss
    @floatoss 7 місяців тому

    Cherno, you are one amazing human being. Thank you so much for making these videos, they are truly awesome!

  • @bunpasi
    @bunpasi Рік тому +2

    Interesting video. Never thought about the effects of calling a method of an uninitialized variable.
    2 notes for viewers.
    - Always initialize your raw type variables like int, float, pointers, etc. Only as members of classes its done for you.
    - Using uninitialized data is undefined and will not throw an error in release mode. This is one of the most occurring bugs, since you might never find out about it.

  • @giovannimadrigal3199
    @giovannimadrigal3199 Рік тому +3

    Thank you! I'm new to C++ and this series has been super helpful. If I may suggest an idea, could you make a video on how to read data from a gzipped file line by line regardless of how long the lines are?

  • @ramonecheverria5856
    @ramonecheverria5856 Рік тому

    this is so educational omg the way you connected the dots, great work!!

  • @shalip
    @shalip Рік тому +1

    WOW, this is one of your best explanations ever. Thanks alot for making me understand it fully :)

  • @louiscresswell5436
    @louiscresswell5436 Рік тому

    clicked on this video to get the answer that was explained in the first minute or so. So glad I stayed, very interesting. The funny thing is I don't see how this knowledge is going to help me with my work but I'm still glad to know it. Hope you do more videos like this. The information was very concise and well explained :)

    • @PedroOliveira-sl6nw
      @PedroOliveira-sl6nw Рік тому

      I don't know if it can help with your work but one thing this knowledge can give you is how to hack into classes. Suppose that you are given a class A { private: int value; }; and you can reinterpret_cast it to class A_hacked { private: int value; public: int get_value() { return value; } }; which you can use to get the value.

  • @yobonez
    @yobonez Рік тому

    This explaination was soo clear, thank you!

  • @GamingDemiurge
    @GamingDemiurge Рік тому

    Best explanation of null I've ever seen. Your videos are clear and on point. Love it.

    • @HermanWillems
      @HermanWillems Рік тому

      He did not explain why NULL is literally a mistake in languages who have it. And that NULL has costed billions and billions of dollars. There is a clear reason why Rust for example has no NULL. Even the inventor of NULL is ashamed for it and said it is his biggest mistake and said it should not exist. Null should not exist.

  • @zdspider6778
    @zdspider6778 Рік тому

    This was an important video and I think you nailed it. Well done.

  • @lorincmate
    @lorincmate Рік тому +4

    As an Embedded C dev, this episode helped me a lot to understand C++. Thank you

    • @mindblower113
      @mindblower113 Рік тому

      Sorry but how much do you earn monthly?

  • @yillin
    @yillin Рік тому

    This was a great video. I am, for the most part, self taught when it comes to c++ programming. That is to say, I've been through books and tutorials but my knowledge of real world application is zero. However, Cherno's videos are still enlightening and very interesting to watch, even if I will never find myself applying this outside of a hobbiest's interest. Thanks for the video.

  • @randomgeocacher
    @randomgeocacher Рік тому +2

    An addition; C/C++ code halting on null pointer dereference should be considered implementation specific. You could memory map 0/null to your process if you are insane, and not all non-memory managed architectures crash on access to 0/null.

  • @Basel-ll8fj
    @Basel-ll8fj Рік тому +1

    This was really deep, before I saw the video I thought I knew what null really is and just watched it for fun,
    good job Cherno 👌👌

  • @hectormejia499
    @hectormejia499 Рік тому +2

    holy crap this is awesome, you just became one of my favorite programming youtubers

  • @mikefochtman7164
    @mikefochtman7164 Рік тому +1

    Great explanation. Used to argue with a coworker that NULL COULD in theory be defined as so long as it was sure to be a read access violation. 0x00 just happens to be a read violation on all platforms. But a lot of legacy code/ systems would break because many times it would simply be tested against the integer zero. I know that was quick and easy, but seems like it was mixing data types in a messy way. I always preferred to use NULL or nullptr as it keeps the proper abstraction concepts. Not so 'implementation specific'.
    Like using an integer value for a Boolean. If you change some details of implementation, you probably will break things. But if you stick with proper typing, much safer.

  • @pnuema1.618
    @pnuema1.618 Рік тому

    This video is gold in the context of understanding "this". Thanks!

  • @kin-dza-dza3929
    @kin-dza-dza3929 Рік тому

    Your videos are so helpful. Thank you man.

  • @andrez383
    @andrez383 Рік тому

    Hi Cherno awesome video! Please keep on making C++ videos. They are the best ones outthere.
    Also your Walnut video and repository is gold for unexperienced hobby programmers like me. If you would ever show how to port an app built on your Walnut framework to Mac that would make me veery happy :)
    have a good one... cheers

  • @duminduakalanka9340
    @duminduakalanka9340 2 місяці тому

    I was watching the original Cpp videos from 6 years ago and suddenly stumbled on to this video. Wooohhh, Dude has changed so much😀. I love it.

  • @kraiw0
    @kraiw0 Рік тому

    this was such a good video :) ran into this just two days ago, thanks for the explanation

  • @s1nister688
    @s1nister688 Рік тому

    This is some amazing stuff. Thanks for making a video on it.

  • @stephenreaves3205
    @stephenreaves3205 Рік тому

    Super helpful video. You also inadvertently explained why rust methods take in 'self' as the first parameter. Turns out C++ had been doing it for years and just never told you

  • @milasudril
    @milasudril Рік тому +1

    If gcc can infer that you will dereference a nullptr, it sometimes replaces the actual memory de-reference by an ud2 instruction, which is a clever way of crashing (no triggering of pagefaults, and no involvement of syscalls). This results in a SIGILL instead.

  • @VictorMistral
    @VictorMistral Рік тому +2

    NULL is not nullptr, one is a int with the value of 0, the other is a ptr to the zeroth position in memory.
    It makes a important distinction for some fct overloading handling since the type is not the same!

    • @Raspredval1337
      @Raspredval1337 Рік тому

      nullptr is not a pointer tho 😏 It is an object that can be casted to a pointer of any pointer type, and the value of that pointer would be 0. And on some systems, NULL is also a pointer, and not a number, probably a void* or some compiller intrinsic value, that is treated as a memory address

  • @avdhendrasingh9994
    @avdhendrasingh9994 Рік тому

    these kind of video will keep my foundation even more strong

  • @yosefshawah3019
    @yosefshawah3019 Рік тому

    Absolute legend mate! Thanks

  • @Karlemilstorm
    @Karlemilstorm Рік тому

    I learned quite a bit from this. Thank you!

  • @mbehboodian
    @mbehboodian Рік тому

    Man... I literally missed you. The last videos I saw from your channel were OpenGL series from 5 years ago! I'm not sure how YT suggested a video while I wasn't subscribing, but man, you got older :)
    Keep it up my friend...

  • @freezinfire
    @freezinfire Рік тому

    Not only null, but my concepts of structs, classes and memory address allocation were all cleared. Thank you.

  • @Byynx
    @Byynx Рік тому +1

    Priceless explanation!!!

  • @gamesucht2097
    @gamesucht2097 Рік тому

    thankyou. that gived me a new understanding of C++ and compiled programming languages and how they work

  • @c2ashman
    @c2ashman Рік тому

    Amazing content. Great stuff.

  • @minneelyyyy8923
    @minneelyyyy8923 Рік тому +5

    It would be really cool to see you cover virtual memory. covering how the operating system actually loads your code, how it manages your memory, and stuff like that could be so cool. you seem to have an above average understanding of these low level topics, which is why I am bringing it up. virtual memory is a very very VERY important concept in modern operating systems and a video about it could contain some useful information for programmers.

    • @piechulla1966
      @piechulla1966 Рік тому

      If your application really gets into this situation, you should rather buy additional main memory.

    • @tablettablete186
      @tablettablete186 Рік тому

      @@piechulla1966 What? Virtual memory is not just important to use with paging, it is also useful to isolate processes.

    • @piechulla1966
      @piechulla1966 Рік тому +1

      @@tablettablete186 The definition(s) of "virtual memory" are not entirely clear to me now. Cherno should perhaps really take on the topic 🙂

    • @tablettablete186
      @tablettablete186 Рік тому

      @@piechulla1966 Agree, but perhaps I can help a bit.
      Virtual memory is nothing more than an abstraction over the physical address of memory.
      So, the process can only access virtual addresses and they are translated on the fly by the hardware when being in use.
      In another way, a process will read address 1000 and the hardware will map it to a physical address (let's say 3888) and return its value.
      This helps in 2 things:
      1. Isolation: map different physical addresses to different processes, and they all see (0 to 2^64)
      2. Paging: you can use use this abstraction to map virtual addresses to your SSD
      Source: Tannenbaum's book Modern Operating Systems (or something similar)

    • @piechulla1966
      @piechulla1966 Рік тому +1

      @@tablettablete186 When does this come into play? Say I do:
      m_pSomething = new int[nNumOfNeededSomethingSlots]; // Nevermind C26409: Avoid calling new and delete explicitly, use std::make_unique instead (r.11).
      or
      std::vector vLfSomething;
      // ..
      vLfSomething.push_back(lfSomeValue);
      Usually I do not care what goes on behind the scenes. As long as I don't have to worry about it actually being swapped to disk (performance decline with a factor 10,000 or so for an HDD, not sure about SSDs).
      Seems to me that swapping to disk was interesting in the 1980ies (back then memory was very expensive) and is no longer important nowadays. Also, looks like it is turned off by default in Win 10, Win 10 IOT, and Win 11.
      Isn't the address space that a process is assigned by the operating system always "virtual" anyway?
      So many questions. I know that I know nothing. Which I mostly do not care :-)

  • @veerbhadra7332
    @veerbhadra7332 Рік тому +1

    Hello Cherno,
    I really like the way you teach. Could you please make a tutorial on design patterns

  • @sir890
    @sir890 Рік тому

    great explanation! thanks for the video!

  • @s1b8
    @s1b8 11 місяців тому

    Awesome , simple explanation!

  • @embeddedbastler6406
    @embeddedbastler6406 Рік тому +2

    The standard does not specify that nullptr has to refer to the address 0. It only says that the literal value of 0, in a pointer context, must be interpreted by the compiler as nullptr.
    Therefore 0 *can* be a valid memory address.

  • @nehadyounis
    @nehadyounis 3 місяці тому

    this is one of the best yt videos i've ever seen

  • @idkkdi8620
    @idkkdi8620 Рік тому

    Wow, this was awesome! Thanks!

  • @roelvanhout5449
    @roelvanhout5449 Рік тому +1

    Now to complete this, do virtual functions and how vtables fit into memory layout of objects. . And how the pack pragma affects these values in msvc.

  • @imereque
    @imereque Рік тому

    Hi Cherno!
    It's amazing how much depth there is in the humble zero!
    Would you consider making a video on variadic templates or custom allocators?

    • @deang5622
      @deang5622 Рік тому

      It usually isn't zero, it's more likely to be:0xFFFFF...
      As many F's as there are in a memory address...

  • @danielnavarrete8571
    @danielnavarrete8571 Рік тому

    Great in-depth explanation!

  • @absolute_zero982
    @absolute_zero982 Рік тому

    this video make my day bro ty

  • @sigmareaver680
    @sigmareaver680 Рік тому +20

    Imagine if you had a class so large that when you access the offset of a member variable, it ends up being inside valid memory.

    • @ohwow2074
      @ohwow2074 Рік тому

      How would you store the object in the memory in the first place? I think what you said doesn't make sense since it probably can't happen.

    • @Sandeep-cz7ls
      @Sandeep-cz7ls Рік тому +5

      @@ohwow2074 forgive me if i’m wrong (i come from a java background), but can’t we try initializing so many nested pointers that ultimately point to 0x0, and the offset from these pointer locations add up to a valid location?

    • @TheMaginor
      @TheMaginor Рік тому

      @@Sandeep-cz7ls I'm not sure what you mean by nested pointers, but if it is a member variable of a class that points to another member of the same class, that still only adds 8 bytes total to the class. The next pointer is located at a potentially completely different location in memory.

    • @bluesillybeard
      @bluesillybeard Рік тому +4

      a class with millions of members... sounds intense

    • @SerBallister
      @SerBallister Рік тому +1

      @@bluesillybeard Arrays can puff up your class sizes in no time.

  • @enesk2120
    @enesk2120 Рік тому

    bro, this was just awesome

  • @jaime7295
    @jaime7295 Рік тому

    These videos talking about general c++ concepts are really interesting

  • @greob
    @greob Рік тому

    Interesting, thanks for sharing!

  • @htmCode
    @htmCode Рік тому +2

    i can't imagine that i actually have an java exam tomorrow and i'm here watching this c++ video because of how great it is

    • @snlagr
      @snlagr Рік тому

      how was the exam bro?

  • @aspidinton
    @aspidinton Рік тому

    Congrats with 500k

  • @siddharthchauhan1129
    @siddharthchauhan1129 Рік тому

    I kid you not.. after I learn things from you.. I sure sound really smart in front of my colleagues.. your explanations are so precise and cover very critical points..

  • @ErrorJan
    @ErrorJan Рік тому

    16:45 THIS MAKES SOO MUCH SENSE! Wow. I love C++

  • @RequiDev
    @RequiDev Рік тому +2

    Could've used Godbolts Compiler Explorer to showcase what actually happens under the hood as well.

  • @smarttrupti2007
    @smarttrupti2007 Рік тому

    Wow, Amazing session👍

  • @pejsh726
    @pejsh726 Рік тому

    What a great video... and I don't even work with C++, still really great and interesting. Thanks.

  • @444limm
    @444limm Рік тому +2

    I really like c++ series on this channel

  • @xsandre-l
    @xsandre-l 11 місяців тому

    Any chance we can get a video on cpp modules? that would make my year thanks😄

  • @themyuul6559
    @themyuul6559 Рік тому

    fantastic video, cheers

  • @MutantOpa
    @MutantOpa Рік тому

    I'm always surprised how helpfull C++ and C code is when thinking about assembler and reverse engineering. Thank you | good video :)

  • @skeleton_craftGaming
    @skeleton_craftGaming Рік тому

    There's a physical address zero but it is always reserved by the operating system, and also the operating system most likely nowadays doesn't allow you to use direct addressing anyway, so the virtual address tables that the OS uses probably doesn't have a zero entry.

  • @DeathStar95
    @DeathStar95 Рік тому

    beautifully explained!

  • @stera182
    @stera182 Рік тому

    That’s why the typical java null pointer exception felt so wrong for me as a C++ dev, the problem is never to use a null pointer, the problem is using the memory pointed by a null pointer.

  • @stupidm
    @stupidm Рік тому

    nice explaination!

  • @sanctuary_of_soul
    @sanctuary_of_soul Рік тому

    Very very cool! Thanks

  • @stdDizzasTeR
    @stdDizzasTeR Рік тому

    Nobody teaches this stuff and I'm always caught off with such knowledge Cherno!

  • @Ghost-Snow-Rabbit
    @Ghost-Snow-Rabbit 9 місяців тому

    Thank you, it's so good to know how it works.

  • @eplusplus
    @eplusplus Рік тому +1

    Interresting! What font are you using? It looks clean

  • @Debreky
    @Debreky Рік тому

    Brilliant video!

  • @kraiw0
    @kraiw0 Рік тому

    also off topic - but I really like the color grading you have on your videos lol

  • @charlesmuzhanye4144
    @charlesmuzhanye4144 Рік тому

    Wish I could double like this, great content

  • @revan1139
    @revan1139 Рік тому +1

    This raises the question: How do you write to address zero when necessary? Let's say I'm on an embedded system and some hardware (maybe ram) is located/starts right at adress zero on the bus, how to read/write to it? Is it like a convention to never actually hook something to adress zero on the bus?

  • @TravisVroman
    @TravisVroman Рік тому

    I like how the thumbnail used black, whose RGBA would also be 00000000 - same as a nullptr. 🤔
    Also dropped in to say congrats on 500k subscribers!

  • @rookiecookie8258
    @rookiecookie8258 Рік тому

    Pointers, even if they are double, triple and so on, always seemed so easy. You allocate stuff, you de-allocate stuff and do null checks.
    Don't know why most high level languages don't provide pointers but I guess it's to avoid boilerplate code and/or errors that come from mishandling them

  • @MrSasYo
    @MrSasYo Рік тому

    Great video!