Thread: __restrict compiler flag?

  1. #1
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656

    __restrict compiler flag?

    I thought the __restrict flag meant that the pointer couldn't be changed...but it can it seems......can somebody tell me clearly what the __restrict flag does?

    Code:
    #include <stdio.h>
    #include <al/etl.h>
    #include <sfl.h>
     
    char *blar( char *__restrict__ sz )
    {
    	char *s = malloc( NAME_MAX );
    	ASSERT( s );
    	strncpy( s, "Whoa whoa whoa", NAME_MAX );
    	sz = s;
    	return sz;
    }
    
    int main( void )
    {
    	char *__restrict hello = "How are you?";
    	hello = blar( hello );
    	puts( hello );
    	free( hello );
    	return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    restrict means that the pointer is the only way to access the object to which it points.

    C: A Reference Manual, 5th Edition
    Page 94, 4.4.6 Restrict

    The type qualifier restrict is new in C99. It may only be used to qualify pointers to
    objects or incomplete types, and it serves as a "no alias" hint to the C compiler. This means
    that that pointer is, for the moment, the only way to access the object to which it points. Vi-
    olating this assumption results in undefined behavior. The phrase "at the moment" means
    that in some circumstances within a function or block aliases can be created from the orig-
    inal restrict-qualified pointer as long as those aliases are elminated by the end of the func-
    tion or block. The C99 standard provides a precise mathematical definition of
    restrict, but here are some common situations.

    ...
    There are a bunch of examples, but I didn't feel like typing them all up. It doesn't mean the pointer can't be changed. It means that this pointer is the only way to access the current object.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    I'm not talking about the restrict type in C99, I'm talking about the __restrict for compilers like gcc. And I can still edit the contents of the pointers =[
    Code:
    #include <stdio.h>
    #include <al/etl.h>
    #include <sfl.h>
    
    void changeContents( char *__restrict sz )
    {
    	char *s = sz;
    	*s++ = 'S';
    	*(sz+1) = 'h';
    	*++s = 'i';
    	*(sz+3) = 't';
    	*(s+2) = ' ';
    }
    
    int main( void )
    {
    	char *__restrict hello = malloc( NAME_MAX );
    	
    	strncpy( hello, "Hello ma'am!", NAME_MAX );
    	changeContents( hello );
    	
    	puts( hello );
    	free( hello );
    	
    	return EXIT_SUCCESS;
    }
    Code:
    ........  ma'am!
    The compiler call seems so pointless
    Last edited by Kleid-0; 05-26-2005 at 06:31 PM.

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    How about reading next time m'kay?
    The type qualifier
    __restrict isn't a type, its a type qualifier just like const

    It doesn't mean you can't change the object, its just a way of telling the compiler that it should be the only thing pointing to that object. But of course theres an exception
    The phrase "at the moment" means
    that in some circumstances within a function or block aliases can be created from the orig-
    inal restrict-qualified pointer as long as those aliases are elminated by the end of the func-
    tion or block.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I know full well what you're talking about, and I already answered you. Howver, don't just take my word for it... Ta-dah! As stated, restrict aka __restrict doesn't have anything to do with making a pointer unmodifyable. That would be const.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Quote Originally Posted by quzah
    __restrict doesn't have anything to do with making a pointer unmodifyable. That would be const.
    Alright Quzah I may have misunderstood you. Ok I know what const does, I probably didn't mean what I said, I hope.

    Ok so what I meant is that __restrict says that a different pointer cannot access the contents of the __restrict/restricted pointer.

    The gcc documentation does not make any sense, because it doesn't give any good examples. I'm an example kind of guy. The docs doesn't seem to explain correctly for me because I cannot express what they're saying in a good example to show myself what __restrict/restrict really does.

    Could somebody give me an example of using __restrict?

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    EXCELLENT link Dave, thank you =]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  2. Compiler Paths...
    By Cobra in forum C++ Programming
    Replies: 5
    Last Post: 09-26-2006, 04:04 AM
  3. C Compiler and stuff
    By pal1ndr0me in forum C Programming
    Replies: 10
    Last Post: 07-21-2006, 11:07 AM
  4. I can't get this new compiler to work.
    By Loduwijk in forum C++ Programming
    Replies: 7
    Last Post: 03-29-2006, 06:42 AM
  5. how to call a compiler?
    By castlelight in forum C Programming
    Replies: 3
    Last Post: 11-22-2005, 11:28 AM