Passing arrays into functions(array decay to pointer)- prefer std::vector | Modern Cpp Series Ep. 28

Поділитися
Вставка
  • Опубліковано 3 лис 2024

КОМЕНТАРІ • 22

  • @dhanushs1802
    @dhanushs1802 2 роки тому +1

    Great video as always. Very well explained. Thank You.

    • @MikeShah
      @MikeShah  2 роки тому

      Cheers, thank you for the kind words!

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

    Excellent insights and clear presentations.

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

    Amazing video! One question though. I understand that when passing the `std::array` type from the STL library to a function, we are required to specify the size of the array, as in `function(std::array)`. However, it raises the question of the purpose of this practice if we still have to explicitly indicate the size to the compiler. I envisioned that the advantage of using the array STL type would be expressed as follows: `function(std::array)`, allowing us to work with a container that inherently knows its own size. I must admit that the necessity to specify the size remains somewhat bothersome when dealing with arrays in general.

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

      You're right -- and std::span helps solve that problem for any contiguous data structure by representing the containerer as a pointer and length: ua-cam.com/video/OQu2pZILjDo/v-deo.html

  • @insadeyt
    @insadeyt 2 місяці тому +1

    hey mike, iam super new to programming and my question is that what is size_t?? i ve learned so much from this playlist. all thanks to you but i am so confused at this point what in the world is this? size_t and vectors

    • @MikeShah
      @MikeShah  2 місяці тому +1

      Welcome! size_t is an 'unsigned integer', usually an unsigned long or some type that can otherwise hold lots of integer values. By convention we use it to ensure we don't access a negative index, as well as to indicate a 'count' or 'index' of something

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

    Thanks. You mentioned C libraries. That is a concern for some people. You may use some of the std::vector features to pass information to a C library especially if the use of library is const. In some cases, you can define a std::vector and use it directly 99% of the time. Next, the code can temporarily adapt for the C library function call without a defining a variable created as a plain C array. I have not seen your whole series. A plain array declared within a struct or class allows more traits to be passed.

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

      You can certainly pass data into C library functions from something like std::vector (e.g. .data() member function). I think the concern is rather if I need to wrap my C++ code and use it in a C library, I need to use raw pointers as parameters versus some specific c++ feature.

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

    You have mentioned passing a vector to prevent this decay. Can we also pass an int array reference int& arr[] will that prevent the decay? Will the compiler just swap the reference with the array that is being passed when the function is called?

    • @MikeShah
      @MikeShah  11 місяців тому +1

      Will int& arr[] will still decay to pointer as I understand. So we probably want to use std::array specifically, vector, or even better (explained later in this series) to use a std::span for other containers where we need length information.

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

    sort of getting lost right here 9:00 because as far as I know, passByReference used when we are mutating data.
    Whereas, here in the given timestamp, you said since we are not mutating data, we can use passByreference. Doesn't we should leave it as it is?(passByValue)
    Clearing this confusion is highly appreciated. Thanks Mike! Great content though!

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

      Cheers! If we are not modifying the data in the container, we pass by reference WITH the 'const' qualifier. However, without the 'const' qualifier, we can indeed modify data (and avoid a copy).

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

      Hey @@MikeShah got it. I was aware that the const used to let compiler know not to optimize the value but pass by reference is now clear as mud.
      thanks again for the series!

  • @guilherme5094
    @guilherme5094 2 місяці тому +1

    👍!

  • @JaswinderSingh-dz1ui
    @JaswinderSingh-dz1ui Рік тому +1

    100

  • @didgerihorn
    @didgerihorn 2 роки тому +1

    vector is too expensive. Use std::span

    • @MikeShah
      @MikeShah  2 роки тому +3

      I will introduce std::span with C++20 after I do a lesson std::vector ;) I'm excited to also show folks some cheaper data structures like std::string_view (instead of passing around strings) later on :)

    • @didgerihorn
      @didgerihorn 2 роки тому +1

      @@MikeShah looking forward to that :)
      I know this is a beginners' video, but the point I dissgree on is the mindset to default to heap allocated containers.
      The function that the data is passed into should not force the csller to make a heap allocation (maybe I'm biased since I work on microcontrollers)

    • @MikeShah
      @MikeShah  2 роки тому +1

      @@didgerihorn Hehe, you have much different constraints :) Agreed, this will be a nice trade off to discuss, and from an embedded C or C++ programmer perspective, not having (what seems like) an infinite heap does change how you choose data structures :) Progression for some future lessons will involve the standard C++ STL containers (Sequence and associative containers), then I'll shift a bit to sequencing view data structures (std::string_view, std::span)