ไม่สามารถเล่นวิดีโอนี้
ขออภัยในความไม่สะดวก
ต่อไป
เล่นอัตโนมัติ
#BB7 Pass by Value or Reference - What's the difference?Ralph S Bacon
มุมมอง 13K
WHY did this C++ code FAIL?The Cherno
มุมมอง 246K
Master Pointers in C: 10X Your C Coding!Dave's Garage
มุมมอง 297K
SUPERMARKET ด้วยกันHEARTROCKER
มุมมอง 798K
JAEHYUN 재현 'Dandelion & Roses' MVSMTOWN
มุมมอง 1.3M
🔴Live โหนกระแส ศึกชิงอพาร์ทเม้นท์ ผัวเมียหย่ากันแบ่งสมบัติไม่ลงตัวโหนกระแส [Hone-Krasae] official
มุมมอง 603K
Ice cold between the sticks 🥶Bundesliga
มุมมอง 8M
Weak Pointers in C++ (std::weak_ptr)The Cherno
มุมมอง 46K
#BB10 Namespaces & Macros for Arduino (and other) microcontrollersRalph S Bacon
มุมมอง 14K
Andrew Kelley Practical Data Oriented Design (DoD)ChimiChanga
มุมมอง 75K
#265 Me & My 3D Printer - Just a quick chat!Ralph S Bacon
มุมมอง 7K
#224 🛑 STOP using Serial.print in your Arduino code! THIS is better.Ralph S Bacon
มุมมอง 225K
#BB4 MOSFETs - From an Arduino PerspectiveRalph S Bacon
มุมมอง 22K
why are switch statements so HECKIN fast?Low Level Learning
มุมมอง 399K
Compilers, How They Work, And Writing Them From ScratchAdam McDaniel
มุมมอง 144K
POINTERS in C++The Cherno
มุมมอง 1M
โกงการแข่งชิงแชมป์โลก #shorts #freefireZz Man
มุมมอง 562K
นายกฯ ต่อจาก "เศรษฐา" ใครมีโอกาสได้ตำแหน่งมากที่สุด? | Thai PBS NewsThaiPBSNews
มุมมอง 325K
ลูกชิ้นทอดเจ๊ซิมจักรวรรดิ เหลาปลาร้ากุ้งสด ใครจะอดใจไหว!! | BB MemoryBB Memory
มุมมอง 235K
PASULOL กุ่ย นาย ฮอล และชีวิตติดหน่วงPASULOL
มุมมอง 2.5M
Running With Bigger And Bigger FeastablesMrBeast
มุมมอง 16M
[UNCUT] หวั่นขาดความเชื่อมั่น ศาล รธน. วินิจฉัยคดีถอนถอนนายกฯ เศรษฐา | คนดังนั่งเคลียร์ | 14 ส.ค. 67ช่อง8 : Thai Ch8
มุมมอง 427K
Ice cold between the sticks 🥶Bundesliga
มุมมอง 8M
Welcome back, Ralph. You've been missed.👍
Thank you! And a Merry Xmas to you too! 🎄🎅
Bacon is always missed when not around!!! 🤣😂🤣
Yes but he is missing again for quite some time now
@@hansdegroot652 i was just thinking the same as you today. i hope all is well. x
Miss him as well. Hope he's doing well.
Pointers and References are always a tough subject to explain, and so dependent on using precise language to accurately describe.
I think you did a great job of both describing and demonstrating the subject.
I have for years used "int &x" but can see that "int& x" is actually much more accurate. And the double use-meaning of * is indeed confusing.
Thanks and hope 2024 is a good one for all! 73...
Glad it was helpful! Ironically, since that video I've had to use pointers because the power that be at Espressif decided their interface to tasks was going to be a pointer. So it's always good to be aware of (as well as beware of) pointers!
Hope to see you back soon Ralph. I appreciate that you need your life balance, but once you have us hooked, you’re easily missed. Take care. Talk soon 👍🏾🤓
I hope so too!
Good to see you again Ralph, it has been some time.
It has been a while! Merry Xmas, Happy Holidays! 🎄❄️☃️
Was wondering where you where. Nice to have you back again.
Hey, thanks! And a Merry Xmas to you too! 🎄🎅
Good to see you back
Merry Xmas, Happy Holidays! 🎄❄️☃️
Hi Ralph, so glad to see you back. I have really missed you, I have learnt so much from your tutorials over the years. please keep it going.
Noted!
Merry Christmas Ralph
Merry Xmas, Happy Holidays! 🎄❄️☃️
A Christmas Ralph vid… Thankyou.. Have a very Happy Christmas
Nice to see you here again! Merry Xmas, Happy Holidays! 🎄❄️☃️
I love your vids so much. So educational. So real. Well explained. I just sent my brother your way because he has shown interest in arduino and the like and he loves your content too. You are a massive help and inspiration to me in the world of microcontrollers and coding, and frankly life. Thank you so much :D
Excellent video. Very useful.
The only advantage to pointers is that the caller KNOWS that the function COULD change the value. That prevents a debugging nightmare when the function changes the value of a parameter and the calling routing still relies on the ORIGINAL value.
This could be a big problem! but then I'm old and stuck in my ways.
Still not a problem if you know what it will do due to being well documented code.
Still find for embedded work C works just fine for me.
Just had to laugh as i saw "A reference allows you to manipulate an object using a pointer, but without the pointer syntax of referencing and dereferencing" so it's just a pointer with an overcoat on to hide it's internals.🙂
@TheEmbeddedHobbyist Yes, like most syntactic sugar, references are (behind the scenes) pointers but with a friendly face. Then again, all variables are really pointers (if you really want to stretch the analogy). And it cleans up after itself, unlike pointers.
@basicjackson3829 Agreed, in principal; but any function that changes the incoming parameters without good reason is a bad function. It should make a copy of the underlying values and change those, for internal use.
Welcome back and Merry Christmas
Merry Xmas, Happy Holidays! 🎄❄️☃️
Ralph! Glad to see you are well, and I wish you and your family Merry Christmas and a happy New year, and I hope to see more videos from you in the new year 🙂
I hope so too! And a Merry Xmas to you too! 🎄🎅
Good to see you again Ralph
Merry Xmas, Happy Holidays! 🎄❄️☃️
What an early Christmas Present! Glad to see you back.
And a Merry Xmas to you too! 🎄🎅
Now I no longer know if a function is going to change my variables. With pointers it is pretty obvious when a function has access to my data. It is not complicated to use pointers. It is only made complicated by people explaining it who don't really understand pointers. What's difficult about address of (&) and contents of (*) operators? That's all it boils down to.
Merry Christmas and thanks for another great video.
Well, the fact that you don't think it difficult to understand pointers means you're one of the viewers at whom this video was not aimed! Once you "get it" even the rather tortuous code I wrote the other day is clear:
applianceType thisApplianceType = *(applianceType *)parameters;
becomes clear. Why such nonsense? Because on an ESP32 Espressif decided that the parameter to a task was a (void) pointer. Crazy. And that's because all Espressif coders write in C not C++ (pretty much).
I was fixing to post the same thing. I don't want functions autonomously changing variables, I want to pass a pointer so I explicitly know that I'm passing a variable that's going to be altered. I've been writing code for over 40 years and used or experimented with as many languages, and next to COBOL and Python, C++ is one of the worst. Oh, it has some nice features, but the main problem is the way people unknowingly (or deliberately) obfuscate code or make it insanely difficult to debug because of multiple inheritances and such. Also putting code in header files -- That's a crime against nature in my book. My preferred language of choice is C, followed by Forth and assembly (for just about any processor).
BTW, Ralph, you're missing the
on your print() in line 16 :)
Missing your videos Ralph - Hope all is well.
Thank you for the infomative video. My old programming lecturer at university spent over 3 hrs explaining pointers usage in C++. At the end of the 2 part tutorial he said any programming assignment submitted that used pointers where referencing could be used instead would lose points. This was 30 years ago, paid off bigtime as most programs where (and still are) easier to debug.
Great to hear! I like that lecturer already.
Great to see you back Ralph - we've missed you. Another excellent video - very clearly explained, and I have to admit to being one of those old C programmers who used pointers many years ago long before C++ even existed. Bought back a lot of fond memories. Merry Xmas !
Glad you enjoyed it, although as a seasoned C programmer you have my full permission (like you need it!) to continue using pointers! 😆
merry xmas, ralph, for you and your family! thank you for all your teachings :)
My pleasure! And a Merry Xmas to you too! 🎄🎅
Welcome back Ralph been awhile since the last old grey cell workout.
I'm not sure whether the workout was for me or you, but I hope it clarifies things for Arduites, generally!
@@RalphBaconI used to liked doing software / firmware V&V at a medical start-up, checking the code to the standards was always a good grey cell work out. Nowadays getting a nudge to go and look something up gives them a little tweak. 🙂
Thanks Ralph. I was playing with pointers a while back where my function was simply adding 1 to the passed parameter. Today, thanks to you, I discovered that *x += 1; (works) is not the same as *x++: (does not work). That one kicked my butt for quite a while since x++; and x += 1; normally functions the same.
Ah yes, the shorthand methods are tricky to use with pointers. But good job in figuring it out. 👍
Very informative Ralph. Thank you! I was worried! I was ready to send out ASAR to look for you! (Arduino Search and Rescue!). Happy holidays to you and your family 🙂
Thanks Gord, but no AS&R required, I was just held captive in my workshop by "other work"!
Merry Xmas and a Happy New Year to you too!
That was very very helpful. When I programmed using 360 assembly and PL/S pointers were always used. I never understood why they are used in modern languages, except as you mentioned yo minimize the overhead of moving large quantities of characters or numbers. Thank you very much
Glad you found it useful!
Good video, pointers are useful when sending bytes over a radio link, and say you have floats to send byte by byte and reassemble at the receiver, but don't make mistakes. take care Graham
Well, maybe, Graham but I don't use radio links like that. I just send out packets of data (perhaps using ESP-NOW) and let the device figure out how to do it! Still gets there! Thanks for your Best Wishes and the same to you 🎄
Pointers can be useful if you are using for example an array of structs, but if you are using an array of structs and you need indexing or something like that, then you will probably know how to use it anyway's.
I do a lot of programming in structured text, which is derived from C and CPP. I like the use of pointers in structured text a little more. If you declare a pointer for an integer, it is declared as ^INT (you see it is pointing to something). In the code, if you want the value of the pointer, the syntax is ^value, if you want the pointer address it is #value. If you think about it, # is the house and ^ is what is living under the roof. In C I always have to look it up to know if I have to use & or * although the syntax is the same, only different symbols. (matter of using it more often, I know).
In short, pointers can be a minefield. Like a box of matches in the hands of a child. Ready to burn you (or the house down) if used incorrectly.
There are doubtless scenarios where pointers would be useful but by the time you need that extra functionality I would think the expertise would be there too.
I use pointers now and again - but try very hard to stick to C++ coding and not drop down to C constructs.
Thanks Ralph, just what I needed. An earlier video of yours introduced me to references and this one tied up a few loose ends and answered some questions I had!
Glad it helped!
Completely with you on this Ralph; pointers have their uses, but there's so much possibility of inadvertently creating bugs that I have avoided them for years. By reference and by value should be enough for pretty much all tasks, unless you are hitting the hardware and need every clock tick available, but in that case you would probably be programming a PIC in assembler and not an Arduino in C++ 🙂
Agreed. C++ gives us References for a reason: to avoid the "challenges" of using Pointers! We could write a bit of assembler for Arduinos too, but does anyone do that except for some real edge cases?
Welcome back.
And a Merry Xmas to you too! 🎄🎅
Happy holidays Ralph
And Happy Holidays to you too! 🎄🎅
Merry Christmas a very Happy New Year! Glad you are back.
Same to you! 🎄🎅
Hi Ralph ! You are missed ! Hope everything is well on your side. Merry Christmas and happy new year 🎉
Happy new year! Everything fine here, just too much "other work", but I may still be able to make the occasional video.
Ralph I hope is well in England. Looking forward to your next program/video.
All is well. Thanks for your concern. 👍
Actually, all is well with ME, but England is currently going to the dogs with high interest rates, junior doctors on strike, train drivers ditto, food price inflation out of control and two of our lovely Royal Family now with the big C diagnoses. Can it get any worse? (Rhetorical, of course it can, but let's hope it doesn't).
I blame Covid-19 and Brexit - they have decimated a once proud and prosperous country. 😢
@@RalphBacon Good to see you're still around - have really enjoyed finding your videos recently as I started out on my own Arduino adventure. Have just been sim racing this evening with a bespoke sim racing button box, using an Arduino Leonardo, a good deal of which I learnt about through your videos.
As for Brexit - have a look at Ireland and Scotland just this week - we are better off out of the corrupt EU.
And as for COVID - our politicians (along with many others around the world) lied, lied and lied again - all while they lined their own pockets and those of their mates.
And as for the Royal Family.... sadly there isn't an honest one among them.
Ralph! Good to see you, sir.
You too!
Thanks Ralph, this clears up a lot. Will take some time to sink in.
It is valuable to me as I have been working on an ambitious project that involves porting over Arduino IDE based code and libraries to Pico SDK code using cmake in VSC.
Yes, I know, the SDK/cmake platform is designed for C but it supports CPP just fine.
A few of the hiccups I've encountered is converting progmem references which uses pointers (or maybe it has been reference?), the other is passing arrays.
Perhaps beyond the scope of this topic, but I had found arrays, or rather large memory locations being passed the start of the memory location, and a variable in the function indexing from the start of the memory location by adding to the pointer value.
That "mistake" of printing the value of the pointer rather than the value of what the pointer points to could be a good trick for debugging, or at least exploring.
Arrays are always passed by reference (never by value) because all you get is the address of the first element of the array. You don't even get the size of the array.😲 Hence the better use of vectors in C++, at least. Good luck with your porting project.
@@RalphBacon I'm aware that arrays are always passed by reference.
But I wanted the function to use the memory area pointed to as it sees fit, but compiler wants to keep the dimensions and data type consistent from original
I figured workarounds
Great episode! It took me back to my early programming days. I wasn’t, and still am not as clever as you true professional programmers. I never will be, sadly.
Glad you enjoyed it! But don't underestimate your skills, Doug.
I’m sat here watching you videos on state machines and it hit me, I never thanked you for your kind supportive response to my comment. Thank you
hi Ralph...love U'r stuff.. so U'll REF an INT 2 make the VAR.....end.....An integer is a whole number that can be positive, negative, or zero.......
In C language, a variable is the name of a memory location that is used for storing data...A variable is a characteristic that can be measured and that can assume different values.👀
U could have a sensor feeding int, then var would find a value. is there a print yet..& it proves that U'r stack will feed in the direction U want it to go...good luck O'l boy!!🌞🌻
Well, I'm sure that was crystal clear when you wrote your comment but I'm having a minor issue sorting the wheat from the chaff. But I'll accept your word on what you reckon is the best way to go! Merry Xmas, Happy Holidays! 🎄❄️☃️
Howdy Ralph !
Dare I say "squeak"? Nice to see you here again!
Squeak !!@@RalphBacon
Nice to see you back Ralph.
Interesting topic - as always.
Have to admit I watched it over again before I finally got it
Good stuff! Yes, it may well take a few watches to get it to click.
Great explanation of pointers.
Glad you think so!
Welcome back Ralph! Thanks for another great video. Happy Holidays (politically correct U.S. way of saying Merry Christmas) to you and yours.
Merry Christmas and A Happy New Year, in a totally non-PC UK way of wishing your the absolute best! 🎅🎄😁
pointers are very usefull when interfacing with memory interfaces and stuff like that ;) nothing will replace pointers anytime soon in embedded .
There are always exceptions to the rule and this is doubtless one of them!
Happy Christmas Ralph.
Thank you, to you too! 🎄🎅
Friendly explenation. Very good.
Many thanks!
I’ve done similar things but, like this:
int a = 5;
int b = 10;
a , b = swap(a , b);
int swap(a , b) {
int temp = a;
a = b;
b = temp;
return a, b;
}
This won't work in C. You can only return a single value.
@@K5HJ Oops! I write programs in Python and several other languages (for fun). I often get mixed up with what each language can do. Thanks for correcting my mistake.
Yes, I read this last night and saw that it would never work, but by this morning @K5HJ has already written a response explaining all.
Oh, hi - welcome back ... where have you been for so long ?
Can you recommend a project for a HOME BURGLER ALARM with PIRs, maybe cameras, HORNs/Sirenes for making noise with wireless ON/OFF arming !
Maybe even by use of internet/Android phone.
I have some wired 12V PIRs /HORNs/Sirenes from an older project already placed 😉
Rather than following someone else's project why not start your own. You have already excluded microcontrollers without Wi-Fi because you want and Internet/Phone interface.
The ESP32 is the device of choice right now (if you are not using a Raspberry Pi) with decent processing power, Wi-Fi and lots of memory, especially if you get the 8Mb or even 16Mb versions.
You could create something in about 10 minutes as a proof of concept and work up from there.
Alternatively look at ESPHome (esphome.io/index.html ) which probably has a project you want just waiting for you!
@@RalphBacon THX - I'll look into your proposals and the links 🙂
Merry christmas
Thank you, to you too! 🎄🎅
Back in the old days, Macintosh programming used handles which I believe were pointers to a pointer. Lots of double * in those days 😂
Hmm. Pointers to pointers still exist but, as you might imagine, I decided not to even mention them! 😆
Very useful, thanks!
Glad you liked it. And a Merry Xmas to you too! 🎄🎅
Good point, no pun intended!
I won't reference your reply then 😲 in writing this response.
At 25:15 you show the value of the pointers as big hex numbers. But in Arduino pointers are two bytes, so how come the pointer values are so huge? Is it because its running on your 64 bit PC?
Welcome back, by the way, and looking forward to more videos in the New Year
Yes, spot on!
Because I was running this on a 64-bit PC, the pointers (an integer) were 8-bytes long (64-bits divided by 8-bit bytes = 8 bytes).
On a 32-bit (eg ESP32 or older Windows PC) machine each pointer/integer is 4 bytes long, and on the humble 8-bit Arduino they use 2-bytes, 16-bit integers, the compiler being optimised for this - the C language requires all integers to be (at least) 16 bits unless you use a definition such as int8_t which "forces" the compiler to use a single byte.
@@RalphBacon Thanks. Supplementary question: everywhere it says Arduino pointers are 2 bytes/16 bits, which can address 64K assuming unsigned int. But some devices have much more memory than that, such as the RPi Pico, so when I compile the sketch for it I see:
Sketch uses 377156 bytes (18%) of program storage space. Maximum is 2093056 bytes.
Do pointer sizes change with the device you define in the IDE?
@@stuartajc8141 Pointers are very specific to the device, and since they point to a memory address, their size is going to depend on the number of bytes used to fully address the memory space. A good way to know for sure is to use
sizeof(ptr)
Best not to assume😀
Thanks@@qcnck2776 it's something I never thought about until Ralph said they were always 16 bits, which is what the Arduino docs say. But I guess they are referring to the original MCUs with 64KB memory. Now that the Arduino IDE can compile for loads of boards with much more memory they should update the docs.
What @qcnck2776 says is spot on. The pointer must be able to contain the memory address of the last possible byte of memory. I think Intel 80286 (24-bit) got this wrong and they had to "page" memory (max 16MB) for it all to work with virtual memory (up to 1GB).
But in some ways the size of the pointer is irrelevant; what are you doing that you need to know the size of a pointer, rather than "just using it"?
Great channel.
I have at question regarding global variables.
void loop() {
int x=5;
x will get reset after every turn, a global will not get reset.
How to workaround it?
Declare the integer as you have done in the loop but with the word 'static' in front of it.
This will ensure it is removed (by the compiler) from the actual loop but is accessible to the code in there. A sort of pseudo global variable!
@@RalphBacon Thank you Ralph😄
Ralph, I hope you are ok, missing your content
Whatever you do, don't unsubscribe or you will miss any future videos!
Very Interesting
Glad you think so! And a Merry Xmas to you too! 🎄🎅
I have had to use pointers when parsing a response from an http get. And when processing a packet from a TCP/IP response. A pain in the butt. Especially when dealing with big endians. I could have created a struct for each case, but I couldn't anticipate each case, so pointers.
Sometimes you have to grasp the nettle and do what must be done 😟
It was brought to my attention from a person I really respect to follow the Barr Groups methods for C, how does their implementation convert to C++ from a newbie prospective?
As many will tell you, C++ is not C. Neither is it just "C with classes".
Bjarne Stroustrup found C unnecessarily primitive and prone to error. He wanted a static type safe language along with abstraction (OOP). The rest, as they say, is history.
Of course, every C++ compiler will also compile C without issue. In fact, some C functions are now part of the C++ language too so the lines can become blurred and confusing. So many supposed C++ developers still use C-style arrays when they should be using vectors; probably because they have a C background - and it works!
I can only assume that there are resources out there showing the true C++ path, or what NOT to do in C++ that you do in C. Using pointers willy-nilly, for example, when references were the C++ replacement.
The "best" C++ developers are those who learned C++ without learning C. The "worst" are those that know C well, and then "convert", as you put it, to C++ in a haphazard manner.
Buy Bjarne's "C++ Programming Language", 3rd edition (or later if there is one). It's not what I call an easy read, but it is the definitive C++ language reference. But others may serve your purpose better by presenting the material in an easier-to-read manner (including a better font! Doh!) with, dare I say it, simpler, Real Life examples!
As a predominantly VB6 programmer coming to c/c++/Arduino/esp32 pass byVal and pass byRef is familiar territory.
The majority of texts and videos about pointers are far to verbose and over complicated, because they never show you the pass byRef syntax which would as you rightly say be enough for most people learning the language.
I am now wondering whether Java and JavaScript have similar syntax.
Java is always Pass by Value. JavaScript too. Unless you are passing an object in which case it becomes a Pass by Reference, if you change the (underlying) members of that object.
I believe there are some anomalies in both languages but I steer clear of those.
If you are sorting a bunch of data,, not a 1000 bytes, but even 5 or 10, but you have 50 of them. Wouldn't it be more economical to sort the pointers? Vs moving all those bytes around?
That depends on what the data is, Kent.
A pointer is a two byte integer. If the data is bigger then, yes, it would be better to do so. But sorting character data is unusual; you would normally sort the index (which would probably be a one or two byte array).
Very much depends on the application and data!
Ralph where are youuuuu?
I'm here somewhere... not sure where, right now. 🤷
Ralph, is everything alright? Haven't heard anything from you in three months time.
Yes, indeed, thanks for asking. Taking a bit of a break from the YT treadmill although I have been extremely busy and productive in the last few months. I might even make a video or two about what I've been building!
@@RalphBacon, can't wait!
will you be making anymore videos or are you done?
Not done, exactly but certainly taking a break. Getting my life back! I've several topics in mind for future videos and I'm sure I'll eventually get the time to do the videos.
where is Mr. Ralph???
Taking a break from TH-cam whilst I do LOTS of "other work". There simply isn't enough time in a day to do both right now.
I can't see the point of this🙂
that's because you look more like a greyhound than a pointer. 🙂
It's the way you tell 'em, Barney!
@@RalphBacon I do my best to bring Xmas cheer🙂
Happy New Year Ralph, this year I only has time to back to hardware, hopefully things get smooth. Cheers.
Same to you! Busy, busy!