Thread: Can't piece together this code

  1. #1
    Registered User
    Join Date
    Feb 2023
    Posts
    1

    Can't piece together this code

    Hello,

    Does this code look sane? I'm supposed to be checking mirrored values for card images that will be emulated and I am not sure of the pointer arithmetic performed on LHS, RHS memset values. buf is a fixed with integer pointer malloc'd some multiple of 2, seems to me we're comparing the same memory?

    Code:
    int check_buf(void *buf, int size, int full_size)
    {   
        if (size / 2 && memcmp(buf, buf + size / 2, size / 2) == 0)
        {
            if (!check_buf(buf, size / 2, full_size))
            {
                printf("Mirrored memory.");
                assert(0);
            }
            return 1;
        }
        else
        {
            return 0;
        }
    }
    Best
    Last edited by Xhy; 02-06-2023 at 01:57 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    When posting code, try to make a minimally complete example out of it.
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <assert.h>
    
    int check_buf(void *buf, int size, int full_size)
    {   
        if (size / 2 && memcmp(buf, buf + size / 2, size / 2) == 0)
        {
            if (!check_buf(buf, size / 2, full_size))
            {
                printf("Mirrored memory.");
                assert(0);
            }
            return 1;
        }
        else
        {
            return 0;
        }
    }
    
    int main()
    {
        char buff1[] = "ABCDDCBA";
        char buff2[] = "12345678";
        char buff3[] = "AAAAAAAA";
        printf("R1=%d\n", check_buf(buff1, 8, 8));
        printf("R2=%d\n", check_buf(buff2, 8, 8));
        printf("R3=%d\n", check_buf(buff3, 8, 8));
        return 0;
    }
    
    
    $ gcc -Wall foo.c
    $ ./a.out 
    R1=0
    R2=0
    a.out: foo.c:13: check_buf: Assertion `0' failed.
    Aborted (core dumped)
    2. Calling assert is dumb.
    You do all that work and bomb out with a segfault.

    3. Cite your source. I'm assuming you found this code somewhere and are confused.
    Knowing the context of how it is used where you found it could be useful.

    > Does this code look sane?
    The recursion is weird to say the least.

    > I'm supposed to be checking mirrored values for card images
    Define "mirrored".

    To me, two mirrored memory cards would compare identical with a single memcmp.
    All this size/2 and recursion is baffling.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do I fix this piece of code?
    By deepee in forum C Programming
    Replies: 32
    Last Post: 12-04-2013, 08:06 PM
  2. Anyone with a Mac willing to compile a piece of code?
    By thefeedinghand in forum C++ Programming
    Replies: 9
    Last Post: 09-18-2010, 08:41 PM
  3. Help with a piece of code
    By Victor4015 in forum C++ Programming
    Replies: 1
    Last Post: 11-16-2005, 05:38 PM
  4. Help with a little piece of code
    By cdonlan in forum C Programming
    Replies: 5
    Last Post: 11-15-2004, 12:38 PM
  5. question about a piece of code
    By librab103 in forum C Programming
    Replies: 6
    Last Post: 06-27-2003, 01:11 PM

Tags for this Thread