std::memcmp(3)
π std::memcmp(3) π
okay bestie, welcome to the std::memcmp page from the C++ Standard Library manual. This is the byte-comparison function, and it's lowkey iconic. No cap. π«Ά
β¨ NAME β¨
std::memcmp - std::memcmp β yes bestie, the name is literally the name. It's the byte-comparing function from the C++ Standard Library manual, and it's here to tell you which buffer comes first in lexicographical order. π«Ά
π Synopsis π
It's defined in the header <cstring> β copy that, bestie. π
Defined in header <cstring>
int memcmp( const void* lhs, const void* rhs, std::size_t count );
So what does it actually do? It reinterprets the objects pointed to by lhs and rhs as arrays of unsigned char and compares the first count bytes of those arrays. The comparison is done lexicographically β like alphabetical order, but for bytes.
The sign of the result is the sign of the difference between the values of the first pair of bytes (both interpreted as unsigned char) that differ in the objects being compared. That's the whole mood. β¨
π Parameters π
lhs,rhsβ pointers to the memory buffers to comparecountβ number of bytes to examine
π₯ Return value π₯
- A negative value if the first differing byte (reinterpreted as
unsigned char) inlhsis less than the corresponding byte inrhs. 0if allcountbytes oflhsandrhsare equal.- A positive value if the first differing byte in
lhsis greater than the corresponding byte inrhs.
π§ Notes π§
β οΈ okay so this is important: this function reads object representations, not the object values. It's typically meaningful for only trivially-copyable objects that have no padding. Don't forget that, bestie.
For example:
memcmp()between two objects of typestd::stringorstd::vectorwill not compare their contents. It's giving "nope, wrong layer."memcmp()between two objects of typestruct { char c; int n; }will compare the padding bytes whose values may differ when the values ofcandnare the same. π- And even if there were no padding bytes, the
intwould be compared without taking into account endianness.
π Example π
// Run this code
#include <cstring>
#include <iostream>
void demo(const char* lhs, const char* rhs, std::size_t sz)
{
std::cout << std::string(lhs, sz);
const int rc = std::memcmp(lhs, rhs, sz);
if (rc < 0)
std::cout << " precedes ";
else if (rc > 0)
std::cout << " follows ";
else
std::cout << " compares equal to ";
std::cout << std::string(rhs, sz) << " in lexicographical order\n";
}
int main()
{
char a1[] = {'a', 'b', 'c'};
char a2[sizeof a1] = {'a', 'b', 'd'};
demo(a1, a2, sizeof a1);
demo(a2, a1, sizeof a1);
demo(a1, a1, sizeof a1);
}
π₯οΈ Output π₯οΈ
abc precedes abd in lexicographical order
abd follows abc in lexicographical order
abc compares equal to abc in lexicographical order
π See also π
strcmpβ compares two strings (function)strncmpβ compares a certain number of characters from two strings (function)- C documentation for
memcmpβ the C version is right there too.
and that's the tea β: source http://cppreference.com, date 2024.06.10, for std::memcmp(3). mwah πβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms