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.
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
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
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
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.
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.
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?
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.
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!
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).
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!
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 :)
@@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)
@@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)
Great video as always. Very well explained. Thank You.
Cheers, thank you for the kind words!
Excellent insights and clear presentations.
Cheers!
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.
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
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
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
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.
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.
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?
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.
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!
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).
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!
👍!
100
Cheers
vector is too expensive. Use std::span
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 :)
@@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)
@@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)