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
0
votes
0 answers

fast compare array of unsigned numbers

Consider this code: constexpr size_t size = 32; constexpr size_t count = 8; using WordCode = unsigned; template int CmpHashArray(const T *l,const T *r) { auto * l1 = reinterpret_cast(l); auto * r1 =…
user5821508
  • 332
  • 2
  • 10
0
votes
2 answers

Can I use memcmp to check struct for non-zero members?

I am using a large struct with many members, and I would like an easy way to quickly see if any of the members are non-zero. I know that memcmp() should not be used to compare two structs for equality (as explained here: How do you compare structs…
phreaknik
  • 615
  • 5
  • 12
0
votes
3 answers

How to use JNI to call MEMCMP from Java

I need to compare 2 byte arrays and know which one is bigger or if they are equal (just equal or different is not enough). The byte arrays represent a String value of 15 characters or more. This comparison is repeated considerably in my code. I…
Diana
  • 41
  • 1
  • 2
0
votes
5 answers

Why do the addresses of initial array elements compare equal?

I have been working on a project and I spent the last hour trying to find the bug in my code. After closer inspection, I noticed something rather odd which has been the problem all along. The addresses of the initial elements of my array are…
user6274708
0
votes
0 answers

c strncasecmp that can handle NULL (character 0)

I have strings that may contain character 0. They are stored in a structure like this: typedef struct somestruct_s { const unsigned char *string; size_t length; }; If I wish to compare 2 of these together I can use memcmp as such: int…
B. Nadolson
  • 2,988
  • 2
  • 20
  • 27
0
votes
1 answer

Performance issue while searching string in memory

I'm developing DLL under Win32 that makes a simple job: it scans host's virtual memory for substring. But for some reason it does it very slow comparing to Cheat Engine, ArtMoney or even OllyDbg that uses single thread to scan. Here's the code of…
Dr Morgan
  • 1
  • 1
0
votes
1 answer

What will happen we pass -1 as value for 3rd parameter in strncmp() ie. n number bytes of bytes to compare?

I have three questions, What will happen we pass -1 as value for 3rd parameter in strncmp() ie. n number bytes of bytes to compare? Why the output differs in ARM and PPC? ie. if which one correct? If I use memcmp instead of strncmp, I m getting…
Harini
  • 13
  • 1
0
votes
1 answer

Compare two unsigned char structures and bitfields

I have a structure containing unsigned chars and bitfields: struct { unsigned char byt1 ; unsigned char var1 :1; unsigned char byt2 ; unsigned char var2 :1; unsigned char var3 :1; unsigned char var4 :1: } struct1; I want to compare this…
zacharoni16
  • 305
  • 1
  • 4
  • 16
0
votes
4 answers

fastest u_int64_t[8] array compare in C/C++

What is the fastest method, to compare two u_int64[8] arrays in C/C++ ? Array 1 is inside a std::vector (~10k elements) array 2 is inside a dynamic allocated struct. (is memcmp() here false positive free?) My (pseudo C) implementation : typedef…
gj13
  • 1,314
  • 9
  • 23
0
votes
1 answer

c++ string pattern matching buffer data

I have inbound buffer data from a script that I need the key => 'value' to so that I can run a math equation against it (yes, I know I need to convert to int). Since I am sure the data is string, I trying to run a pattern match against it. I see the…
brad
  • 870
  • 2
  • 13
  • 38
0
votes
0 answers

Trying to get fast reverse memcmp in C++

I need to compare two char arrays as fast as possible and return which one is bigger. Normally I would use memcmp but unfortunately, there's only a pointer available for my tool pointing to the data stored in reverse order, or to the LSB in other…
Chili
  • 5
  • 4
0
votes
3 answers

c++ best way to compare byte array to struct

I need help. I have an unsigned char * and say I have a struct struct{ int a=3; char b='d'; double c=3.14; char d='e'; } cmp; unsigned char input[1000]; l= recv(sockfd,input , sizeof(cmp),0); I want to compare cmp and…
user3260595
  • 53
  • 1
  • 8
0
votes
2 answers

Displaying hex values inside an array to stdout, and comparing input to those hex values

This is my current code: #include #include #include int main() { int target; char array[] = {'AC', 'EE', '88', '0D', '87'}; printf("Enter the target byte as a hex value: \n"); scanf("%x",…
relapsn
  • 85
  • 7
0
votes
2 answers

Most performant way to compare first 2 bytes (given a pointer) to fixed values?

Given a pointer I want to compare the first two bytes to fixed values. data is a void pointer. Is there a "better" way than this: unsigned char foo[] = {0xFF, 0x3B}; memcmp(data, foo, 2); Maybe where I dont have to create a new char array? Thanks!
tzippy
  • 6,458
  • 30
  • 82
  • 151
0
votes
1 answer

compare before update(memcmp before memcpy), or just update, which performance is better?

The context(although not important), in a netfilter module, we use struct like: struct data { char mac[ETH_ALEN]; char in6_addr addr; }; to keep track of MAC address and ipv6 address. for handling ipv6 address changes: should I compare the…
Ted Feng
  • 829
  • 1
  • 17
  • 22