Thread: Memory Overlap and memcpy function.

  1. #1
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357

    Memory Overlap and memcpy function.

    Hello everybody.

    Code:
    #include<stdio.h>
    #include<string.h>
    
    int main(void)
    {
        char str[20] = "HELLOSIR";
        
        memcpy( str + 2 , str + 1 , 4 );
        
        puts( str );
        
        return 0;
                      
    }
    If I have understood well the above code can be a typical example that decribes a memory overlap. Some of data to the destination (str + 2 ) will be copied before its copy.

    Code:
      void *memcpy( void * restrict s1, const void * restrict s2, size_t n );
    According to the above example I think there is no quarantee even the restrict to the pointer that we won't have overlap.It is legal to use the same pointer and not other in order to have access on the data.So for this the behaviour is not undefined right?

    But how memcpy works? I mean I am taking

    Code:
     HEELLOIR
    as output rather than

    Code:
     HEEEEEIR
    So the behaviour due to overlap is undefined?

    p.s The const on const void * restrict s2 denotes that data can't change from s2 itself?

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    A restricted pointer is supposed to be the only pointer to its object.
    It's the programmer's responsibility to ensure that's true.
    Your call is therefore undefined since str+2 and str+1 point into the same string.

    memmove is the correct standard library function to use to when the buffers may overloap.

    The const in memcpy's declaration says that memcpy won't write to the object through the second pointer.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    A pointer of any type can be converted to void * automatically , so why I can't pass strcmp itself to qsort,since qsort requires a comparison function with two const void * parameters?

    On this case why const char * from strcmp won't be converted to const void * ?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Mr.Lnx
    A pointer of any type can be converted to void * automatically , so why I can't pass strcmp itself to qsort,since qsort requires a comparison function with two const void * parameters?
    Because qsort expects a pointer to a function of a particular type, but a pointer to strcmp is not of that type.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    Thank you

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    And even if you force the compiler to accept strcmp as the compare function, it would just be comparing the strings addresses (as strings) ! That's because qsort will pass char**'s (as void*'s) but strcmp will treat them as char*'s. It needs to dereference the pointers twice but it's coded to do so only once.

    So you need something like this to point strcmp to the right data:
    Code:
    int comp(const void *a, const void *b) {
        return strcmp(*(const char**)a, *(const char**)b);
    }
    ...
        char *logic[] = {"nor", "xor", "and", "not", "or"};
        qsort(logic, sizeof(logic)/sizeof(*logic), sizeof(*logic), comp);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 77
    Last Post: 10-15-2011, 04:45 PM
  2. Replies: 2
    Last Post: 03-11-2009, 07:52 AM
  3. Memory overlap in function call
    By wots_guge in forum C Programming
    Replies: 5
    Last Post: 06-06-2006, 03:22 AM
  4. making a absolute overlapped window, even overlap games
    By hanhao in forum C++ Programming
    Replies: 1
    Last Post: 03-22-2004, 08:53 AM
  5. does memcpy create a new block of memory?
    By Diamonds in forum C++ Programming
    Replies: 2
    Last Post: 02-20-2003, 12:19 AM