Are Pointers Bad for Embedded Systems?

  Рет қаралды 21,320

Jacob Sorber

Jacob Sorber

Күн бұрын

Пікірлер: 96
@Kotesu
@Kotesu 2 жыл бұрын
15-year embedded dev here. I absolutely love the points you're making and want to share this with many of my old coworkers and bosses. In my opinion, the difference between embedded system development and other types of programming really comes down to operational context, for instance: - operational failures can result in hardware destruction, loss of property of loss of life depending on the application - you typically don't trust your toolchains (I've diagnosed silicon errors, bugs in the compiler itself, faulty drivers in an in-circuit debugger and even grounding issues in the building) - you often don't have fancy debug tools so visibility into your system can be really restricted - the system can be installed in the middle of nowhere on the other side of the planet and your customer is on a sat phone asking you questions and you can barely speak their language. Because of this, determinism is absolutely crucial and a good embedded dev must be incredibly self-disciplined in his design and implementation: just because we can do a clever algorithm or elegant architectural pattern doesn't mean that it is wise in our context.
@johnpawlicki1184
@johnpawlicki1184 2 жыл бұрын
I have retired from embedded systems programming. Did it for over 40 years. I have seen that pointer problems come from not understanding pointer syntax. Programmers simply play with combinations of * and & until something seems to work. Of course this results in pushing issues down the stream. Understanding aspects of pointer syntax like, in declaration * means making a pointer of a variable while in execution * means making a variable from a pointer can save a lot of problems. Pointers are really powerful and I used them, even in automotive systems. With great power comes great responsibiity. ;-)
@Kotesu
@Kotesu 2 жыл бұрын
Massive respect, John. 40+years would have seen you navigate so many epochs and sea-changes in technology, industry and application scope. What was your favorite chip you've ever worked with? I have a soft spot for MSP430's and 8052's but ended up doing 7+ years doing ungodly things with PIC16/18's (sales wanted ever more features but EE's refused to re-certify) and another 7 with ARM (Cortex and TDMI 7's).
@christeralmond1081
@christeralmond1081 2 жыл бұрын
HR is clueless nowadays when it comes to selecting the suitable applicants for the job. Me and a friend of mine applied to the same embedded C++ -programmer position. The friend has high school-level education, and he has programmed mostly with python and javascript. He's pretty much at "hello world" - level in C++. He's had a unrelated job recently though. His result: "Welcome to the interview!" I have masters in applied math, and I got 15 years of C/C++ experience, highlights being cross-platform game engines and creating my own interpreted programming language. I've been unemployed for years, though actively programming my personal projects. My result: "We are not interested. You don't have any experience." The scary part is the company we applied to does contract work for NASA.
@eone199
@eone199 2 жыл бұрын
@@christeralmond1081 how do you get money for a living???
@christeralmond1081
@christeralmond1081 2 жыл бұрын
@@eone199 I live in Scandinavia.
@herrbonk3635
@herrbonk3635 2 жыл бұрын
Perhaps, among the young, but pointers ≠ C-pointers (or their syntax). I mean, pointers are a fundamental aspect of how programs actually work, whether implicit or explicit. You cannon read your RAM or ROM without a pointer. I guess people today mean manual addressing or address arithmetics when they say "pointers" (due to C being so widespread), but almost every variable or array/record-element has a pointer at runtime. The exception being variables and constants held in registers.
@BogdanSerban
@BogdanSerban 2 жыл бұрын
You forgot the most important aspect when working directly with hardware: accessing registers
@zweitekonto9654
@zweitekonto9654 2 жыл бұрын
That happens in assembly? High level languages have no way of accessing registers. Correct me if i am wrong.
@lukadj996
@lukadj996 2 жыл бұрын
@@zweitekonto9654 if you mean cpu registrers then yes of course we don't try to access them directly. But in embedded systems there is a lot, so called, memory mapped hardware for eg timers, DMA, gpios etc which is accessible only by using pointers to that specific memory address. So I don't get this video eaven if you use arduino and digitalwrite function under the hood it is derefrencing pointer to the specific memory location and writing 0 of 1 in the specific bit and that register is somehow wired to output latches and push pull transistors and internal pull up resistors and a lot of other things.
@lukadj996
@lukadj996 2 жыл бұрын
Hahahha dal me zajebavas tek sad vidim da dva srbina objasnjavaju zasto su pointeri neizbezni u embeded sistemima 🤣
@filips7158
@filips7158 2 жыл бұрын
@@lukadj996 a "pointer", as referenced in this video, is a notion inherently stemming from the C language, which is itself a wrapper around some ASM memory I/O instructions which are proper to the architecture (MIPS for PIC, ARM, RISC-V, x86, some proprietary and forgotten ones you will never hear about). Registers on an MCU are simpy some well defined address regions in data memory which have a special function understood by the CPU logic, and user data may never be written to them (compilers job and your job). They have variable access, but usually RW. On some MCUs, they are accessed through specially defined MACROs which under the hood resolve to number (address location), or through constant pointers on others, which again resolve to a number. There is therefore no direct relation between a register and a pointer, which is purely a C construct.
@lukadj996
@lukadj996 2 жыл бұрын
@@filips7158 I didn't said that registers are directly accesable of course at the end of the day when we compile C code it gets translated into asm and then to machine code which cpu with his ALU can understand and 'run' it. The point is that I can use void* my_ptr = (void*)0x1234abcd which is some address coresponding to some gpio register defined by the chip manufacturer in the referenc manual. So yes, I am using high level language (C) to manipulate so called control registers of an IO port. I already said that this is not true register inside CPU (like X Y Z...) but they are called controll registers for example AVR has DDRx which is data direction register used to configure port pin as input, output etc...
@herrbonk3635
@herrbonk3635 2 жыл бұрын
How do you write ANY embedded code without pointers? How do you even access memory without pointers? They are totally fundamental to all programming, even though they may be used only implicitly in some languages.
@mowinckel10
@mowinckel10 2 жыл бұрын
A good point from Adam Savage about danger. It is not the most dangerous things that will get you, because those are often obviously dangerous. It is the dangerous thing that either does not appear dangerous you have gotten used to, and thus forgot is dangerous. Pointers are the same. They are dangerous. But they are at their worst if you forget that, or if they are used in a way that hides that they are dangerous
@JacobSorber
@JacobSorber 2 жыл бұрын
Indeed. Thanks!
@electromatic2014
@electromatic2014 2 жыл бұрын
Personally I use pointers A LOT, so I didn't get the why of the video at first. But theeen when talking about dynamic memory allocation with malloc, realloc etc. I was like, yeah those things are from the devil. So nothing wrong with using pointers if you are really sure they point to the intended memory sections, we are C programmers so don't be afraid of memory guys 😳. I also think there are not many use cases for dynamic allocation in embedded systems as most specific aplications you will cover will need a certain "quantity" of resources wich you may just define at compile time, so yeah I try to avoid that as much as I can (except for variable length arrays inside functions when passing a size as parameter 😋). Great video as always, the thumbnail got me like 🧐 at first so it got me.
@InspektorDreyfus
@InspektorDreyfus 2 жыл бұрын
Just yesterday found a bug, where a function had a string as local variable on the stack, returned a pointer to that string (life scope of string ended when function returned). And later the pointer was used in strcpy (which only stops at \0). Of course the dead string on stack was overwritten by some other function meanwhile. And strcpy later did not stop copying because of missing \0. Total mess all over the place. And all that string mess just for logging messages.
@martinprochazka3714
@martinprochazka3714 2 жыл бұрын
Not sure about other compilers but gcc has -Wreturn-local-addr which would have probably caught this.
@edgarbonet1
@edgarbonet1 2 жыл бұрын
@@martinprochazka3714 You don't even need to specify -Wreturn-local-addr, as this option is enabled by default.
@InspektorDreyfus
@InspektorDreyfus 2 жыл бұрын
Thanks for the hint. Yes, gcc checks it by default and there is just the option to disable the warning with -Wno-return-local-addr. In my case we use a Greenhills compiler, which doesn't produce a warning. Or the warning is ignored or suppressed.
@Hauketal
@Hauketal 2 жыл бұрын
Function pointers are great for state machines. No more big switch statements.
@luz_reyes_676
@luz_reyes_676 2 жыл бұрын
do you access them with meaningful indexes?
@EdwinFairchild
@EdwinFairchild 2 жыл бұрын
@@luz_reyes_676 yeah the index to something like a function pointer array will imply the state.
@Hauketal
@Hauketal 2 жыл бұрын
@@luz_reyes_676 No need to use an index at all. The pointers are not stored in an array. Just have a single state variable of type function pointer. Outer loop: state = STATE_INITIAL; do { char c = read_input (); state (c); while (state != STATE_FINAL;
@luz_reyes_676
@luz_reyes_676 2 жыл бұрын
@@Hauketal interesting. and you can change the function pointer before returning? never thought of that or its implications before
@Hauketal
@Hauketal 2 жыл бұрын
@@luz_reyes_676 Right, just a global variable, or file static of the state machine source. But could also be a return value.
@R3DMSR
@R3DMSR 2 жыл бұрын
I once had to interact with an I2C-based RTC with a PIC microcontroler and needed to retrieve the hole date+time which was stored in 6 bytes in the internal memory of the thing in a single function, so I needed to access all at once and retrieve these bytes somehow. In order to have only one bus communication to retrieve the data, I figured I could just have byte pointers as the argument so I could "return" multiple values. That proved to be useful since otherwise I would have to signal and start 6 different reads for data that was consecutive and could be done at once. So, pointers still have their uses in embedded. Now for dynamic memory, I don't really know.
@TomStorey96
@TomStorey96 2 жыл бұрын
One note on stack frames.. I've noticed that GCC, when you have basically any level of optimisation enabled, will often omit stack frames if they aren't needed. But you have to look at the disassembly of your code to determine if that is the case, or perhaps monitor stack usage at run time.
@foomoo1088
@foomoo1088 2 жыл бұрын
Instead of avoiding pointers, I like to avoid nonsensical hypothetical judgements of coding mechanisms out of context.
@theondono
@theondono 2 жыл бұрын
A lot of people do mix up pointers and dynamic memory, I think this is because of how pointers are introduced in a lot of programming intro courses, and the fact that a lot of programmers in the embedded space are EEs, so that’s pretty much all programming courses they’ve done. Pointers can be very dangerous because the C semantics are a bit too simplistic, but if used with good reasons, they are a great tool. That said, the average embedded programmer can be very dangerous with pointers. I once worked in a codebase with: #define HANDLER (void *) I think all viewers of these channel will understand why this is not a good idea 😂
@timtreichel3161
@timtreichel3161 2 жыл бұрын
I mean if you want to use arrays, you need to use pointers and I don't think that there are many (embedded) applications, which work without some form of array. I would gladly learn more about ways to use "dynamic memory" in embedded systems. I guess you could just use some byte array and create your own allocators/free methods on top of it, which maybe don't have heap fragmentation. I think in many situations the following pattern occurs anyway: a = alloc(size_a); b = alloc(size_b); c = alloc(size_c); free(c); free(b); free(a); which allows to use very simple stack allocators. I would be very interested to learn more about object pools.
@me_hanics
@me_hanics 2 жыл бұрын
Hi Jacob! I need your help. I've been developing in C for 3 years, I studied embedded systems (under EE) at uni and now I've been accepted to work on developing self-driving car components. I'll have to write code in C++. I have used C++ before, we learned object oriented programming through it, but it wasn't much, and I've never used it for embedded systems. Can you make a video on what are the differences when developing in C vs C++ on embedded systems? Thanks, Mike
@InspektorDreyfus
@InspektorDreyfus 2 жыл бұрын
The good news is: programming for embedded systems means, that you avoid a lot of things that the language theoretically offers. Saying it negatively: you are not allowed to use many language quirks that you learned.
@SerBallister
@SerBallister 2 жыл бұрын
@@InspektorDreyfus Some C++ features are a step up from C. I really like references which in a lot of cases replace the need for pointers.
@erbenton07
@erbenton07 Жыл бұрын
I was programming a PLC that picked up a 2 ton roll of paper from a conveyor and was supposed to move it over its head and set it down on another conveyor. I made a mistake, and instead it simply tossed it over its back and let go. Fortunately we never let anyone near a machine when we were developing code for it. but wow! that was a real shock
@droohyen
@droohyen Жыл бұрын
As always: great video! I got one request for next video: what would you say about KPIs for embedded software development? Do you know any so you can say something about?
@Muhammed.Abd.
@Muhammed.Abd. 6 ай бұрын
I have seen arrays of function pointers used in Embedded, Can't say it way easy to understand but man it was elegant, like a masterpiece.
@sharpfang
@sharpfang 2 жыл бұрын
Gawd, if one thinks pointers are only used with malloc et al, they probably didn't program much embedded. Every write to a mem-mapped device/peripheral (and like 90% of them are mem-mapped) goes through a pointer. If you set up an interrupt vector, that's a function pointer. Instead of malloc, you set aside as much memory as needed and access it through a pointer. Moving data to/from a buffer - pointer, sometimes disguised as an array. You don't often use strings, but when you do, the string is a pointer to the location of the first character. The premise of that question was sooo misguided.
@Elixz89
@Elixz89 Жыл бұрын
Embedded programmer here, most of my function arguments and returns are pointers. Why should I allocate more memory to hold the same content. I do not use any malloc, only static declaration
@loknathshankar5423
@loknathshankar5423 2 жыл бұрын
Depends on the hardware, for ex short or char is smaller in size than a pointer, so if someone tries to use pass by pointer or reference for a those, that's just wasting memory.
@surya.6283
@surya.6283 2 жыл бұрын
In embedded without pointers there is no code
@EdwinFairchild
@EdwinFairchild 2 жыл бұрын
I agree, you literally need pointers to all the register addresses. I mean just look at the headers it just structs mapped to addresses and then pointers to those structs.
@surya.6283
@surya.6283 2 жыл бұрын
@@EdwinFairchild ya for the code modularity ... the hals etc are all pointers to functions, arguments of pointer type etc.
@wlcrutch
@wlcrutch Жыл бұрын
Jacob…where can I get some of those t-shirts?
@johnheaney3349
@johnheaney3349 2 жыл бұрын
I almost never use a heap, but how can you even avoid using function pointer? Most of the function pointers I use const, so they are in flash, part of the code really and not significantly different than a function call.
@alexrossouw7702
@alexrossouw7702 2 жыл бұрын
That's weird because embedded system use C mainly, and so many C functions are based on pointers under the hood
@leonardocaetano6307
@leonardocaetano6307 2 жыл бұрын
Not often embedded C programs don't use the stdlib
@varunsahni7825
@varunsahni7825 2 жыл бұрын
Hi jacob, Could you please tell some tools or software which can be use for detecting memory leakage or other type of error if plateform is linux. Please reply🥺
@wvpatuser
@wvpatuser Жыл бұрын
valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes will do it for you in a lot of cases
@Majkieboy
@Majkieboy 2 жыл бұрын
I am particularly interested in object pools!
@belesiu
@belesiu 2 жыл бұрын
How do you copy memory and strings without pointers? I’ve needed to do this in all of my embedded systems that had UIs or that processed data streams from external inputs.
@SuperLateral2
@SuperLateral2 Жыл бұрын
Hello, Jacob Could you please make a video on pool of objects in embedded systems?
@asadbeknizomov1450
@asadbeknizomov1450 Жыл бұрын
Hi Jacob. I've been watching your videos for 1 month. So my question is why did you call function pointers "dangerous". I'd be happy if you could explain this. cheers
@kitanowitsch
@kitanowitsch 2 жыл бұрын
I work for more than 10 years in Embedded and around 5 years in Automotive C, where pointers are allowed, but double pointers (pointer to pointer) are not.
@rogo7330
@rogo7330 2 жыл бұрын
Are SIMD instructions exist in embeded systems and how they work in comparison to x86_64 ones? I readed some stuff about AVX and SSE, cache and other stuff. But I never heard about them on AVR systems, like Arduino
@edgarbonet1
@edgarbonet1 2 жыл бұрын
Some ARM chips have an SIMD extension called “NEON”. AVR is a very simple, very basic architecture, with no SIMD.
@vonnikon
@vonnikon 2 жыл бұрын
In general, no. However, ARM Cortex-M4 is starting to gain popularity in embedded Systems, and it does have SIMD.
@salsamancer
@salsamancer 2 жыл бұрын
Weird, I just saw that exact same question on stack overflow this week
@elclippo4182
@elclippo4182 2 жыл бұрын
Pointers aren’t per se bad for embedded. Just know how to use them and avoid potentially dangerous things like pointer casts leading to alignment issues or pointer arithmetic.
@radman999
@radman999 2 жыл бұрын
How would you ever do anything in C without using pointers? I don't get it.
@georgecop9538
@georgecop9538 2 жыл бұрын
4:08 Languages like Python have implemented a maximum recursion limit, but C doesn't.
@JacobSorber
@JacobSorber 2 жыл бұрын
On most more traditional platforms (laptops, desktops - things with MMUs) there's a similar limit (for C/C++ programs) determined by the amount of memory mapped for the stack. Once you exceed it, you seg fault. Of course, it's not available on a lot of MMU-less microcontrollers, and will vary with machine/configuration. Do you know how this is implemented in python?
@georgecop9538
@georgecop9538 2 жыл бұрын
@@JacobSorber when I was.looking in the os (or sys) module, I've found os.maximumRecursionLimit
@georgecop9538
@georgecop9538 2 жыл бұрын
Pointers are used in embedded, but under the hood(if you don't believe me, check libopencm3).
@nexovec
@nexovec 2 жыл бұрын
I thought recursive stack frames can mostly be optimized out these days, is that false?
@traznai2
@traznai2 2 жыл бұрын
All recursive functions can be turned into iterative functions. Look up how tail call optimization works.
@Hauketal
@Hauketal 2 жыл бұрын
@@traznai2 Not really. Often yes, but there are counterexamples. See Ackermann's function. Of cause, you can always simulate recursion with an array of saved values and iteration. But that is just doing manually what the compiler would do, with more chances for errors.
@casperes0912
@casperes0912 2 жыл бұрын
@@traznai2 Tail call optimisation only works if the recursive call is "the tail". If you do all sorts of stuff with the result of your recursive call you can't apply tail call optimisation. In some cases the compiler may be able to turn it into something loop based or similar, but definitely not always - And you might not even want it to in some cases.
@theondono
@theondono 2 жыл бұрын
That is generally true as long as you make your code tail-recursive, compilers won’t (in general) unroll recursion unless it has the right structure. The other thing to consider is that a lot of embedded C code will have references to volatile and memory barriers, so compilers are more constrained in making this kind of optimizations.
@arghyadhar5805
@arghyadhar5805 2 жыл бұрын
I think the issue with pointer is cash miss sometimes
@donythomas7
@donythomas7 2 жыл бұрын
Can you please share your thoughts on Rust programming language? I heard that it has the potential to replace C++ in the future.
@Squirrelies1
@Squirrelies1 Жыл бұрын
I heard that Carbon may be the C++ replacer as it allows directly importing C/C++ header files as-is rather than having to interop, allowing legacy code bases to port-over over time.
@mtalhakhalid1679
@mtalhakhalid1679 Жыл бұрын
It happen to me uae a lot of lubrary in arduino at once i was working on project and herebwas no way to eliminate cz every library had a function and then itnuse 70%if memory spacethen i use uint8_t insted of int it uses 69% of memory hahahaand at soemnpoint arduino goes to infinite loop hang
@henrykkaufman1488
@henrykkaufman1488 2 жыл бұрын
Didn't watch yet and never did embedded so naturally I'm gonna comment right away: pointers are ok or even maybe recommended, because dont copy if you dont have to, but doing malloc/free like a lunatic isn't. EDIT: wasn't completely wrong. wonderful experiment.
@rustycherkas8229
@rustycherkas8229 2 жыл бұрын
Pointers are bad? Gotta paraphrase Jessica Rabbit: "I'm not bad. I'm just deployed that way..." Or, Winnie-the-Pooh after failing to hop like Kanga: "Some can; some can't. That's just how it is..."
@greg4367
@greg4367 2 жыл бұрын
Programmers should avoid pointers for the same reason carpenters should avoid tools. Because they are not yet mature enough to use them, However, for adult programmers who want to get some work done...
@markuscwatson
@markuscwatson 2 жыл бұрын
Yes to object pools
@abdallahmah1867
@abdallahmah1867 2 жыл бұрын
for you
@burningglory2373
@burningglory2373 2 жыл бұрын
I use pointers all the time in embedded programming. Just dont use malloc at all due to small amounts of memory size.
@ivanscottw
@ivanscottw 2 жыл бұрын
You never answered the actual question ! ** Are Pointers Bad for Embedded Systems ? ** You ranted about stack/heap collision.. About dynamic vs static allocation.. About pointers pointing to the wrong place.. but never : ** ARE POINTERS BAD FOR EMBEDDED SYSTEMS ? **
@JacobSorber
@JacobSorber 2 жыл бұрын
...no they are not. Sorry if that wasn't clear. There are problematic things people do with pointers in embedded systems, but pointers are a common part of C/C++ programming (including embedded systems). Just use them wisely.
@djouze00
@djouze00 2 жыл бұрын
No, they are not. Dumb question, really.
@Jkauppa
@Jkauppa 2 жыл бұрын
bs is bad for everyone
@Jkauppa
@Jkauppa 2 жыл бұрын
and you do it for money, patreon
@Jkauppa
@Jkauppa 2 жыл бұрын
double death bad
Why are my ADC readings messed up? (Arduino example)
5:44
Jacob Sorber
Рет қаралды 7 М.
Pulling Back the Curtain on the Heap
21:38
Jacob Sorber
Рет қаралды 37 М.
Une nouvelle voiture pour Noël 🥹
00:28
Nicocapone
Рет қаралды 9 МЛН
How to treat Acne💉
00:31
ISSEI / いっせい
Рет қаралды 108 МЛН
To Brawl AND BEYOND!
00:51
Brawl Stars
Рет қаралды 17 МЛН
How Strong Is Tape?
00:24
Stokes Twins
Рет қаралды 96 МЛН
Is memcpy dangerous?
14:08
Jacob Sorber
Рет қаралды 24 М.
Debugging Embedded Systems With GDB?
13:51
Jacob Sorber
Рет қаралды 49 М.
A const int is not a constant.
9:16
Jacob Sorber
Рет қаралды 70 М.
How do I access a single bit?
11:07
Jacob Sorber
Рет қаралды 23 М.
The What, How, and Why of Void Pointers in C and C++?
13:12
Jacob Sorber
Рет қаралды 55 М.
What Actually is Embedded C/C++? Is it different from C/C++?
11:05
Jacob Sorber
Рет қаралды 157 М.
Another way to check pointers at runtime in C
12:16
Jacob Sorber
Рет қаралды 13 М.
Hardy's Integral
13:47
Michael Penn
Рет қаралды 2,8 М.
Une nouvelle voiture pour Noël 🥹
00:28
Nicocapone
Рет қаралды 9 МЛН