Questions tagged [memcmp]

memcmp is a function available in the string.h library.

Official Definition

The memcmp() function compares the first n bytes (each interpreted as unsigned char) of the memory areas s1 and s2.

Syntax of usage

#include <string.h>
int memcmp(const void *s1, const void *s2, size_t n);

Source

Linux Man Pages

106 questions
6
votes
4 answers

How to do reverse memcmp?

How can I do reverse memory comparison? As in, I give the ends of two sequences and I want the pointer to be decremented towards the beginning, not incremented towards the end.
Paul Manta
  • 30,618
  • 31
  • 128
  • 208
6
votes
1 answer

Unexpected segfault in memcmp

The following simple program segfaults for me: #include int main() { void* voidp = NULL; char zeroes[sizeof(void*)]; memset(zeroes, 0, sizeof(void*)); int res = memcmp(&voidp, zeroes, sizeof(void*)); return 0; } The…
HighCommander4
  • 50,428
  • 24
  • 122
  • 194
6
votes
2 answers

Why does a bitmap compare not equal to itself?

This is a bit puzzling here. The following code is part of a little testing application to verify that code changes didn't introduce a regression. To make it fast we used memcmp which appears to be the fastest way of comparing two images of equal…
Joey
  • 344,408
  • 85
  • 689
  • 683
5
votes
5 answers

C array comparison

Is the defacto method for comparing arrays (in C) to use memcmp from string.h? I want to compare arrays of ints and doubles in my unit tests I am unsure whether to use something like: double a[] = {1.0, 2.0, 3.0}; double b[] = {1.0, 2.0,…
bph
  • 10,728
  • 15
  • 60
  • 135
5
votes
3 answers

What prevents the compiler from optimizing a hand written memcmp()?

Given: #include bool test_data(void *data) { return memcmp(data, "abcd", 4) == 0; } The compiler can optimize it into: test_data: cmpl $1684234849, (%rdi) sete %al ret which is nice. But if I use my own memcmp()…
Ammar Faizi
  • 1,393
  • 2
  • 11
  • 26
5
votes
1 answer

C++17 std::byte produces less optimized code with the standard algorithms in GCC

I really like std::byte as a distinct type that implements the concept of byte as specified in the C++ language definition. What I don't like is the fact that modern C++ compilers will produce less optimized code using the standard algorithms. Here…
user21009021
5
votes
8 answers

Implementing memcmp

The following is the Microsoft CRT implementation of memcmp: int memcmp(const void* buf1, const void* buf2, size_t count) { if(!count) return(0); while(--count && *(char*)buf1 == *(char*)buf2 ) { buf1 =…
5
votes
2 answers

Compare structs in C using memcmp() and pointer arithmetic

I know that memcmp() cannot be used to compare structs that have not been memset() to 0 because of uninitialized padding. However, in my program I have a struct with a few different types at the start, then several dozen of the same type until the…
John
  • 2,015
  • 5
  • 23
  • 37
4
votes
3 answers

Test of 8 subsequent bytes isn't translated into a single compare instruction

Motivated by this question, I compared three different functions for checking if 8 bytes pointed to by the argument are zeros (note that in the original question, characters are compared with '0', not 0): bool f1(const char *ptr) { for (int i…
Daniel Langr
  • 22,196
  • 3
  • 50
  • 93
4
votes
2 answers

What does memcmp do if you pass two identical pointers as inputs?

I am comparing two byte arrays with memcmp (or rather a library function does that). The arrays can become relatively large, and they can actually be the same array in many cases. Would it make sense to write something like this, or will memcmp…
SteakOverflow
  • 1,953
  • 13
  • 26
4
votes
5 answers

How to call memcmp() on two parts of byte[] (with offset)?

I want to compare parts of byte[] efficiently - so I understand memcmp() should be used. I know I can using PInvoke to call memcmp() - Comparing two byte arrays in .NET But, I want to compare only parts of the byte[] - using offset, and there is no…
brickner
  • 6,595
  • 3
  • 41
  • 54
4
votes
8 answers

How to efficiently compare a block of memory to a single byte?

I'm trying to see if a struct came back as all 0xFF for the size of the struct. memcmp seems like the obvious starting point, BUT I'd have to allocate a second memory block, populate it with 0xFF's. It just seems like a waste. Does a standard…
tarabyte
  • 17,837
  • 15
  • 76
  • 117
4
votes
1 answer

How do I write a reliable content-independent implementation of memcmp()?

A naive implementation of memcmp() goes something like this (from this answer): int memcmp_test(const char *cs_in, const char *ct_in, size_t n) { size_t i; const unsigned char * cs = (const unsigned char*) cs_in; const unsigned char * ct…
sharptooth
  • 167,383
  • 100
  • 513
  • 979
3
votes
1 answer

memcmp / strcmp vs uint64_t comparisson

I have lots of strings each with size 8 or less. I need to do lots of comparisons there using memcmp() / strcmp(). I wonder if comparisons will work faster if I convert all them to std::uint64_t. In this case, at least on theory comparison will be…
Nick
  • 9,962
  • 4
  • 42
  • 80
3
votes
3 answers

memcmp linker error Visual Studio 2015

I have a visual studio 2012 c++ project. I recently uninstalled it and installed visual studio 2015 and upgraded the project. When i am building the project, getting error as shown below: Error LNK2019 unresolved external symbol _memcmp referenced…
Roop
  • 51
  • 1
  • 5