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

  Рет қаралды 5,505

Mike Shah

Mike Shah

Күн бұрын

Пікірлер: 22
@georgerochester4658
@georgerochester4658 Жыл бұрын
Excellent insights and clear presentations.
@MikeShah
@MikeShah Жыл бұрын
Cheers!
@dhanushs1802
@dhanushs1802 2 жыл бұрын
Great video as always. Very well explained. Thank You.
@MikeShah
@MikeShah 2 жыл бұрын
Cheers, thank you for the kind words!
@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 Жыл бұрын
You're right -- and std::span helps solve that problem for any contiguous data structure by representing the containerer as a pointer and length: kzbin.info/www/bejne/hYLYY6OQfrGdetE
@videofountain
@videofountain Жыл бұрын
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.
@insadeyt
@insadeyt 3 ай бұрын
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 3 ай бұрын
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
@guilherme5094
@guilherme5094 2 ай бұрын
👍!
@tamoghnobanerjee5053
@tamoghnobanerjee5053 Жыл бұрын
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 Жыл бұрын
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 Жыл бұрын
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 Жыл бұрын
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!
@JaswinderSingh-dz1ui
@JaswinderSingh-dz1ui Жыл бұрын
100
@MikeShah
@MikeShah Жыл бұрын
Cheers
@didgerihorn
@didgerihorn 2 жыл бұрын
vector is too expensive. Use std::span
@MikeShah
@MikeShah 2 жыл бұрын
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 жыл бұрын
@@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 жыл бұрын
@@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)
За кого болели?😂
00:18
МЯТНАЯ ФАНТА
Рет қаралды 3,4 МЛН
FOREVER BUNNY
00:14
Natan por Aí
Рет қаралды 35 МЛН
Turn Off the Vacum And Sit Back and Laugh 🤣
00:34
SKITSFUL
Рет қаралды 9 МЛН
Beat Ronaldo, Win $1,000,000
22:45
MrBeast
Рет қаралды 40 МЛН
Passing Arrays and Vectors to Functions - C++ Tutorial 21
16:28
Caleb Curry
Рет қаралды 10 М.
Should I pass by const reference or by value?
10:45
The Cherno
Рет қаралды 110 М.
C++ Examples - Pass by Value vs Reference vs Pointer
11:53
Caleb Curry
Рет қаралды 23 М.
31 nooby C++ habits you need to ditch
16:18
mCoding
Рет қаралды 829 М.
C++ Pass by Value, Reference, Pointer Explained
9:33
Caleb Curry
Рет қаралды 45 М.
why do void* pointers even exist?
8:17
Low Level
Рет қаралды 389 М.
За кого болели?😂
00:18
МЯТНАЯ ФАНТА
Рет қаралды 3,4 МЛН