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
1
vote
1 answer

Identify exactly where memcmp fails

Title is pretty self-explanatory, I am comparing two memory blocks that SHOULD be identical but I'm getting a failure. I have no idea where the test fails. Finding that out would help me debug the issue. So is there a way to find exactly where…
Nadir
  • 23
  • 2
1
vote
1 answer

How to prevent Atmel Studio gcc 6.3.1 from optimizing 4-byte memcmp() to a 4-byte direct comparison?

Running Atmel Studio with its provided gcc 6.3.1 to build firmware for an Atmel/Microchip SAMV70 (ARM Cortex-M7) chip. I have code that compares a 4-byte input array to a 4-byte local array using memcmp(). When compiled with -O0 to disable…
Andrew Cottrell
  • 3,312
  • 3
  • 26
  • 41
1
vote
3 answers

non recursive check of variable length string content

I have a function which takes a char* of variable length and the length. I want to check if its contents are all white spaces. It being a variable length, means I cant use a memcmp because I dont know how long to make the second parameter. Any…
matt
  • 243
  • 1
  • 7
1
vote
1 answer

Where can I find the definition of the macro __insn_dword_align that occurs in memcmp.c of the glibc source code project?

The implementation of function memcmp in glibc uses the macro DBLALIGN(eg, DBLALIG(a3, a0, srcli)) to compare two unsinged int integer. DBLALIGN is delcared as follows. However, the definition of __insn_dword_align is not found in glibc source…
B.Frank
  • 9
  • 4
1
vote
1 answer

C++: Is there a difference between calling an operator and calling it's implementation

I have a class in which I overloaded the == operator with memcmp() on a specific member. Due to a bad copy done in the code (memcpy called with bigger size than it should) I had a segfault when invoking the == operator. I understand that UB is…
CIsForCookies
  • 12,097
  • 11
  • 59
  • 124
1
vote
2 answers

Memcmp for two pointers with different data

This question is a bit hard to explain as the code snippet is part of a larger project. I will try to explain the problem to the best I can. I have two files FILE *f,*m; f=fopen("/home/machine/decoder.txt","a+"); …
user6837407
1
vote
2 answers

how to get substring in c with strchr

I'm trying to fetch part of a string. I have the following code: #include #include #include char mystring[]="The quick brown fox jumps over the lazy dog"; char word1[]="The"; char * posb,pose; char * word2; int…
spf
  • 11
  • 1
  • 3
1
vote
2 answers

Comparing part of a structure using memcmp in C

I have 2 structures of the same type and want to compare them. The size of the structure is 420 bytes and I want to skip over the first 2 bytes when I do the comparison since I know these will never match. I am using memcmp as follows: ` typedef…
anna
  • 19
  • 4
1
vote
1 answer

c# memcmp image compare error

im trying to compare 2 smalls blocks of image using the memcmp method. i saw this answer What is the fastest way I can compare two equal-size bitmaps to determine whether they are identical? and i tried to implement this in my project: private void…
user3548681
1
vote
1 answer

faster memory compare for equality 16 bytes block than memcmp

I have to compare for equality 16 bytes memory blocks in a very performance sensitive place. The blocks are always perfectly aligned and they are always exactly 16 bytes. It seems to me that I should be able to utilize this knowledge and come up…
gsf
  • 6,612
  • 7
  • 35
  • 64
1
vote
2 answers

Unexpected memcmp return value

I made a INI file parser, it works well on Windows but not with Linux, the problem comes from the memcmp function, it doesn't return 0 when it should, I already checked with printf and strlen, (I also tried to use strncmp instead, it returned…
Aleksandair
  • 167
  • 1
  • 2
  • 11
1
vote
1 answer

Segfault while using memcmp()

I'm getting segfault in this line: if(memcmp(datap, 0x38 , 1) == 0) This is a trace from gdb, you can see datap here: Program received signal SIGSEGV, Segmentation fault. 0x00000000004010f1 in processMTMHeader ( datap=0x2aaaab0b001c…
MaMu
  • 1,837
  • 4
  • 20
  • 36
1
vote
3 answers

Using file i/o to read byte length

I'm trying to find the byte length of two different files with the following code, but get the byte length as 1, which is obviously wrong. In the long run, I'm trying to compare memory positions of each file and print out where they differ as you'll…
Question_Guy
  • 323
  • 2
  • 3
  • 17
1
vote
1 answer

Using memcpy or memcmp with ranges

Is it possible to use a range when using memcpy or memcmp? char data[900000]; // size 900000 char array[20]; // size 20 if (memcmp(data[50-70], array, 20) == 0) { // do thing } I'd like to be able to compare the (20) keys data[50-70] with…
user3236174
1
vote
1 answer

Array compare with memcmp vs element by element comparsion

Which implementation is better and why? Element by Element comparison using a for loop or memcmp() implementation int a[] = {1,2,3}; int b[] = {1,3,5}; memcmp(a, b, sizeof(int)*n) OR for (i = 0; i < n ; i++) { if (a[i] == b[i]) …
user1581106