hi alexander,
first you have to know that an array variable in C is nothing else than a pointer to the
data of the array. if you address an element in the array, the compiler just shifts this
pointer according to the field size. It’s just a smarter way of dereferencing a pointer.
the compiler also knows the field type and can handle it accordingly then.
compiler warning: structure ‘_color’ passed as function argument changed to pointer
I think this happens:
because you have a union, there are two possibilities for the compiler to handle
the passing of your var to the function: as three-bytes-data or as an array variable,
which is a pointer.
the optimizer finds that you just use the array type in your function and decides
to pass it as a pointer, which makes sense because less data will be put on the
function stack.
In my eyes this is just an optimazation hint, and nothing is wrong
with your code.
as long as you just read the data, there is no problem with that.
If you would write, you would just change the original data, because your
array var is a pointer to the data. It’s a bit weired in conjunctin with the
union thing, e.g. what would happen if you do both in the function, writing
to the single fields and the array fields? maybe then the compiler would copy
the data, and the array-dereference would point to the copied data then?
there are other optimizations like this, which will cause the compiler to throw
a warning:
8.1.5 Loop Reversing
This optimization is done to reduce the overhead of checking loop boundaries for every iteration. Some simple
loops can be reversed and implemented using a “decrement and jump if not zero†instruction. SDCC checks for
the following criterion to determine if a loop is reversible (note: more sophisticated compilers use data-dependency
analysis to make this determination, SDCC uses a more simple minded analysis).
• The ’for’ loop is of the form
for(<symbol> = <expression>; <sym> [< | <=] <expression>; [<sym>++ | <sym> += 1])
<for body>
• The <for body> does not contain “continue†or ’breakâ€.
• All goto’s are contained within the loop.
• No function calls within the loop.
• The loop control variable <sym> is not assigned any value within the loop
• The loop control variable does NOT participate in any arithmetic operation within the loop.
• There are NO switch statements in the loop.
this is from the sdcc manual (I attached it to the post).
I also found this about passing arrays to functions in c++:
http://www.itee.uq.edu.au/~comp2303/Leslie_C_ref/C/SYNTAX/functions.html
I believe that passing arrays as data to functions is not part the concept of C, because function
parameters will be hold on the the stack, which is limited. so for bigger data amounts you would
pass a pointer anyway.
I think I should study this passing issue a bit deeper too.
correction: by default sdcc does not push the params/vars in a function to the stack.
therefore functions are by default non-reentrant. to write a function that you can call recursively, you need to declare
the function with the keyword __reentrant (sdcc manual 3.7 Parameters & local variables). also check
chapter 8.2 ANSI Compliance.
[sdccman.pdf](< base_url >/applications/core/interface/file/attachment.php?id=4510)