Library contents

C++ std::memmove

Move the remaining characters left within the same buffer using an operation that supports overlapping ranges.

A simple example

std::memmove

  1. Start with "xAB" in a writable buffer
  2. Do this Move AB and the terminator one place left
  3. Result "AB"
The regions overlap. memmove preserves the bytes as they shift left.

Syntax

C++ syntax
std::memmove(code, code + 3, count)
Run these examples in C++

Save one example at a time as lesson.cpp. Use C++17 and a Unix-style shell.

c++ -std=c++17 -Wall -Wextra lesson.cpp -o lesson
./lesson

Remove a printed prefix from a package code

A printed scanner code may begin with a standard prefix. Remove it only under the intended matching rule so the same letters inside the actual identifier are preserved.

From the programchar code[] = "PK-482"
Result482
lesson.cpp
#include <cstring>
#include <cstdio>

int main() {
    char code[] = "PK-482";
    std::memmove(code, code + 3, std::strlen(code + 3) + 1);
    std::puts(code);
    return 0;
}
Output
482

Copying four bytes includes the three digits and the terminating null byte.

Advertisements

Shift a label one byte

A badge printer makes room for an extra prefix during a buffer edit. Copy the first three bytes one position to the right in the same buffer. Which four bytes remain at the start?

GivenABCD · copy 0–2 to 1–3
Worked resultAABC

Shift a strip of sensor data

A display moves three occupied positions over by one. Copy the first three bytes one position to the right in the same buffer. Which four bytes remain at the start?

Given1234 · copy 0–2 to 1–3
Worked result1123

What to watch for

Confirm the prefix length and array bounds first. memcpy is unsuitable for overlapping ranges.

Documentation for std::memmove