Thread: restrict type qualified pointers

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    113

    restrict type qualified pointers

    Hello all,

    I want to give a try to "restrict" type-qualifier. I have looked for examples on google and I see the format is used: "int * restrict p";

    However when I write this I get an error: "p is undeclared"

    I am using GNU GCC Compiler with Code::Blocks. How can I solve this problem. Is it possible me to adapt the compiler to C99 standarts, if I can, will it solve my problem?

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    compiler command line option "-std=c99"
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    113
    Thank you very much for your answer. I have looked for it a bit and I can use C99 standarts now. By the way I couldn't understand the restrict qualifier:

    Code:
    #include <stdio.h>
    int main(void){
    
    int a=5;
    int *restrict p=&a;
    int *q;
    q=p;
    printf("%d",*q);
    
    return 0;
    }
    I wrote this code. I assumed the pointer q cannot access the memory stored by p but it does. What I understood was that from my research but it seems I was wrong. Could you explain me the restrict qualifier?

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    you don't use the restrict qualifier that way. it's only for use on function parameters.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    113
    Could you explain me a bit how to use it?

    I did a try:

    Code:
    #include <stdio.h>
    
    int f(int *restrict x,int *restrict y, int *restrict z);
    
    int main(void){
    int a=5;
    int b=3;
    int *restrict p=&a;
    int *restrict q=&b;
    int *restrict r=&b;
    int result=f(p,q,r);
    printf("%d",result);
    return 0;
    }
    
    int f(int *restrict x,int *restrict y, int *restrict z){
    *y+=*z;
    *x+=*z;
    printf("%d\n",*x);
    printf("%d\n",*y);
    printf("%d\n",*z);
    return (*x)*(*y)*(*z);
    }
    I did a mistake intentionally. Although I declared p and q as restrict both point to the same object.

    I assumed the compiler will assume z was unchanged -because I declared it as restrict- and I will get a wrong result. However it works perfectly. So I don't understand how the compiler can be optimised with restrict if it doesn't assume it is changed?

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    you cannot use restrict in the body of a function. it is only for function parameters.

    this is ok:

    Code:
    int f(int *restrict x,int *restrict y, int *restrict z);
    this is not:

    Code:
    int main(void){
      int a=5;
      int b=3;
      int *restrict p=&a;
      int *restrict q=&b;
      int *restrict r=&b;
      int result=f(p,q,r);
      printf("%d",result);
      return 0;
    }
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  7. #7
    Registered User
    Join Date
    Dec 2010
    Posts
    113
    Thank you very much for your correction. However the code below still doesn't do what I assume.

    Code:
    #include <stdio.h>
    
    int f(int *restrict x,int *restrict y, int *restrict z);
    
    int main(void){
    int a=5;
    int b=3;
    int *p=&a;
    int *q=&b;
    int *r=&b;
    int result=f(p,q,r);
    printf("%d",result);
    return 0;
    }
    
    int f(int *restrict x,int *restrict y, int *restrict z){
    *y+=*z;
    *x+=*z;
    printf("%d\n",*x);
    printf("%d\n",*y);
    printf("%d\n",*z);
    return (*x)*(*y)*(*z);
    }
    According to the wikipedia article, I think I should get a wrong result because the value of "z" will be loaded once.

    restrict - Wikipedia, the free encyclopedia

    However, it doesn't seem like this.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    this may be a case of undefined behavior. literally anything could happen with the variables involved.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    When you engage in undefined behavior the results is NOT defined!!

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  10. #10
    Registered User
    Join Date
    Dec 2010
    Posts
    113
    The last two answers really have explained to me. So I can say I shouldn't trust the code above to be always true.

    Thank you very much for your answers.

  11. #11
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by Elkvis View Post
    you cannot use restrict in the body of a function. it is only for function parameters.
    (bold mine)

    Since when?

  12. #12
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by SirPrattlepod View Post
    (bold mine)

    Since when?
    since GCC complains if you try it in a function body.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  13. #13
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by Elkvis View Post
    since GCC complains if you try it in a function body.
    This works:
    Code:
    // gcc -std=c99 -Wall -Wextra restrict.c
    
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
       int * restrict p = malloc(sizeof(int));
       *p = 42;
       printf("%d\n", *p);
       free(p);
       return 0;
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  14. #14
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    it's possible that I forgot to add -std=c99 to my compiler command. I'll have to try it again when I have access to a C compiler again on monday.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Qualified typedef
    By DL1 in forum C++ Programming
    Replies: 5
    Last Post: 07-17-2009, 04:46 AM
  2. What is needed to be qualified as a game designer
    By kenryuakuma in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 12-17-2008, 11:17 PM
  3. qualified name and unqualified name
    By George2 in forum C++ Programming
    Replies: 8
    Last Post: 03-07-2008, 08:41 AM
  4. Replies: 4
    Last Post: 06-12-2007, 09:55 AM
  5. restrict and function pointers
    By Laserve in forum C Programming
    Replies: 13
    Last Post: 06-27-2004, 01:42 AM