I've written a few 8bit embedded systems and the codebase I inherited and have expanded is basically 80% global variables (extern volatile), and than non-global control flags and logic variables as needed.
The end result, is that you end up with a lot void() functions to modify the global variables.
The systems run fine and the software quite readable and easy to work on, but I always have that design nag in the back of my head that I should be refactoring everything and pointer'ize the next design.
I don't have any religiosity about the memory usage aspect, the static memory is there to use as much as the heap is. It's more a question of as systems get larger, I suspect maybe the globals start to be more of a hinderance than you think? I've never gotten there in program size.
Do many of you experienced embedded software programmers use pointers extensively in your 8bits systems?
I can think of three possible use cases for using pointers in C51: linked lists (which we don't use) in this system, structs in place of independent variables and then functions with pointers to modify the strucs, maybe a double pointer if you absolutely need to modify a pointer but I can't fathom a situation in building a consumer product with an 8051 (maybe building an OS...).
I get that the functions can get re-used if you pointer'ize your code, but in one sense the functions are pretty trivial and one off in the embedded systems I've built. Very application specific stuff.
A couple of you guys who are still around C51 forum, what is your most compelling (or not compelling) use case for pointers in the code you have released?
Is this your point to a degree, if you really get down to it, if you have absolute globals why even use the overhead of functions?
Yes, given that these processors are relatively slow, I see little point in giving them extra work to do just so I can appease the code style police. This is not an argument I would give on newer, faster or more capable processors. It's simply a case of realism and knowing the limits of the processor being used.
What sort of particulars are at play when you say that pointers / dynamic memory allocation is inefficient with C51? That really cuts to the heart of the matter.
The 8x81 is an 8 bit processor, and a limited one at that. It always amazed me that Keil (and others) managed to make a compiler that complied so well with the C standards.
That said, for each line of C code, the compiler has to produce the corresponding assembly code. Even for simple operations, the code produced can be lengthy and require significant cpu cycles at run time.
The original 8051 only has one 16 bit pointer, so it meant that using pointers requires a lot of data shuffling.
Using dynamic memory allocation requires quite extensive use of pointers, so that inefficiency can be multiplied.
Not so nice.
@Neil -- that's some really helpful insight. Yeah sadly, we build some really cheap products so 8051s are what's in the budget.
For the global variables, I think your described approach is kind of similar to what I have.
We might have some really useful global ArrayX[] that three functions modify in some form.
I guess the only actual reason we use functions on the globals is to try to modularize the code a bit, and try to conceivably reuse the code in some fashion.
Those modules are topically grouped in different .c files to try to build some kind of reusability. (For example, I have some CRC routines I split out to crc.c), it has references to global variables but if you copied those variables over to a new project you'd have a working CRC routine pretty quickly).
I agree it's actually a bit timely to even re-use those scraps of code. Now if you had pointerized the functions, they are more re-usable.
But in the case of my CRC function, how many times am I going to pass a 5 byte array, and do the calculation, and dump the CRC checkcode in the 6th byte.... The software I end up having just seems so narrow.
From a refactoring standpoint, what is not good is that we didn't at least use a "g_" or some denominator to at least call out what the global is associated with.
----
Because I totally see more updated ARM Cortex M code, like middleware (the Nordic SDK comes to mind) is really heavy with strucs and pointers.
I stopped C51 (aka 8x51) development many years ago now and, where it is practical, I would always suggest you move from that outdated architecture to something current.
Pointers in C51 are extremely inefficient. As a consequence, dynamic memory allocation in C51 is extremely inefficient. I avoided both wherever practical. I know at least one person on this forum that would disapprove when I say that I frequently used the LARGE memory model. For our systems it worked incredibly well, since we had to store relatively large amounts of data at low-ish speed. But I still avoided dynamic memory allocation and pointers wherever possible.
I do wonder with what you describe though. You say you have 80% global variables but use void() functions to modify them. Why?
If they are really global variables, you don't need the overhead of functions to access them. You probably mean local variables to a module.
At the risk of receiving plenty of "that's not the way to do it", what we did was have many true global variables that could be manipulated without access functions, but named them in such a way that the owning module of the data items was clearly apparent. This reduced the need for the overhead of access functions.
This was only something that we considered 'acceptable' because of the limited platform capabilities. All developers of the projects were aware of the agreed rules. And we all stuck to them, so the code was relatively easy to maintain.
As I suggested at the start, if possible try to move your developments across to a newer architecture. We went the ARM route (with Keil tools) and have not regretted it. You can then use pointers and dynamic memory allocation without worrying about their impact. It's been great.
Doing this will also allow you to have coding standards that are far more typical.
View all questions in Keil forum