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
40
votes
3 answers

Why is memcmp so much faster than a for loop check?

Why is memcmp(a, b, size) so much faster than: for(i = 0; i < nelements; i++) { if a[i] != b[i] return 0; } return 1; Is memcmp a CPU instruction or something? It must be pretty deep because I got a massive speedup using memcmp over the loop.
jsj
  • 9,019
  • 17
  • 58
  • 103
24
votes
6 answers

memcmp vs multiple equality comparisons

Precondition: Consider such a class or struct T, that for two objects a and b of type T memcmp(&a, &b, sizeof(T)) == 0 yields the same result as a.member1 == b.member1 && a.member2 == b.member2 && ... (memberN is a non-static member variable of…
Emil Laine
  • 41,598
  • 9
  • 101
  • 157
15
votes
6 answers

How to compare generic structs in C++?

I want to compare structs in a generic way and I've done something like this (I cannot share the actual source, so ask for more details if necessary): template bool structCmp(Data data1, Data data2) { void* dataStart1 =…
Fredrik Enetorp
  • 414
  • 4
  • 15
15
votes
2 answers

Can I pass a null pointer to memcmp?

In particular, is the following well-defined, or does it exhibit undefined behavior? memcmp(0, 0, 0); Does this differ between C and C++? Ideally, please provide a quote from the standard(s).
avakar
  • 32,009
  • 9
  • 68
  • 103
11
votes
5 answers

Internal consistency of C structures

If I have two C structures initialised to have identical members, can I guarantee that: memcmp(&struct1, &struct2, sizeof(my_struct)) will always return zero?
Sparky
  • 2,694
  • 3
  • 21
  • 31
10
votes
6 answers

equivalent of memcmp() in Java?

If I have two byte[] arrays, is there a built-in function to compare them ala C's memcmp() ?
Jason S
  • 184,598
  • 164
  • 608
  • 970
9
votes
2 answers

overloading operator == for pods

I am working on some low level code with high level interfaces and felt need for comparisons operator for unit testing for plain old data types(like FILETIME struct) but since C++ doesn't even provide memberwise comparisons, so I wrote…
9
votes
3 answers

C++ is_trivially_copyable check

How to check whether or not C++ type is trivially copyable? I have a class, which uses memcpy and memcmp functions with specified template type T and I would like to fire assert for types, that are not safe to copy with memcpy. Is there any way to…
axe
  • 217
  • 3
  • 9
9
votes
1 answer

Can std::memcmp read any bytes past the first difference?

Consider: constexpr char s1[] = "a"; constexpr char s2[] = "abc"; std::memcmp(s1, s2, 3); If memcmp stops at the first difference it sees, it will not read past the second byte of s1 (the nul terminator), however I don't see anything in the C…
Ryan Haining
  • 35,360
  • 15
  • 114
  • 174
9
votes
1 answer

return value of memcmp(ptr1, ptr2, 0)?

In reading: How can I check that elements of an array are all same without using counter? , @Skizz uses the nifty solution: memcmp (&string [0], &string [1], sizeof string [0] * (N - 1)) So if N happens to be 1, we get memcmp (&string [0], &string…
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
8
votes
2 answers

Comparing two string literals using memcmp

I have compared two string literals using the memcmp function. #include #include int main() { char str1[] = "abcd"; char str2[] = "ab"; if (memcmp(str1, str2, 4) == 0) { printf("equal string\n"); } return…
msc
  • 33,420
  • 29
  • 119
  • 214
8
votes
6 answers

memcmp but need to compare block with fixed value

I need to compare a block of memory to a fixed value in C. Can I do this with memcmp? Something like: memcmp (starting_address , fixed_value , num_byte) I need fixed_value to be a fixed value not the starting address of a block. Writing the fixed…
Arash Fotouhi
  • 1,933
  • 2
  • 22
  • 43
7
votes
4 answers

Using memset on structures in C++

I am working on fixing older code for my job. It is currently written in C++. They converted static allocation to dynamic but didn't edit the memsets/memcmp/memcpy. This is my first programming internship so bare with my newbe-like question. The…
garry
  • 127
  • 1
  • 4
  • 11
7
votes
2 answers

Is there a memcmp equivalent for comparing byte arrays in Mono?

There is a well-known efficiency for comparing two byte-arrays in .Net by importing the memcmp function from msvcrt.dll, as described here. Is there an equivalent library import in mono? Would it need to be different when running mono on linux or…
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
6
votes
3 answers

Can i use memcmp two compare multibyte characters string?

I am trying to write code to compare two string. In windows i can use strcmp but i want write for multibyte character string so that it compatible to all other platform Can i use memcmp? if no then is there any other API i can use or i need to write…
Suri
  • 3,287
  • 9
  • 45
  • 75
1
2 3 4 5 6 7 8