Problems while compiling dhcp.c file

I’m adding support in DHCP, using dhcp.c of yours.

During compilation the following error is issued:
“Warning[Pa039]: use of address of unaligned structure member”.

The compiler directs me to RIP_MSG struct, where there are 2 members (uint32_t xid and uint16_t flags) which cause this warning.
Can you tell why is that, and I can I get rid of this warning?


I do not know the device and the compiler you’re using, but if you plan to use 16 or 32 bits devices should be careful to use of the structures because the compiler performs the padding of fields.
That the error occurs then the strructure does not will correspond to what is expected and almost certainly the DHCP server will not be able to recognize the contents of RIP_MSG also as message length.
The source work without modification with 8-bit devices and I used the source in question with PIC18 without problems but for the 16-bit versions (PIC24) and 32 bit (PIC32) I could not pass the structure in transmission but I had send a single byte UDP.

Hello Coccoliso,
thanks for quick reply.

Well, I use STM32 MCU (32 bits) and IAR compiler.
Can you explain what do you mean by [quote]but I had send a single byte UDP[/quote]?

What should I do in order to use this code with a 32 bits MCU?


Use the structure to prepare the data to be transmitted but then “manually” send every single field of the same breaking it down into single byte … for the fields consisting of several bytes (like XID or Flags) must send in the order the most significant byte. The W5500 allows direct access to his memory sequentially and at the end of the writing of every single bytes of RIP_MSG set rSn_TX_WR0 with the number of bytes to be transferred.
I have had this problem with the PIC24 (16 Bit) and the developer of the compiler has confirmed to me that the compilers operate optimizations on the structures in speed and memory footprint but unusable in serialization.

I found the answer to my question about the padding that gave me the developer of Firewing PIC24 / PIC32:

“Data types larger than a byte have to be aligned on word/dword locations or you get an address fault, so structures must typically be padded with extra bytes if you have mixed data types in them.”

“Anything larger than a byte has to be aligned (either 2 or 4 byte, depending on the core). Compiler re-orders (and packs, where necessary) structure members in order to word align data. This means you cannot guarantee order of structure members. In practical terms, this has no impact on normal program operation. You can even assign structures of the same type to each other.”

Then you need to use the longest type of the variable used in the structure, aligning the fields shorter … result is that the whole is much different from the original message that is wanted to transfer.

To use the original source decompose fields different from the bytes into sub-fields long one byte only.

XID is 4 bytes then XID0, XID1, XID2, XID3
Secs is 2 bytes then Secs0, Secs1

ensuring that the structure is composed of only fields of type Byte

Thanks again.

What about __packed or #pragma pack(1) for the RIP_MSG ?
Will that solve the issue as well?