Thread: The keyword restrict in the prototype of memcpy function.

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

    The keyword restrict in the prototype of memcpy function.

    I have a query about memcpy's prototype.

    Since the arguments are restricted pointers and according to a restricted pointer only that pointer has access to the object how the function works? and copies the bytes from one pointer to another?

    I think overlapping situation is something like :

    Code:
    #include<stdio.h>
    
    int main(void)
    {
      char msg[60]="abcdefghijklmnopqrstuvwxyz";
     char tmp[60];
    
    strcpy(tmp , msg);
    memcpy(tmp+4 , tmp+16 , 10); /* without overlap temp is : abcdqrstuvwxyzopqrstuvwxyz */
    
    strcpy(tmp,msg);
    memcpy(tmp+6, tmp+4,10); /* with overlap temp is :
    abcdefefefefefefqrstuvwyz */
    
    return 0;
    }
    for example here :

    Code:
    // C99 code
    void *memcpy(void * restrict dest, const void * restrict src, size_t n)
    {
        char *d = dest;
        const char *s = src;
        size_t i;
        for (i = 0; i != n; ++i)
            d[i] = s[i];
        return dest;
    }
    How the assignment d[i]=s[i] works? since d is restricted.

    I am confused when I see the keyword restrict in the function although I know its concept.
    Last edited by Mr.Lnx; 05-01-2015 at 12:39 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Mr.Lnx
    Since the arguments are restricted pointers and according to a restricted pointer only that pointer has access to the object how the function works? and copies the bytes from one pointer to another?

    I think overlapping situation is something like :
    Refer to the C standard:
    Quote Originally Posted by C11 Clause 7.24.2.1
    The memcpy function

    Synopsis
    Code:
    #include <string.h>
    void *memcpy(void * restrict s1,
                 const void * restrict s2,
                 size_t n);
    Description
    The memcpy function copies n characters from the object pointed to by s2 into the object pointed to by s1. If copying takes place between objects that overlap, the behavior is undefined.

    Returns
    The memcpy function returns the value of s1.
    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

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Restrict doesn't mean nobody else can access the data. It declares that there isn't some other pointer floating around that points at that data. That's just advisory information.

    In the prototype of memcpy(), it serves to document the fact that the source and destination regions are not allowed to overlap each other.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    Quote Originally Posted by brewbuck View Post
    Restrict doesn't mean nobody else can access the data. It declares that there isn't some other pointer floating around that points at that data. That's just advisory information.

    In the prototype of memcpy(), it serves to document the fact that the source and destination regions are not allowed to overlap each other.
    And why my book writes :

    "A pointer that is been declared using restrict is called restricted pointer.The intent is that if p points to an object that is later modified ,then that object is not accessed in any way other than through p.(Alternative ways to access the object include having another pointer to the same object or having p point to a nammed variable.) Having more than one way to access an object is called aliasing."

    the statement in memcpy above function
    Code:
     d[i] = s[i] ;
    is not an access?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Mr.Lnx
    the statement in memcpy above function
    Code:
     d[i] = s[i] ;
    is not an access?
    It is an access, but since there is no overlap, at no point do you access an element through d that is also accessed through s.
    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

  6. #6
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    Ok but with this the concept of restriction of a pointer does not corrupted?

    The behavior if you have an access to a pointer that have been declared as restricted does not undefined?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Mr.Lnx
    Ok but with this the concept of restriction to a pointer does not corrupted?
    What do you mean by "corrupted"?
    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

  8. #8
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    Quote Originally Posted by laserlight View Post
    What do you mean by "corrupted"?
    I mean that is canceled. When you say that you declare a pointer to be restricted how you can have access from some other? live the previous examples. The only access is from one pointer.

    For example if you have :

    Code:
      memcpy( str , str+2 , N);
    So this is a possible overlapping. But the call
    Code:
     memcpy( str+2 , str , N)
    is a certain overlapping.

    Maybe you mean if we have a different strings for example string1 and string2.

    Code:
      char string1[N]="bla bla";
              char string2[N]="bla blah";
    So on this case you are right since there is no overlap, at no point do we access an element through d that is also accessed through s. The copy comes from *s1++ = *s2++; statement there is no some assignment s1=s2; I have only this on my brain in order to understand the concept of restriction. Sorry
    Last edited by Mr.Lnx; 05-02-2015 at 12:20 PM.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Mr.Lnx
    I mean that is canceled. When you say that you declare a pointer to be restricted how you can have access from some other? live the previous examples. The only access is from one pointer.
    Please show how you have access from some other pointer. I have already told you that that does not happen. The only access is from one pointer.

    EDIT:
    I quoted the C standard is post #2: if there is overlap, the behaviour is undefined. In other words, if you violate the pre-condition, then you have this logical problem of aliasing despite the use of restrict, but that is a bug with the memcpy call, not with memcpy itself. Fix your bug instead of complaining about restrict and memcpy.
    Last edited by laserlight; 05-02-2015 at 12:24 PM.
    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

  10. #10
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    Quote Originally Posted by laserlight View Post
    Please show how you have access from some other pointer. I have already told you that that does not happen. The only access is from one pointer.
    Sorry but how is from one pointer? s[i] is also a pointer right? the function treats it as a pointer on its body like we know. And d[i] is also a pointer in the function only! So we copy from s[i] to d[i] so it is an access from a different pointer (the s[i]). I am confused. :/

    p.s Look at the previous msg please. It is edited. Thank you.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Mr.Lnx
    Sorry but how is from one pointer? s[i] is also a pointer right? the function treats it as a pointer on its body like we know. And d[i] is also a pointer in the function only! So we copy from s[i] to d[i] so it is an access from a different pointer (the s[i]). I am confused. :/
    s[i] is not a pointer: it is a const char. s is a pointer, but its value is based on src, so there is no problem: ultimately, access to the object only happens via src.
    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

  12. #12
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    Hmm ok. I got it. With the restrict keyword we don't want two pointers point to the same thing. Pointing to the same thing is always a possibly situation for overlapping.We should use memmove instead in order to avoid this problem.Thank you very much for your time laserlight.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function Prototype, Function Call, and Function definition
    By dmcarpenter in forum C Programming
    Replies: 9
    Last Post: 04-09-2013, 03:29 AM
  2. Replies: 8
    Last Post: 07-18-2012, 08:53 PM
  3. Replies: 2
    Last Post: 03-11-2009, 07:52 AM
  4. Replies: 13
    Last Post: 08-24-2006, 12:22 AM
  5. restrict and function pointers
    By Laserve in forum C Programming
    Replies: 13
    Last Post: 06-27-2004, 01:42 AM