Thread: Restricted pointers C99.

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

    Restricted pointers C99.

    Hello to all according to the theory this snippet of code :

    Code:
    #include<stdio.h>
    void f( int * restrict p , int *q);
    
    int main( void )
    {
    	int x=3;
    	
    	f(&x , &x);
    	
    	printf("%d" , x);
    	
    return 0;
    }
    void f ( int * restrict p , int *q ) 
    {
    	(*q) += 1;  // x = 4
    	
    	*q = *p + 2;  // x = 6
    	
    	return;
    }
    procudes Undefined Behaviour or not? I think yes... but I am not sure.

    Thank you in advance for your time.
    Last edited by Mr.Lnx; 07-19-2013 at 07:10 AM.

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Yes, this is undefined behaviour - qualifying 'p' with 'restrict' says that all accesses to the object pointed to by 'p' in that block will be through 'p' or the result will be undefined. However, all accesses to the object pointed to by 'p' are not made through 'p' since some accesses to that object are made through 'q'.
    Programming and other random guff: cat /dev/thoughts > blogspot.com (previously prognix.blogspot.com)

    ~~~

    "The largest-scale pattern in the history of Unix is this: when and where Unix has adhered most closely to open-source practices, it has prospered. Attempts to proprietarize it have invariably resulted in stagnation and decline."

    Eric Raymond, The Art of Unix Programming

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Declaring a pointer as "restrict"ed is a solemn covenant between the programmer and the compiler that that pointer will be the only one accessing that object in the block. It is the programmer's responsibility to ensure this. If he does not, the result is "undefined behavior".

    Restrict potentially allows the compiler to use some optimizations.

    In your code it could produce a result other than x=6 if the assembly code retrieved *p before *q++, then used the old value in *q = *p + 2. In that case, x=5.

    The compiler would be justified in emitting that code since *p doesn't appear on the LHS of any assignments and we've promised that p is the only pointer to its object.

  4. #4
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    Ok we solved this. It is Undefined behaviour when you use restricted pointers with that why... It is your responsibility to ensure that your code will avoid the UB phenomena.

    I have some queries-issues too ....

    1. However, all accesses to the object pointed to by 'p' are not made through 'p' since some accesses to that object are made through 'q'.

    Do you have an example????

    2.
    If a restricted pointer is declared as a local variable without the extern storage class, restrict applies only to p when the block in which p is declared is being executed ..... When restrict is applied to a pointer variable with file scope, however the restriction lasts for the entire execution of the program


    It follows the same rules for the ordinary variables... like local variables and external however it is logical because a pointer is exactly a variable.

    3. There are even situations in which an alias created from a restricted pointer is legal.For example a restricted pointer p can be legally copied into another restricted pointer variable q, provided that p is local to a function and q is define inside a block nested within the function body

    Code:
    #include<stdio.h>
    void f( int * restrict p , int *q);
     
    int main( void )
    {
        int x=3;
         
        f(&x , &x);
         
        printf("%d" , x);  // 5
         
    return 0;
    }
    void f ( int * restrict p , int * restrict q )
    {
       q = p;
       
       *q = 5;
         
        return;
    }
    This is legal???? Inside a block nested within the function body???? Hmmm I think is something like {...} in the function I am not sure that I have understood clearly... any help???

    Thank you for the discussion.
    Last edited by Mr.Lnx; 07-19-2013 at 12:36 PM.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Quote Originally Posted by Mr.Lnx View Post
    1. However, all accesses to the object pointed to by 'p' are not made through 'p' since some accesses to that object are made through 'q'.

    Do you have an example????
    Erm... you gave the example in your first post...

    Quote Originally Posted by Mr.Lnx View Post
    Code:
    *q = *p + 2;  // x = 6
    Here you access the same object through both 'q' and 'p'.

    Can I ask what the end-goal is here?
    Programming and other random guff: cat /dev/thoughts > blogspot.com (previously prognix.blogspot.com)

    ~~~

    "The largest-scale pattern in the history of Unix is this: when and where Unix has adhered most closely to open-source practices, it has prospered. Attempts to proprietarize it have invariably resulted in stagnation and decline."

    Eric Raymond, The Art of Unix Programming

  6. #6
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    Quote Originally Posted by JohnGraham View Post
    Erm... you gave the example in your first post...



    Here you access the same object through both 'q' and 'p'.

    Can I ask what the end-goal is here?
    Yes sorry.

    There is no some specific end-goal that I want to accomplish here ... I just have an example for this concept restricted pointers.

    Do you have any comment about my #3?????

    Thank you.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Quote Originally Posted by Mr.Lnx View Post
    3. There are even situations in which an alias created from a restricted pointer is legal.For example a restricted pointer p can be legally copied into another restricted pointer variable q, provided that p is local to a function and q is define inside a block nested within the function body

    This is legal???? Inside a block nested within the function body???? Hmmm I think is something like {...} in the function I am not sure that I have understood clearly... any help???
    I don't know what you intend to show in your example, but it doesn't appear to bear any relation to point 3. Also, you should make it clear where you get that point from - is it something you are asserting is true, or something you have been told? It seems to have materialised out of thin air.

    The C99 standard gives the following example:

    Quote Originally Posted by C99 Section 6.7.3.1, point 11
    The rule limiting assignments between restricted pointers does not distinguish between a
    function call and an equivalent nested block. With one exception, only ‘‘outer-to-inner’’ assignments
    between restricted pointers declared in nested blocks have defined behavior.

    Code:
    {
        int * restrict p1;
        int * restrict q1;
        p1 = q1; // undefined behavior
        {
            int * restrict p2 = p1; // valid
            int * restrict q2 = q1; // valid
            p1 = q2;
            // undefined behavior
            p2 = q2;
            // undefined behavior
        }
    }
    It might be worth referring to the standard.
    Last edited by JohnGraham; 07-20-2013 at 12:15 PM.
    Programming and other random guff: cat /dev/thoughts > blogspot.com (previously prognix.blogspot.com)

    ~~~

    "The largest-scale pattern in the history of Unix is this: when and where Unix has adhered most closely to open-source practices, it has prospered. Attempts to proprietarize it have invariably resulted in stagnation and decline."

    Eric Raymond, The Art of Unix Programming

  8. #8
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    John if you mean the text from point 3. It comes from a book and I wanted an example for this. Your example that you gave from the standard it seems equivalent because p1 is local and p2 is defined inside a block nested within the function body. So we can have a legal alias for p1. I think that is the situation where you can create valid alias from a restricted pointer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 43
    Last Post: 05-23-2013, 03:01 PM
  2. Replies: 0
    Last Post: 02-16-2012, 02:00 PM
  3. Restricted Integer
    By kolucoms6 in forum C++ Programming
    Replies: 6
    Last Post: 03-01-2008, 08:59 PM
  4. Forum Restricted
    By XelleX90 in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-26-2006, 08:54 AM
  5. How restricted am I by console mode
    By The Gweech in forum C++ Programming
    Replies: 2
    Last Post: 03-20-2002, 09:25 PM