Like Tree4Likes
  • 3 Post By oogabooga
  • 1 Post By laserlight

Memory Overlap and memcpy function.

This is a discussion on Memory Overlap and memcpy function. within the C Programming forums, part of the General Programming Boards category; Hello everybody. Code: #include<stdio.h> #include<string.h> int main(void) { char str[20] = "HELLOSIR"; memcpy( str + 2 , str + 1 ...

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

    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.
    Mr.Lnx, King Mir and iMalc like this.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    309
    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
    22,308
    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.
    Mr.Lnx likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    309
    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, 05:45 PM
  2. Replies: 2
    Last Post: 03-11-2009, 08:52 AM
  3. Memory overlap in function call
    By wots_guge in forum C Programming
    Replies: 5
    Last Post: 06-06-2006, 04: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21