cvanyy
cvanyy

Reputation: 23

Value of array in structure still changing

I have a problem. I written the some values to my array and i want to send handle to inicialize frame to connecting with my device, but when i pick up the structure my data from struct overvrite the another value than i want.

There are my foo:

int handlePackets (struct _PacketData * outputPacket, uint8_t command){
    outputPacket->addr = SERIAL;
    outputPacket->length = 0;
    outputPacket->cmd = command;

    switch (outputPacket->cmd)
    {
        case CMD_SET_OUTPUT:
            outputPacket->length = 0x06;

            outputPacket->data[0] = 0x80;
            outputPacket->data[1] = 0x3F;
            outputPacket->data[2] = 0x10;
            outputPacket->data[3] = 0x40;
            outputPacket->data[4] = 0x30;
            return COMM_FRAME_OK;
    }
    return COMM_FRAME_OK;
}
int sendPacket (struct _PacketData * outputPacket, uint8_t * buffer){
int cntr = 0, x;

buffer[cntr++] = STX;
buffer[cntr++] = outputPacket->addr >> 24;
buffer[cntr++] = outputPacket->addr >> 16;
buffer[cntr++] = outputPacket->addr >> 8;
buffer[cntr++] = outputPacket->addr & 0xff;

buffer[cntr++] = outputPacket->length >> 8;             
buffer[cntr++] = outputPacket->length & 0xff;

buffer[cntr++] = outputPacket->cmd;

for (x = 0; x < outputPacket->length -1 ; x++)
{
    buffer[cntr++] = outputPacket->data[x];     
}
crc = calcCrcBuffer (buffer + 1, cntr - 1);

buffer[cntr++] = (unsigned char) (crc >> 8);        //crc high byte
buffer[cntr++] = (unsigned char) (crc & 0xff);      //crc low byte

buffer[cntr++] = ETX;
return cntr;
}

structure

struct _PacketData{
uint32_t addr;
uint16_t length;
uint8_t cmd;
uint8_t data[256];};

main.c

int main(void) {
struct _PacketData  * outputOutPack;

uint8_t tab[256];
int i;

    uint8_t *buffer = tab;

  handlePackets(&outputOutPack,CMD_SET_OUTPUT );

  result = sendPacket(&outputOutPack, buffer);}

The output of my frame is not like i need. Data overvrite the values from buffer[cntr] but i dont know why.

Output:

[0] = 0x82
[1] = 00
[2] = 0x18
[3] = 0xb0
[4] = 0xa5
[5] = 00
[6] = 0x6
[7] = 0x78
[8] = 0x82
[9] = 00
[10] = 0x18
[11] = 0xb0
[12] = 0xa5
[13] = 0x64
[14] = 0xfe
[15] = 0x83

Position 7 is output->cmd and next is output->data, [8-12], but its not my array :)

Any suggestion?

Thanks

Upvotes: 1

Views: 61

Answers (1)

cmidi
cmidi

Reputation: 2010

When you compile the code you would see the following warnings.

warning: passing argument 1 of 'handlePackets' from incompatible pointer type
note: expected 'struct _PacketData *' but argument is of type 'struct _PacketData **'

The reason for the problem is that your functions(handlePackets and sendPacket) takes a pointer to the struct _PacketData not pointer to a pointer. You need to change struct _PacketData * outputOutPack; to struct _PacketData outputOutPack;

What you are essentially doing when you provide the functions with a pointer to a pointer, is dereferencing the memory you allocated to the pointer type on the stack this leads to undefined behavior and potentially corrupting your stack.

Upvotes: 1

Related Questions