c - Unhandled exception at 0x0f6cf9c4 in server.exe: 0xC0000005: Access violation reading location 0x00000001 -


here code have written in visual studio 2008. data in receive array & using data, preparing response it. error "unhandled exception @ 0x0f6cf9c4 in server.exe: 0xc0000005: access violation reading location 0x00000001." every time @ "strcat" function when code runs.

    byte receive[50];     unsigned char result[50];      int index = 0;     char *type;     char *s =  "ff ff ff 1d 00 01 01 0b 00 01";     char *s1 = "03 00 98 ac 06 36 6f 92";     int i;   if (receive[i] == 0x18)     {          if ((receive[i+1] == 0x20) || (receive[i+1] == 0x21) || (receive[i+1] == 0x22) || (receive[i+1] == 0x23) || (receive[i+1] == 0x24) || (receive[i+1] == 0x25))         {              if (receive[i+2] == 0x00)             {                 result[index] = receive[i-4];`enter code here`                 result[index+1] = receive[i-3];                 index = index+2;                 type = "report";             }         }     } }  index = 0;  if (type == "report") {         strcat(s, result[index]);         strcat(s, result[index+1]);         strcat(s, s1);         strcat(s, array1[j]);         strcat(s, array1[j+1]);         strcat(s, array1[j+2]);         strcat(s, array1[j+3]);         strcat(s, array1[j+4]); 

a candidate cause of crashes though, try modify string literals.

when strcat(s, ...) modify string literal s points to. string literal read-only fixed-size array of characters terminated special character '\0'. when use such string literal destination in strcat call first of modify read-only array, , write out of bounds of array. both of these things leads undefined behavior.

you need create own array, big enough fit data want write it. , don't forget space string terminator.

furthermore use e.g. result[index] string. elements in result single characters, not pointers strings. need use strncat concatenate single characters. and need pass pointer character. depending on array1 might have similar problems.


as possible solution might want use sprintf instead of series of invalid strcat calls:

sprintf(dest, "%s%c%c%s%c%c%c%c%c",         s,         result[index],         result[index+1],         s1,         array1[j],         array1[j+1],         array1[j+2],         array1[j+3],         array1[j+4]); 

Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - IE9 error '$'is not defined -