Zeroing-Out multiple arrays with a single memset/assuming memory layout allowed?

  arduino, arduino-c++, avr, c++

Looking at the source-code of the Arduino-Ethernet-Library I found this:

class DhcpClass {
private:
...
    #ifdef __arm__
        uint8_t  _dhcpLocalIp[4] __attribute__((aligned(4)));
        uint8_t  _dhcpSubnetMask[4] __attribute__((aligned(4)));
        uint8_t  _dhcpGatewayIp[4] __attribute__((aligned(4)));
        uint8_t  _dhcpDhcpServerIp[4] __attribute__((aligned(4)));
        uint8_t  _dhcpDnsServerIp[4] __attribute__((aligned(4)));
    #else
        uint8_t  _dhcpLocalIp[4];
        uint8_t  _dhcpSubnetMask[4];
        uint8_t  _dhcpGatewayIp[4];
        uint8_t  _dhcpDhcpServerIp[4];
        uint8_t  _dhcpDnsServerIp[4];
    #endif
...

aswell as this:

void DhcpClass::reset_DHCP_lease()
{
    // zero out _dhcpSubnetMask, _dhcpGatewayIp, _dhcpLocalIp, _dhcpDhcpServerIp, _dhcpDnsServerIp
    memset(_dhcpLocalIp, 0, 20);
}

Is this really a legal way of accessing/zeroing those arrays, which are fields of a class? Can we really safely assume that they are always in this order and always at a continuous memory location? I believe no but I don’t understand why someone wouldn’t just write one memset() for each array, is the performance really that much better?

Source: Windows Questions C++

LEAVE A COMMENT