🌐
GitHub
github.com › gcc-mirror › gcc › blob › master › libiberty › memcmp.c
gcc/libiberty/memcmp.c at master · gcc-mirror/gcc
memcmp (const void *str1, const void *str2, size_t count) { register const unsigned char *s1 = (const unsigned char*)str1; register const unsigned char *s2 = (const unsigned char*)str2; · while (count-- > 0) { if (*s1++ != ...
Author   gcc-mirror
🌐
TutorialsPoint
tutorialspoint.com › c_standard_library › c_function_memcmp.htm
C library - memcmp() function
The C library memcmp() function can be used to compare two blocks of memory. In general, it is used to compare the binary data or raw data. Here, memcmp() accepts three variable as parameters that compares the first n bytes of memory area str1 and
🌐
GitHub
github.com › lattera › glibc › blob › master › string › memcmp.c
glibc/string/memcmp.c at master · lattera/glibc
February 5, 2022 - # define CMP_LT_OR_GT(a, b) memcmp_bytes ((a), (b)) #endif · · /* BE VERY CAREFUL IF YOU CHANGE THIS CODE! */ · /* The strategy of this memcmp is: · 1. Compare bytes until one of the block pointers is aligned. · 2. Compare using memcmp_common_alignment or ·
Author   lattera
🌐
cppreference.com
en.cppreference.com › c › string › byte › memcmp
memcmp - cppreference.com
#include <stdio.h> #include <string.h> void demo(const char* lhs, const char* rhs, size_t sz) { for(size_t n = 0; n < sz; ++n) putchar(lhs[n]); int rc = memcmp(lhs, rhs, sz); const char *rel = rc < 0 ? " precedes " : rc > 0 ?
🌐
KooR
koor.fr › C › cstring › memcmp.wp
KooR.fr - memcmp - Langage C
#include <assert.h> #include <stdio.h> ... 54, 85, 19, 63, 21 }; size_t size = sizeof( int ) * 5; assert( memcmp( array1, array2, size) == mymemcmp( array1, array2, size) ); assert( memcmp( array1, array1, size) == mymemcmp( array1, ...
🌐
Aticleworld
aticleworld.com › home › how to use and implement own memcmp in c
How to use and Implement own memcmp in C - Aticleworld
May 7, 2021 - Your compiler/standard library will likely have a very efficient and tailored implementation of the memcmp() function.
Find elsewhere
🌐
Clc-wiki
clc-wiki.net › wiki › C_standard_library:string.h:memcmp
C standard library:string.h:memcmp - clc-wiki
#include <stddef.h> /* size_t */ int memcmp(const void* s1, const void* s2,size_t n) { const unsigned char *p1 = s1, *p2 = s2; while(n--) if( *p1 != *p2 ) return *p1 - *p2; else p1++,p2++; return 0; }
🌐
Urho3D
docs.huihoo.com › doxygen › postgresql › memcmp_8c_source.html
PostgreSQL Source Code: src/port/memcmp.c ... - Huihoo
00001 /*------------------------------------------------------------------------- 00002 * 00003 * memcmp.c 00004 * compares memory bytes 00005 * 00006 * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group 00007 * 00008 * 00009 * IDENTIFICATION 00010 * src/port/memcmp.c 00011 * 00012 * This file was taken from NetBSD and is used by SunOS because memcmp 00013 * on that platform does not properly compare negative bytes. The 00014 * NetBSD copyright terms follow. 00015 */ 00016 00017 /*- 00018 * Copyright (c) 1990, 1993 00019 * The Regents of the University of California. All rights reserved. 00020 * 00021 * This code is derived from software contributed to Berkeley by 00022 * Chris Torek. 00023 * 00024 * Redistribution and use in source and binary forms, with or without 00025 * modification, are permitted provided that the following conditions 00026 * are met: 00027 * 1.
🌐
Clc-wiki
clc-wiki.net › wiki › memcmp
memcmp - clc-wiki
June 11, 2014 - #include <stddef.h> /* size_t */ int memcmp(const void* s1, const void* s2,size_t n) { const unsigned char *p1 = s1, *p2 = s2; while(n--) if( *p1 != *p2 ) return *p1 - *p2; else p1++,p2++; return 0; }
🌐
GitHub
github.com › esmil › musl › blob › master › src › string › memcmp.c
musl/src/string/memcmp.c at master · esmil/musl
int memcmp(const void *vl, const void *vr, size_t n) { const unsigned char *l=vl, *r=vr; for (; n && *l == *r; n--, l++, r++); return n ?
Author   esmil
🌐
W3Resource
w3resource.com › c-programming › string › c-memcmp.php
C memcmp() function
November 2, 2024 - C memcmp() function (string.h): The memcmp() function compares the first n bytes of str1 and str2.
Top answer
1 of 3
3

From the C Standard (7.24.4.1 The memcmp function)

Returns

3 The memcmp function returns an integer greater than, equal to, or less than zero, accordingly as the object pointed to by s1 is greater than, equal to, or less than the object pointed to by s2.

2 of 3
3

The C Standard (C2x) specifies this:

7.26.4 Comparison functions

The sign of a nonzero value returned by the comparison functions memcmp, strcmp, and strncmp is determined by the sign of the difference between the values of the first pair of characters (both interpreted as unsigned char) that differ in the objects being compared.

[...]

The memcmp function returns an integer greater than, equal to, or less than zero, accordingly as the object pointed to by s1 is greater than, equal to, or less than the object pointed to by s2.

The posted code for memcmp is just a possible implementation. The C Standard does not specify the exact return value for blocks with different contents, just the sign of the return value. Hence returning s1[-1] - s2[-1] is just as compliant as returning s1[-1] < s2[-1] ? -1 : 1 or 1 - 2 * (s1[-1] < s2[-1])

Note also that your implementation has problems:

  • it makes no sense to test ((char *)s1)[i] == '\0') as memcmp does not stop on any null terminator.
  • comparing the byte values as char values is incorrect: the C Standard specifies that the comparison must be performed using the unsigned char values.

Here is a modified version:

#include <stddef.h>

int ft_memcmp(const void *s1, const void *s2, size_t n)
{
    size_t  i;

    i = 0;
    if (n == 0)
        return (0);

    while (((unsigned char *)s1)[i] == ((unsigned char *)s2)[i])
    {
        i++;
        if (i == n)
        {
            return (0);
        }
    }
    if (((unsigned char *)s1)[i] < ((unsigned char *)s2)[i])
        return (-1);
    else
        return (1);
}

The main function is incorrect:

  • it must only check for zero values and that the signs of ft and lib match if both are non zero.
  • the count passed to both functions should not exceed the size of the objects. This explains why you get inconsistent results when count is greater than 7 for the identical strings: the contents of memory beyond the first 8 or so bytes or the identical strings differ. Reading the bytes beyond the end of the strings (ie: after the null terminator) has undefined behavior anyway.

Here is a modified version:

int main(void) {
    const char *test_strings1[] = {
        "fdjkDKDJFLDkjdfkjdf", "-456", "ALO marciano!!!", "xc42:",
        "     7894545989828547", "  +99", "abc123", "12abc", ""
    };
    const char *test_strings2[] = {
        "fdjkDKDJFLDSkjdfkjdf", "-456", "ALO_ALO marciano!!!", "xc42",
        "   789454598982854752", "  +99", "abc123", "12abc", ""
    };
    int test_count = sizeof(test_strings1) / sizeof(*test_strings1);

    for (int i = 0; i < test_count; i++) {
        size_t size1 = strlen(test_strings1[i]) + 1;
        size_t size2 = strlen(test_strings2[i]) + 1;
        for (size_t count = 0; count <= size1 && count <= size2; count++) {
            int ft = ft_memcmp(test_strings1[i], test_strings2[i], count);
            int lib = memcmp(test_strings1[i], test_strings2[i], count);
       
            if ((ft < 0 && lib >= 0) 
            ||  (ft == 0 && lib != 0)
            ||  (ft > 0 && lib <= 0)) {
                printf("*** Wrong!!!  lib  %i  ft  %i  count = %zu  string = %i ***\n",
                       lib, ft, count, i);
            }
        }
    }
    return 0;
}
🌐
GeeksforGeeks
geeksforgeeks.org › c language › memcmp-in-c
memcmp() in C - GeeksforGeeks
January 8, 2025 - In C, memcmp() is a built-in function used to compare the given number of bytes of data pointed by two pointers passed as arguments.
🌐
Fossies
fossies.org › linux › musl › src › string › memcmp.c
musl: src/string/memcmp.c | Fossies
March 1, 2024 - As a special service "Fossies" ... and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "memcmp.c" see the Fossies "Dox" file reference documentation....