1

I am when I am building a struct like this in C :

typedef struct packet {
uint32_t source;            // processus source
uint64_t numPacket;     // Number of the message. The first message has a value of 1 for this attribute.
uint8_t ack;                    // is ack or not (0 if not ack)
uint32_t size;
char message[MAX_BUFLEN];               // message (data)
} Packet;

The address of the different fields is not correct. For example, if ptr is pointing on a "Packet" :

ptr               is pointing on the address 0x7fff59464720
&(ptr->source)    is pointing at the address 0x7fff59464720
&(ptr->numPacket) is pointing at the address 0x7fff59464728
&(ptr->ack)       is pointing at the address 0x7fff59464730
&(ptr->size)      is pointing at the address 0x7fff59464734
&(ptr->message)   is pointing at the address 0x7fff59464738

As you can see, this is not the correct address since the size of the source field is a uint32_t (=4) and not 8, and the size of ack is a uin8_t (=1) and not 4.

I do not know why this behave like this. If you have an idea, thanks for your answer.

J.Doe
  • 53
  • 5
  • "As you can see, this is not the correct address" Yes it is. – tkausl Nov 14 '16 at 13:33
  • It is not the exact address since there is a padding as the precedent topic is showing. Thank you for the answer. – J.Doe Nov 14 '16 at 14:05
  • It might not be the address you expected but it is the correct address to the field in the struct. – tkausl Nov 14 '16 at 14:07

0 Answers0