Like Tree4Likes
  • 2 Post By Elkvis
  • 1 Post By stahta01
  • 1 Post By oogabooga

restrict type qualified pointers

This is a discussion on restrict type qualified pointers within the C Programming forums, part of the General Programming Boards category; Hello all, I want to give a try to "restrict" type-qualifier. I have looked for examples on google and I ...

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

    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
    2,292
    compiler command line option "-std=c99"
    Code:
    namespace life
    {
        const bool change = true;
    }

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    93
    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
    2,292
    you don't use the restrict qualifier that way. it's only for use on function parameters.
    Code:
    namespace life
    {
        const bool change = true;
    }

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    93
    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
    2,292
    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;
    }
    Code:
    namespace life
    {
        const bool change = true;
    }

  7. #7
    Registered User
    Join Date
    Dec 2010
    Posts
    93
    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
    2,292
    this may be a case of undefined behavior. literally anything could happen with the variables involved.
    stahta01 and GokhanK like this.
    Code:
    namespace life
    {
        const bool change = true;
    }

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

    Tim S.
    GokhanK likes this.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  10. #10
    Registered User
    Join Date
    Dec 2010
    Posts
    93
    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
    2,292
    Quote Originally Posted by SirPrattlepod View Post
    (bold mine)

    Since when?
    since GCC complains if you try it in a function body.
    Code:
    namespace life
    {
        const bool change = true;
    }

  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;
    }
    stahta01 likes this.
    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
    2,292
    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.
    Code:
    namespace life
    {
        const bool change = true;
    }

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, 10:17 PM
  3. qualified name and unqualified name
    By George2 in forum C++ Programming
    Replies: 8
    Last Post: 03-07-2008, 07: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

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