Skip to content

Commit 475f649

Browse files
committed
Fix: std::back_inserter already adds new elements, no need to reserve buffer before using std::back_inserter.
Doing so doubles the memory requirements.
1 parent 17d84b5 commit 475f649

File tree

4 files changed

+3
-3
lines changed

4 files changed

+3
-3
lines changed

Diff for: examples/UDP_Client/UDP_Client.ino

+1
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ void loop()
121121
prev_udp_packet_sent = now;
122122

123123
/* Prepare UDP packet. */
124+
uint8_t udp_tx_msg_buf[256] = {0};
124125
int const tx_packet_size = snprintf((char *)udp_tx_msg_buf, sizeof(udp_tx_msg_buf), "Single-Pair Ethernet / 10BASE-T1S: packet cnt = %d", tx_packet_cnt);
125126

126127
/* Send a UDP packet to the UDP server. */

Diff for: examples/UDP_Server/UDP_Server.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ void loop()
121121
int const rx_packet_size = udp_server.parsePacket();
122122
if (rx_packet_size)
123123
{
124-
std::vector<uint8_t> udp_tx_buf(rx_packet_size);
124+
std::vector<uint8_t> udp_tx_buf;
125125
IPAddress const destination_ip = udp_server.remoteIP();
126126
uint16_t const destination_port = udp_server.remotePort();
127127

Diff for: src/Arduino_10BASE_T1S_UDP.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ size_t Arduino_10BASE_T1S_UDP::write(uint8_t data)
129129

130130
size_t Arduino_10BASE_T1S_UDP::write(const uint8_t * buffer, size_t size)
131131
{
132-
_tx_data.reserve(_tx_data.size() + size);
133132
std::copy(buffer, buffer + size, std::back_inserter(_tx_data));
134133
return size;
135134
}

Diff for: src/Arduino_10BASE_T1S_UDP.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ class Arduino_10BASE_T1S_UDP : public UDP
9595
: _remote_ip(remote_ip)
9696
, _remote_port(remote_port)
9797
, _rx_data_len(data_len)
98+
, _rx_data(p_data, p_data + data_len)
9899
{
99-
std::copy(p_data, p_data + data_len, std::back_inserter(_rx_data));
100100
}
101101

102102
typedef std::shared_ptr<UdpRxPacket> SharedPtr;

0 commit comments

Comments
 (0)