1

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

7
  • 2
    Well, allocating one byte and then using it as it had more will do that. Commented Jun 26, 2013 at 12:51
  • Replace byte *Packet1 = new byte; with byte *Packet1 = Readstr; Commented Jun 26, 2013 at 12:54
  • but the size of the array is unknown to me. Should i work with a big initialized array and the number of bytes i fill in it? i have lots of suck blocks in the same function and i was planning to use that 'Packet1' in all the blocks as it is only an address Commented Jun 26, 2013 at 12:55
  • @ctn no the Readstr doesn't have anything to do with Packet1 i should edit my function not to be misunderstood Commented Jun 26, 2013 at 13:01
  • 1
    @arianoo You know the size: it's add + 8, you even say so in the call to SerialPort.Write (but your computation of add looks weird). Commented Jun 26, 2013 at 13:04

2 Answers 2

2

I don't see how it can ever work. You allocate one byte on the heap but treat it as multiple bytes:

byte *Packet1 = new byte;
*Packet1 = 0x01;
*(Packet1+1) = 'P';  // !!!
*(Packet1+2) = '1';  // !!!
*(Packet1+3) = STX;  // !!!
*(Packet1+4) = '(';  // !!!
Sign up to request clarification or add additional context in comments.

2 Comments

Or rather byte *Packet1 = new byte[add+8];.
Your solution works completely fine and for my aim, I delete and initialize the array again in each block with the new size I want. Thanks so much for pointing out my problem
2

Here you allocate just one byte

byte *Packet1 = new byte;

and then use the pointer beyond the allocated memory

*(Packet1+1) = 'P';
*(Packet1+2) = '1';
*(Packet1+3) = STX;
*(Packet1+4) = '(';

This causes undefined behaviour, sometimes it may work. So you want something like

byte Packet1 = new byte[size]

where size is appropriate for your needs (probably add + 8, since this is the amount of bytes you write to in that function). Then delete it with delete[]. You could also use stack allocation, or std::vector<byte> since this is c++.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.