Copied from the tech forums. Remember, NCAT (Non-admins can’t answer there)
I just wanted to mention this post from the ROBOTC forums, because it goes into detail about how exactly to pass stuff by reference. It also talks about structs, and using unions as a cool trick to mass-change/initialize a struct (though there are better ways, like using memset (described in RobotC Help/ROBOTC Functions/Miscellaneous))
Specifically, I was wondering if their would be some cool hack that we could do to simulate dynamic memory allocation and pointers, by using a union’d struct+array combo, and defining a really big array at compile time with a bunch of int members, so that we could access any of the members by a “pointer” (really just an index to the array inside the struct, which is linked to an int)?
Yes, I have often done something like this when pointers were needed. The method that you describe is pretty much what it was. An array of structs. Many times though, you just simply need one global struct to make it work.
Many small embedded systems don’t have dynamic memory allocation and if they do it can cause problems in a multi tasked/multi threaded program. Its fairly easy to implement the system where data structures are assigned from static memory on an as needed basis, however, for this to work successfully “real” pointers allowing things like linked lists are really needed.
When I first started using RobotC I though these types of limitations would be a problem, however, so far it hasn’t had much impact on development as anything developed on the cortex with its limited resources and relatively simple IO is in itself going to be fairly simple. Once you adjust to the mindset of only having 12K of RAM available and that all variables (including local variables) are really globals, then the coding style adapts accordingly.
That was probably a misleading statement on my part. What I mean is that usually variables declared within a subroutine (excluding statics) exist on the stack (an area of memory used for temporary storage) whereas in RobotC all variables are stored at constant memory locations and are accessed in the same way as global variables as far as the compiler is concerned, if you look at the assembly listing or the variable debug window you will see this. This is why we can not have recursive subroutines in RobotC. I agree the scope of the variable is limited to within the subroutine but once declared they use storage that cannot be used for other things.
Suppose I have a library that operates on some struct(s). I shouldn’t normally pass by value, but pass by reference. Or maybe I have a buffer I need to operate on by some function: I pass the pointer to the buffer, not a copy of the buffer.
Pointers are an important part of programming, regardless of whether you are programming for a simple microcontroller or doing web development although they are reincarnated in different forms.
Function pointers can be especially useful for implementing something like true-speed recalculation, among may other things.