c - Error while trying to update array element -


i working on embedded platform not have debugging features. hard error source.

i have defined in header file:

typedef struct cm_packet {   cm_header header; //header of packet 3 bytes   uint8_t *data; //packet data 64 bytes   cm_footer footer; //footer of  packet 3 bytes } cm_packet;  typedef struct cm_inittypedef{   uint8_t               deviceid;   cm_packet             packet;         } cm_inittypedef;  extern cm_inittypedef cmhandler; void cm_init(cm_inittypedef *handler);     cm_appendresult cm_appenddata(cm_inittypedef *handler, uint8_t identifier                               , uint8_t *data, uint8_t length); 

and somewhere in implementation have:

uint8_t bufferindex = 0;  void cm_init(cm_inittypedef *cm_initer) { //init handler   cmhandler.deviceid = cm_initer->deviceid;      cm_packet cmpacket;     cmpacket.header.deviceid = cm_initer->deviceid;   cmpacket.header.packetstart = cm_start;   cmpacket.footer.packetend = cm_end;   //initialize data array   uint8_t emptybuffer[cm_max_data_size] = {0x00};    cmpacket.data = emptybuffer;    cm_initer->packet = cmpacket; }  cm_appendresult cm_appenddata(cm_inittypedef *handler, uint8_t identifier                               , uint8_t *data, uint8_t length){         //some check see if new data not make data overflow                   uint8_t i;      /*** error happens here!!!! ***/     handler->packet.data[bufferindex++] = identifier;      //now add data     for(i = 0; < length; i++) {      handler->packet.data[bufferindex++] = data[i];     }      //reset indexer     if(bufferindex > 64) {        packetready(); //mark packet ready        bufferindex = 0       };      //return result } 

the idea update packet.data other source codes have access handler. example other sources can call append function change packet.data. see in code, have commented place causes micro-controller go in hard fault mode. not sure happening here. know @ line micro goes hard fault mode , never recovers!

this might race condition before else want know have written correct c !!! code try rule out other problems.

in function cm_init, setting cmpacket.data point local array:

uint8_t emptybuffer[cm_max_data_size] = {0x00}; cmpacket.data = emptybuffer; 

accessing memory address outside scope of function yields undefined behavior.


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -