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

Why are negative numbers greater than positive numbers?

I made my bubble sort program generic. I went on testing it and it was working well until I placed a negative number in the array, and I was surprised that it was pushed to the end, making it bigger than positive numbers. Obviously memcmp is the…
machine_1
  • 4,266
  • 2
  • 21
  • 42
3
votes
1 answer

Can segmentation fault occur for reading memory?

I think reading memory should not cause any issue eg char *d=""; char *d2="test"; memcmp(d,d2,10); Can memcmp() ever fail ?
Zxcv Mnb
  • 733
  • 8
  • 19
3
votes
3 answers

Why is this slower than memcmp

I am trying to compare two rows of pixels. A pixel is defined as a struct containing 4 float values (RGBA). The reason I am not using memcmp is because I need to return the position of the 1st different pixel, which memcmp does not do. My first…
Rotem
  • 21,452
  • 6
  • 62
  • 109
2
votes
1 answer

memcmp return value, inconsistent comportment

Here is a small C code to highlight an issue on memcmp return value: char *str1 = "\200"; char *str2 = "\0"; int val1 = memcmp(str1, str2, 2); int val2 = memcmp("\200", "\0", 2); printf("val 1 : %d \n",val1); printf("val 0…
2
votes
4 answers

What, exactly, is memcmp supposed to return?

I would like to know what the function memcmp must return. I've been searching on the Internet, and usually, memcmp definitions state something like the following: The memcmp() function returns an integer greater than, equal to, or less than zero,…
nounoursnoir
  • 671
  • 2
  • 10
  • 25
2
votes
3 answers

The use of strncmp and memcmp

Does if(strncmp(buf, buf2, 7) == 0) do the same thing as if(memcmp(buf, buf2, 7) == 0) buf and buf2 are char* arrays or similar. I was going to append this to another question but then decided perhaps it was better to post it separately.…
FreelanceConsultant
  • 13,167
  • 27
  • 115
  • 225
2
votes
1 answer

Custom memcmp() of a struct with char member

I've written the following C code to compare two areas of memory and check if they are identical #include struct s1 { int a, b; char c; } s1; int memcmpwannabe(void* value1, void *value2, int size1, int size2) { if(size1 !=…
debevv
  • 145
  • 2
  • 8
2
votes
1 answer

Performance of the Equal operator Vs memcmp for primitive data types

I have been using memcmp function for compare 2 integers in my performance critical application. I had to use this other than using equal operators as I have to deal with the other datatypes generically. However, I suspected the memcpy performance…
Sujith Gunawardhane
  • 1,251
  • 1
  • 10
  • 24
2
votes
2 answers

What does size of the memcmp return value mean?

I just happened to debug an incredibly nasty error: On my own PC (Windows 7 x64, MinGw) my C program would successfully sort an array using the memcmp when comparing array members. My function used bubble sort algorithm and it's skeleton would look…
Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
2
votes
3 answers

why memcmp returns -1 although equal

I'm comparing using memcmp() two variables of the same struct (the struct has union in it). The variables are in two arrays and I'm running a loop where each iteration I do memcmp(&arr1[i], &arr2[i], sizeof(arrtype)). When debugging I see that…
theWizard
  • 111
  • 2
  • 10
2
votes
0 answers

using memcmp from within device code CUDA

I am using uthash (http://uthash.sourceforge.net/) for hash table implementation in my CUDA C program. I have a bunch of keys say allkeys[100]. What I would like to do is, perform a parallel hash table look up using those 100 keys on the hash table…
dparkar
  • 1,934
  • 2
  • 22
  • 52
1
vote
1 answer

memcmp to compare segments of an array (remove duplicates)

I've been working on this for a while (in C) and can't figure it out. I have a buffer containing an array of chars. I've used qsort to sort through the array and it's all in proper order now. I now need to remove duplicates (or just print out the…
pauliwago
  • 6,373
  • 11
  • 42
  • 52
1
vote
2 answers

How the memcmp on structure with integer variable in c lang compares. Result is not as expected

I have a structure with integer, I am comparing the struct by using memcmp, I don't want to use other memthods. #include #include typedef struct Foo { int d; } Foo; int main(int argc, const char *…
1
vote
1 answer

Are there other C standard library functions like memcmp that have timing side-channel risk?

I found that memcmp() will return false earlier if the first byte is different in both strings, and I thought it has a timing attack risk. However, when I tried to find out if there were other functions that had side-channel risks like memcmp, I…
1
vote
2 answers

Ada issue with STORAGE_ERROR

I have this small function that is causing me headaches on RHEL6, and I am not sure what is going on... and it is a pain to debug. When I run this I am getting a STORAGE_ERROR, so I did a gstack to see where the program is hanging (See below). …
mainstringargs
  • 13,563
  • 35
  • 109
  • 174