This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Array intialization

I'm seeing some strange behavior when initializing arrays. I'm using the following code to intialize a 256 element array to zeros:

xdata char myarray[256] = {0}

The code seems to hang when I run this using my debugging hardware. If I run this with the simulator it seems to work fine. If I reduce the number of elements to 50 it works OK, but increase it to 60 and I get it to hang again. Has anyone seen this before? Is it a hardware issue vs compiler/assembler issue?

Parents
  • Thanks for the prompt responses. I went back and reviewed my code and noticed the watchdog timer was enabled. The watchdog is enabled by default on a reset and was timing out before the initialization could complete. This explains why seemed to hang and is why it works fine with a 50 element array, but not with 60 elements. I took Per's advice and removed the explicit zero initializations and now allow the compiler to do it for me.

    Along the same lines...I noticed a substantial decrease in the code size when I move nonzero global variable initialization into the main body of code instead of when I define the global variables. I saw a reduction of close to 130 bytes for a single variable if I go about things this way. Is there really that much "black magic" going on in the startup code? This is very surprising as it only takes 3 bytes to assign the same nonzero value in the main body.

Reply
  • Thanks for the prompt responses. I went back and reviewed my code and noticed the watchdog timer was enabled. The watchdog is enabled by default on a reset and was timing out before the initialization could complete. This explains why seemed to hang and is why it works fine with a 50 element array, but not with 60 elements. I took Per's advice and removed the explicit zero initializations and now allow the compiler to do it for me.

    Along the same lines...I noticed a substantial decrease in the code size when I move nonzero global variable initialization into the main body of code instead of when I define the global variables. I saw a reduction of close to 130 bytes for a single variable if I go about things this way. Is there really that much "black magic" going on in the startup code? This is very surprising as it only takes 3 bytes to assign the same nonzero value in the main body.

Children