Solving Common Pointer Conundrums - Loris Cro

Поделиться
HTML-код
  • Опубликовано: 14 янв 2025
  • НаукаНаука

Комментарии • 33

  • @WilliamCulver
    @WilliamCulver 3 месяца назад +5

    zig is getting me back into low level code after decades of hiding from the crazy and old C preprocessor. Reading macro laden code haunts my dreams and tooling only helps me a little. I'm very thankful for this language :)

  • @charlesleninja
    @charlesleninja 2 года назад +53

    I just started laughing when he said "If you do not know what pointers are, I'm sorry but I'm just not gonna address that".
    Like, I knpw it wasn't meant, but it's still a good pun imo

    • @seanknowles9985
      @seanknowles9985 2 года назад +9

      Your comment is failing to compile (line 3 > error: Like, I knpw it wasn't meant)

    • @jaybestemployee
      @jaybestemployee 7 месяцев назад +1

      I thought sometimes it is simpler to just introduce the concept right away than to waste time explaining why not to do so. Concept of a pointer is not difficult if visualized well

    • @aleksszukovskis2074
      @aleksszukovskis2074 6 месяцев назад +3

      @@seanknowles9985 parsing error: "Like, I knpw it wasn't meant" found on line 4

    • @shrek95372q
      @shrek95372q 4 месяца назад

      pointer is a stupid abstraction, use raw adresses like a pro

  • @valentinrafael9201
    @valentinrafael9201 2 месяца назад

    What I some times do @27:22 when I just dont wanna deal with type mismatches in testing, is set a comptime parameter to accept a type, which I send by doing typeof on the whateverI am trying to send to the function, and the actual parameters that the function will use will be set to that comptime type. I sometimes do the same for the return values ( from within that function )

  • @blacklistnr1
    @blacklistnr1 2 месяца назад

    Perfect scope of clarifying zig pointers for programmers. I've dealt with slices, raw pointers, typed pointers etc. But I ran by accident into a slice type error and couldn't figure it out. I also found the memory ownership of sized arrrays unexpected, I thought they were just a typed pointer.

  • @miltonio94
    @miltonio94 2 месяца назад

    Really good video, thank you for making it and posting. I fixed a bug in a program I'm writing and I had no clue how my changes fixed it, this video explained exactly why it was broken in the first place and how my changes fixed the issue

  • @totheknee
    @totheknee 3 года назад +14

    I love the font you are using for the presentation code. Is it Proggy?

    • @ZigSHOWTIME
      @ZigSHOWTIME  3 года назад +6

      fonts.google.com/specimen/Press+Start+2P

  • @balen7555
    @balen7555 4 года назад +3

    At 17:54, it was said that the first line( *[*]MyType ) is a single pointer to a sequence of unknown number of "MyType"s but shouldn't it be a single pointer to a pointer to a sequence of unknown number of "MyType"s or am I mistaken here?

    • @kristoff-it
      @kristoff-it 4 года назад +7

      I'm not perfect at describing weird pointer combos, but in general the important part is that [*] is a pointer to an item, and all the brackets are telling you that by adding sizeOf(MyType) to that pointer you might have another instance of MyType. The way you can know that's the case or not is through a separate scheme (e.g. you have a second argument that conveys how many, or you know there's going to be a sentinel instance of MyType that tells you it's the end of the line). So with that in mind, *[*]T means that you have a pointer pointing to a pointer to T, so you have a **T, but you know that the pointer you get by dereferencing the initial value, might have other Ts in front of it. So if `a` is the original value, `a.*` is a pointer to T, `a.*[1]` is a T (index access to a pointer adds the size and dereferences).
      I hope that is a good explanation, also take a look at the compileLog messages in this snippet: zig.godbolt.org/z/EYzrqT

    • @balen7555
      @balen7555 4 года назад

      @@kristoff-it That was a very clear explanation, thank you!

    • @shoulderstack5527
      @shoulderstack5527 4 года назад

      @@kristoff-it I think your description in your response to Balen is correct, from looking at the Zig docs and the compileLog link you gave. But I think that means that your first two explanations (17:54) must be wrong... or at least a slip of the tongue. shouldn't they both have one more level of indirection than you said because the [ ] are also a dereference themselves.

    • @kristoff-it
      @kristoff-it 4 года назад

      @@shoulderstack5527 I think I might have used "sequence" and "sequence of pointers" a bit too liberally (to mean the same thing). Looking at the timestamp you linked, the first example is a single pointer that, when dereferenced, brings you to an array of pointers to MyType, but you don't know the length of that array. So to get to an instance of MyType (so not a pointer-to, but straight up a MyType value) you would write something like `my_var.*[2]`. The second example has the array in the outer layer, so you'd need to do `my_var[2].*` to obtain a MyType. Of course, this is all assuming the [*] have enough elements to accommodate for my example.

    • @shoulderstack5527
      @shoulderstack5527 4 года назад +5

      @@kristoff-it Also, I wanted to say 'Thanks so much' for these Zig Showtime and the Advent of Code videos. They really help to lower the cost of entry for someone like me. It means I can afford to devote some time to finding out if Zig is a good choice for me because you made it so easy to explore. Thanks!

  • @PLAYGAME-wj9bw
    @PLAYGAME-wj9bw 11 месяцев назад

    42:04 this doesn't seem to work as of today

  • @mephistotel87
    @mephistotel87 3 года назад +1

    Thank you! You are awesome!

  • @SohomBhattacharjee
    @SohomBhattacharjee 2 месяца назад

    whoever. came up with the citrus duck. THANK YOU

  • @kanji_nakamoto
    @kanji_nakamoto 3 года назад +1

    Thanks 👍 keep sharing!

  • @mtoon0
    @mtoon0 Год назад

    36:42

  • @diegosorte
    @diegosorte 4 года назад +1

    Thank you very much :)

  • @DooMWhite
    @DooMWhite Год назад

    Good stuff

  • @HairyPixels
    @HairyPixels Год назад

    How is an optional pointer the same size as a pointer? doesn't it need some flag to know if it's set or not?

    • @shilangyu
      @shilangyu Год назад +1

      Null (address 0) represents the nil case of an optional value. This is similar to Rust's null pointer optimization where for instance the size of Option is the same as the size of &T. The 'nil' case can be safely represented as address 0 because pointers in rust (and zig) cannot be null.

    • @HairyPixels
      @HairyPixels Год назад +1

      ​@@shilangyuoh that's interesting, nil is reserved by virtue of it being illegal on raw pointers. Makes sense now thanks.

    • @TheMachina42
      @TheMachina42 11 месяцев назад +1

      @@HairyPixels Yeah basically Optional pointers in Zig or Rust are just syntactic sugar for branching on null. The equivalent in C would be :
      foo() {
      ... stuff happening.
      my_type *ptr = memory_reference_of_something();
      if (ptr == NULL) // aka pointing at address 0.
      return (error);
      ... stuff happening.
      }
      The problem with null pointer is that they can be null, and if they are not systematically checked than it's a big deal, Optional, put that burden on the type system and the compiler. In Zig or Rust you can't access a pointer unless you are explicitly acknowledging that it can be null, either by handling the null case, or by assuming it to be unreachable.

  • @neohashi3396
    @neohashi3396 Год назад +2

    "if you know python.." goes on with an example where a slice in a reference and an assignment is a copy.. the exact opposite of python :D

  • @fjadron
    @fjadron Год назад

    ty nise video explaining pointers in zig . i have this idea hat insted of "&adress" it should be "adress." so the logic whit the pontiers would be easyer adress. *pointer .value insted of &adress *pointer . value

  • @GLiBERN
    @GLiBERN Год назад

    At 14:20 it should be "you first unpack the optional", not "you first unpack the pointer".

  • @charleslegates9231
    @charleslegates9231 8 месяцев назад

    ruclips.net/video/VgjRyaRTH6E/видео.html
    feature suggestion: suggest the fix in the compiler error like rust does

  • @Iridescence
    @Iridescence 4 года назад

    Nice :)