Тёмный

#BB12 

Ralph S Bacon
Подписаться 54 тыс.
Просмотров 6 тыс.
50% 1

It's true! At the beginner to moderate hobbyist coder, pointers are just trouble 😮
► PCBWay $5 for 10 pieces www.pcbway.com
00:00 Welcome Back
00:01 Intro and quick C++ vs C discussion
04:43 Why the need for references or pointers?
10:08 References walkthrough
16:50 Pointers walkthrough
31:03 Conclusion
GitHub entry for this video: github.com/RalphBacon/BB12-Po...
which contains the example files used in the demo.
So you asked "What about pointers, Ralph?". And I kept telling you, "Not needed". I was finally brow-beaten, er, sorry, "persuaded" to show you the difference between C++ user-friendly References and C-style, hard-to-decipher (and use) Pointers.
It is, of course, good to know about pointers, as other people's code might be full of them. Especially libraries, ported from the C language. But it doesn't mean you should be tempted to use them when we have References available.
Unless you know differently? Your views in the comments, please!
► List of all my videos
(Special thanks to Michael Kurt Vogel for compiling this)
bit.ly/RU-vidVideoList-RalphB...
► If you like this video please give it a thumbs up, share it and if you're not already subscribed please consider doing so and joining me on my Arduinite (and other μControllers) journey
My channel, GitHub and other stuff are here:
------------------------------------------------------------------
• / ralphbacon
• github.com/RalphBacon
• buymeacoffee.com/ralphbacon
------------------------------------------------------------------
My ABOUT page with email address: / ralphbacon

Наука

Опубликовано:

 

