AnsweredAssumed Answered

Defining a temporary array on stack

Question asked by Mark Butcher on May 4, 2006
Latest reply on May 8, 2006 by CrasyCat
Hi All
I just had a problem with CW4.5 for HC(S)12. It started me thinking - I wonder whether any one knows the answer:
1. I had a project which was working when compiled with other compilers but would work strangely when compiled with CW. It would crash when a certain routine was called and did something strange when another routine was called.

2. I realised that both issues were either present, or could be corrected, by writing static arrays differently, as follows:
Method 1:
unsigned char tx_message[ HEADER_LENGTH ] = { INTERNAL_ROUTE, INTERNAL_ROUTE , tcp_tx->cWakeTask, INTERRUPT_EVENT, TX_FREE };
This doesn't work with CW when it is used within a conditional statement in a routine (but I think is OK when used on entry to a routine) and causes strange behaviour.
Method 2:
               unsigned char tx_message[ HEADER_LENGTH ];
               tx_continue_message[ 0 ] = INTERNAL_ROUTE;
               tx_continue_message[ 1 ] = INTERNAL_ROUTE;
               tx_continue_message[ 2 ] = tcp_tx->cWakeTask;
               tx_continue_message[ 3 ] = INTERRUPT_EVENT;
               tx_continue_message[ 4 ] = TX_FREE;
This construct works fine with CW. The code runs perfectly.
3. I was not getting any warnings (from none of the compilers I use) about this type of construct, therefore my question - is it a non-standard construct which I shouldn't really be using ? Or is it a CW bug?
4. I actually prefer the first method since it allows setting the array without defining its length and I think that it is easier to make a mistake when coding using the second method. I was however surprised to find out that when I checked the code efficience (using GNU compiler as reference - which doesn't mean it is the same for all) the second method was actually being coded with only about 50% of the code size - so I have used the same construct in all code now.....Just a note..
Any one have comments?
Mark Butcher