I’ve read many Q&A's which seemed similar to this problem but haven’t found any answers yet:
I have to make some assignments to a dynamic byte array in the fillbyte function like this:
int Error;
result = fillbyte (&Error);
if I comment the line shown below, everything works fine. but if that line gets executed, the second time that this function is called, access violation exception will be raised however the first time the code runs properly and everything goes alright. I can’t seem to find the problem with this line of code or another way to fill the array with password bytes.
Bool fillbyte(int *Error)
{
byte BCC;
byte *Packet1 = new byte;
*Packet1 = 0x01;
*(Packet1+1) = 'P';
*(Packet1+2) = '1';
*(Packet1+3) = STX;
*(Packet1+4) = '(';
int add = sizeof(readingprops.password)*2;
for(int i=0;i<add;i++)
{
*(Packet1+(5+i)) = readingprops.password[i]; //this line raises the problem
}
*(Packet1+add+5) = ')';
*(Packet1+add+6) = ETX;
BCC = calc.CalcBCC(Packet1,add+7);
*(Packet1+add+7) = BCC;
SerialPort.Write(Packet1,add+8);
delete Packet1;
return true;
}
Any help would be appreciated
byteand then using it as it had more will do that.byte *Packet1 = new byte;withbyte *Packet1 = Readstr;Readstrdoesn't have anything to do withPacket1i should edit my function not to be misunderstoodadd + 8, you even say so in the call toSerialPort.Write(but your computation ofaddlooks weird).