21 дек 2023

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 158   
@medrolet
@medrolet 7 месяцев назад
Welcome back, Ralph. You've been missed.👍
@RalphBacon
@RalphBacon 7 месяцев назад
Thank you! And a Merry Xmas to you too! 🎄🎅
@SpinStar1956
@SpinStar1956 7 месяцев назад
Bacon is always missed when not around!!! 🤣😂🤣
@hansdegroot652
@hansdegroot652 3 месяца назад
Yes but he is missing again for quite some time now
@philipreed2758
@philipreed2758 3 месяца назад
@@hansdegroot652 i was just thinking the same as you today. i hope all is well. x
@rpr42
@rpr42 2 месяца назад
Miss him as well. Hope he's doing well.
@borayurt66
@borayurt66 7 месяцев назад
Good to see you again Ralph, it has been some time.
@RalphBacon
@RalphBacon 7 месяцев назад
It has been a while! Merry Xmas, Happy Holidays! 🎄❄️☃️
@gregwmanning
@gregwmanning 7 месяцев назад
Merry Christmas Ralph
@RalphBacon
@RalphBacon 7 месяцев назад
Merry Xmas, Happy Holidays! 🎄❄️☃️
@ElieWar
@ElieWar 7 месяцев назад
Good to see you back
@RalphBacon
@RalphBacon 7 месяцев назад
Merry Xmas, Happy Holidays! 🎄❄️☃️
@hariseldon2577
@hariseldon2577 7 месяцев назад
Was wondering where you where. Nice to have you back again.
@RalphBacon
@RalphBacon 7 месяцев назад
Hey, thanks! And a Merry Xmas to you too! 🎄🎅
@martin87865
@martin87865 7 месяцев назад
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.
@RalphBacon
@RalphBacon 7 месяцев назад
Noted!
@tonybell1597
@tonybell1597 7 месяцев назад
A Christmas Ralph vid… Thankyou.. Have a very Happy Christmas
@RalphBacon
@RalphBacon 7 месяцев назад
Nice to see you here again! Merry Xmas, Happy Holidays! 🎄❄️☃️
@SpinStar1956
@SpinStar1956 7 месяцев назад
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...
@RalphBacon
@RalphBacon 7 месяцев назад
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!
@user-xl6qu2jg4h
@user-xl6qu2jg4h 7 месяцев назад
Good to see you again Ralph
@RalphBacon
@RalphBacon 7 месяцев назад
Merry Xmas, Happy Holidays! 🎄❄️☃️
@willofirony
@willofirony 7 месяцев назад
Welcome back and Merry Christmas
@RalphBacon
@RalphBacon 7 месяцев назад
Merry Xmas, Happy Holidays! 🎄❄️☃️
@OsoPolarClone
@OsoPolarClone 7 месяцев назад
What an early Christmas Present! Glad to see you back.
@RalphBacon
@RalphBacon 7 месяцев назад
And a Merry Xmas to you too! 🎄🎅
@stevex3976
@stevex3976 7 месяцев назад
Merry Christmas a very Happy New Year! Glad you are back.
@RalphBacon
@RalphBacon 7 месяцев назад
Same to you! 🎄🎅
@fredflintstone1
@fredflintstone1 7 месяцев назад
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 🙂
@RalphBacon
@RalphBacon 7 месяцев назад
I hope so too! And a Merry Xmas to you too! 🎄🎅
@MikesAllotment
@MikesAllotment 7 месяцев назад
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 !
@RalphBacon
@RalphBacon 7 месяцев назад
Glad you enjoyed it, although as a seasoned C programmer you have my full permission (like you need it!) to continue using pointers! 😆
@Friendroid
@Friendroid 7 месяцев назад
merry xmas, ralph, for you and your family! thank you for all your teachings :)
@RalphBacon
@RalphBacon 7 месяцев назад
My pleasure! And a Merry Xmas to you too! 🎄🎅
@basiljackson3829
@basiljackson3829 7 месяцев назад
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.
@TheEmbeddedHobbyist
@TheEmbeddedHobbyist 7 месяцев назад
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.🙂
@RalphBacon
@RalphBacon 7 месяцев назад
@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.
@RalphBacon
@RalphBacon 7 месяцев назад
@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.
@TBoy58_405
@TBoy58_405 2 месяца назад
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 👍🏾🤓
@RalphBacon
@RalphBacon Месяц назад
I hope so too!
@kaoshavoc
@kaoshavoc 27 дней назад
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
@ianneill9188
@ianneill9188 7 месяцев назад
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!
@RalphBacon
@RalphBacon 7 месяцев назад
Glad it helped!
@gpTeacher
@gpTeacher 7 месяцев назад
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 🙂
@RalphBacon
@RalphBacon 7 месяцев назад
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!
@colepdx187
@colepdx187 7 месяцев назад
Ralph! Good to see you, sir.
@RalphBacon
@RalphBacon 7 месяцев назад
You too!
@DavidUnderhill
@DavidUnderhill 7 месяцев назад
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.
@RalphBacon
@RalphBacon 7 месяцев назад
Great to hear! I like that lecturer already.
@markgreco1962
@markgreco1962 7 месяцев назад
Happy holidays Ralph
@RalphBacon
@RalphBacon 7 месяцев назад
And Happy Holidays to you too! 🎄🎅
@TheEmbeddedHobbyist
@TheEmbeddedHobbyist 7 месяцев назад
Welcome back Ralph been awhile since the last old grey cell workout.
@RalphBacon
@RalphBacon 7 месяцев назад
I'm not sure whether the workout was for me or you, but I hope it clarifies things for Arduites, generally!
@TheEmbeddedHobbyist
@TheEmbeddedHobbyist 7 месяцев назад
@@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. 🙂
@_AmHam_
@_AmHam_ 7 месяцев назад
Hi Ralph ! You are missed ! Hope everything is well on your side. Merry Christmas and happy new year 🎉
@RalphBacon
@RalphBacon 7 месяцев назад
Happy new year! Everything fine here, just too much "other work", but I may still be able to make the occasional video.
@andreweastland9634
@andreweastland9634 7 месяцев назад
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.
@RalphBacon
@RalphBacon 7 месяцев назад
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).
@JCWren
@JCWren 7 месяцев назад
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 :)
@henrybecker2842
@henrybecker2842 7 месяцев назад
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
@RalphBacon
@RalphBacon 7 месяцев назад
Glad you found it useful!
@digihz_data
@digihz_data 7 месяцев назад
Happy Christmas Ralph.
@RalphBacon
@RalphBacon 7 месяцев назад
Thank you, to you too! 🎄🎅
@grahamwise5719
@grahamwise5719 7 месяцев назад
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
@RalphBacon
@RalphBacon 7 месяцев назад
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 🎄
@williammcdonald9086
@williammcdonald9086 7 месяцев назад
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.
@RalphBacon
@RalphBacon 7 месяцев назад
Ah yes, the shorthand methods are tricky to use with pointers. But good job in figuring it out. 👍
@computergururick
@computergururick 3 месяца назад
Great explanation of pointers.
@RalphBacon
@RalphBacon 3 месяца назад
Glad you think so!
@danielcastillo357
@danielcastillo357 4 месяца назад
Ralph I hope is well in England. Looking forward to your next program/video.
@RalphBacon
@RalphBacon 4 месяца назад
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. 😢
@chriskeeble
@chriskeeble 4 месяца назад
@@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.
@MrJozza65
@MrJozza65 7 месяцев назад
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++ 🙂
@RalphBacon
@RalphBacon 7 месяцев назад
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?
@daveharris3384
@daveharris3384 7 месяцев назад
Welcome back.
@RalphBacon
@RalphBacon 7 месяцев назад
And a Merry Xmas to you too! 🎄🎅
@rinokentie8653
@rinokentie8653 7 месяцев назад
Very useful, thanks!
@RalphBacon
@RalphBacon 7 месяцев назад
Glad you liked it. And a Merry Xmas to you too! 🎄🎅
@sveinarsandvin6418
@sveinarsandvin6418 7 месяцев назад
Friendly explenation. Very good.
@RalphBacon
@RalphBacon 7 месяцев назад
Many thanks!
@CXensation
@CXensation 7 месяцев назад
Nice to see you back Ralph. Interesting topic - as always. Have to admit I watched it over again before I finally got it
@RalphBacon
@RalphBacon 7 месяцев назад
Good stuff! Yes, it may well take a few watches to get it to click.
@douglasosborn3378
@douglasosborn3378 5 месяцев назад
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.
@RalphBacon
@RalphBacon 5 месяцев назад
Glad you enjoyed it! But don't underestimate your skills, Doug.
@douglasosborn3378
@douglasosborn3378 3 месяца назад
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
@philipwalker2800
@philipwalker2800 Месяц назад
Missing your videos Ralph - Hope all is well.
@danman32
@danman32 7 месяцев назад
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.
@RalphBacon
@RalphBacon 7 месяцев назад
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.
@danman32
@danman32 7 месяцев назад
@@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
@Ed19601
@Ed19601 7 месяцев назад
Merry christmas
@RalphBacon
@RalphBacon 7 месяцев назад
Thank you, to you too! 🎄🎅
@lilbuzz999
@lilbuzz999 7 месяцев назад
Welcome back Ralph! Thanks for another great video. Happy Holidays (politically correct U.S. way of saying Merry Christmas) to you and yours.
@RalphBacon
@RalphBacon 7 месяцев назад
Merry Christmas and A Happy New Year, in a totally non-PC UK way of wishing your the absolute best! 🎅🎄😁
@andymouse
@andymouse 7 месяцев назад
Howdy Ralph !
@RalphBacon
@RalphBacon 7 месяцев назад
Dare I say "squeak"? Nice to see you here again!
@andymouse
@andymouse 7 месяцев назад
Squeak !!@@RalphBacon
@steverileyretired
@steverileyretired 7 месяцев назад
Very Interesting
@RalphBacon
@RalphBacon 7 месяцев назад
Glad you think so! And a Merry Xmas to you too! 🎄🎅
@__--JY-Moe--__
@__--JY-Moe--__ 7 месяцев назад
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!!🌞🌻
@RalphBacon
@RalphBacon 7 месяцев назад
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! 🎄❄️☃️
@jadus2198
@jadus2198 6 месяцев назад
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).
@RalphBacon
@RalphBacon 6 месяцев назад
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.
@keldsor
@keldsor 7 месяцев назад
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 😉
@RalphBacon
@RalphBacon 7 месяцев назад
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!
@keldsor
@keldsor 7 месяцев назад
@@RalphBacon THX - I'll look into your proposals and the links 🙂
@Tony770jr
@Tony770jr 7 месяцев назад
Good point, no pun intended!
@RalphBacon
@RalphBacon 7 месяцев назад
I won't reference your reply then 😲 in writing this response.
@stuartajc8141
@stuartajc8141 7 месяцев назад
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
@RalphBacon
@RalphBacon 7 месяцев назад
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.
@stuartajc8141
@stuartajc8141 7 месяцев назад
@@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?
@qcnck2776
@qcnck2776 7 месяцев назад
@@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😀
@stuartajc8141
@stuartajc8141 7 месяцев назад
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.
@RalphBacon
@RalphBacon 7 месяцев назад
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"?
@EmbeddedEnigma
@EmbeddedEnigma 7 месяцев назад
pointers are very usefull when interfacing with memory interfaces and stuff like that ;) nothing will replace pointers anytime soon in embedded .
@RalphBacon
@RalphBacon 7 месяцев назад
There are always exceptions to the rule and this is doubtless one of them!
@Disillusioned_one
@Disillusioned_one 14 дней назад
Ralph, the return 0 in the main function is not needed as the main function is a special case and will return whatever, try it.
@TradieTrev
@TradieTrev 7 месяцев назад
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?
@RalphBacon
@RalphBacon 7 месяцев назад
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!
@clausjrgenahlerhansen1236
@clausjrgenahlerhansen1236 Месяц назад
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?
@RalphBacon
@RalphBacon Месяц назад
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!
@clausjrgenahlerhansen1236
@clausjrgenahlerhansen1236 Месяц назад
@@RalphBacon Thank you Ralph😄
@TheRealNVT
@TheRealNVT 2 месяца назад
Ralph, I hope you are ok, missing your content
@RalphBacon
@RalphBacon Месяц назад
Whatever you do, don't unsubscribe or you will miss any future videos!
@jasonlee3247
@jasonlee3247 7 месяцев назад
Back in the old days, Macintosh programming used handles which I believe were pointers to a pointer. Lots of double * in those days 😂
@RalphBacon
@RalphBacon 7 месяцев назад
Hmm. Pointers to pointers still exist but, as you might imagine, I decided not to even mention them! 😆
@kentswanson2807
@kentswanson2807 7 месяцев назад
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?
@RalphBacon
@RalphBacon 7 месяцев назад
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!
@KaiseruSoze
@KaiseruSoze 7 месяцев назад
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.
@RalphBacon
@RalphBacon 7 месяцев назад
Sometimes you have to grasp the nettle and do what must be done 😟
@lindsaymcphee2885
@lindsaymcphee2885 7 месяцев назад
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.
@RalphBacon
@RalphBacon 7 месяцев назад
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.
@SprocketN
@SprocketN 7 месяцев назад
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; }
@K5HJ
@K5HJ 7 месяцев назад
This won't work in C. You can only return a single value.
@SprocketN
@SprocketN 7 месяцев назад
@@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.
@RalphBacon
@RalphBacon 7 месяцев назад
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.
@hansdegroot652
@hansdegroot652 3 месяца назад
Ralph where are youuuuu?
@RalphBacon
@RalphBacon 3 месяца назад
I'm here somewhere... not sure where, right now. 🤷
@TheUnofficialMaker
@TheUnofficialMaker 2 месяца назад
will you be making anymore videos or are you done?
@RalphBacon
@RalphBacon 2 месяца назад
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.
@lilbuzz999
@lilbuzz999 4 месяца назад
Ralph, is everything alright? Haven't heard anything from you in three months time.
@RalphBacon
@RalphBacon 4 месяца назад
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!
@lilbuzz999
@lilbuzz999 4 месяца назад
@@RalphBacon, can't wait!
@zyghom
@zyghom 3 месяца назад
where is Mr. Ralph???
@RalphBacon
@RalphBacon 3 месяца назад
Taking a break from RU-vid whilst I do LOTS of "other work". There simply isn't enough time in a day to do both right now.
@fredflintstone1
@fredflintstone1 7 месяцев назад
I can't see the point of this🙂
@TheEmbeddedHobbyist
@TheEmbeddedHobbyist 7 месяцев назад
that's because you look more like a greyhound than a pointer. 🙂
@RalphBacon
@RalphBacon 7 месяцев назад
It's the way you tell 'em, Barney!
@fredflintstone1
@fredflintstone1 7 месяцев назад
@@RalphBacon I do my best to bring Xmas cheer🙂
@nexuzinnovation-com
@nexuzinnovation-com 5 месяцев назад
Happy New Year Ralph, this year I only has time to back to hardware, hopefully things get smooth. Cheers.
@RalphBacon
@RalphBacon 5 месяцев назад
Same to you! Busy, busy!
Далее
are "smart pointers" actually smart?
9:44
Просмотров 73 тыс.
PORTAL SPAMMER🤬🤬🤬| Doge Gaming
00:19
Просмотров 1,3 млн
#265 Me & My 3D Printer - Just a quick chat!
23:42
Просмотров 7 тыс.
Use Arc Instead of Vec
15:21
Просмотров 141 тыс.
5 Levels of Go Error Handling
19:13
Просмотров 6 тыс.
Faster than Rust and C++: the PERFECT hash table
33:52
Просмотров 532 тыс.