Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1107351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:55:29+00:00 2026-05-17T01:55:29+00:00

I’m a little confused about the rules regarding restricted pointers. Maybe someone out there

  • 0

I’m a little confused about the rules regarding restricted pointers. Maybe someone out there can help me out.

  1. Is it legal to define nested restricted pointers as follows:

    int* restrict a;
    int* restrict b;
    
    
    a = malloc(sizeof(int));
    
    
    // b = a; <-- assignment here is illegal, needs to happen in child block
    // *b = rand();
    
    
    while(1)
    {
        b = a;  // Is this legal?  Assuming 'b' is not modified outside the while() block
        *b = rand();
    }
    
  2. Is it legal to derive a restricted pointer value as follows:

    int* restrict c;
    int* restrict d;
    
    
    c = malloc(sizeof(int*)*101);
    d = c;
    
    
    for(int i = 0; i < 100; i++)
    {
        *d = i;
        d++;
    }
    
    
    c = d; // c is now set to the 101 element, is this legal assuming d isn't accessed?
    *c = rand();
    

Thanks!
Andrew

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T01:55:30+00:00Added an answer on May 17, 2026 at 1:55 am

    For reference, here’s the restrict qualifier’s rather convoluted definition (from C99 6.7.3.1 “Formal definition of restrict”):

    Let D be a declaration of an ordinary
    identifier that provides a means of
    designating an object P as a
    restrict-qualified pointer to type T.

    If D appears inside a block and
    does not have storage class
    extern, let B denote the block. If D
    appears in the list of parameter
    declarations of a function
    definition, let B denote the
    associated block. Otherwise, let B
    denote the block of main (or the block
    of whatever function is called at
    program startup in a freestanding
    environment).

    In what follows, a pointer
    expression E is said to be based on
    object P if (at some sequence point
    in the execution of B prior to the
    evaluation of E) modifying P to point
    to a copy of the array object into
    which it formerly pointed would change
    the value of E. Note that “based” is
    defined only for expressions with
    pointer types.

    During each execution of B, let L be
    any lvalue that has &L based on P. If
    L is used to access the value of the
    object X that it designates, and X is
    also modified (by any means), then the
    following requirements apply: T shall
    not be const-qualified. Every other
    lvalue used to access the value of X
    shall also have its address based on
    P. Every access that modifies X shall
    be considered also to modify P, for
    the purposes of this subclause. If P
    is assigned the value of a pointer
    expression E that is based on another
    restricted pointer object P2,
    associated with block B2, then either
    the execution of B2 shall begin before
    the execution of B, or the
    execution of B2 shall end prior to
    the assignment. If these
    requirements are not met, then the
    behavior is undefined.

    Here an execution of B means that
    portion of the execution of the
    program that would correspond to the
    lifetime of an object with scalar type
    and automatic storage duration
    associated with B.

    My reading of the above means that in your first question, a cannot be assigned to b, even inside a “child” block – the result is undefined. Such an assignment could be made if b were declared in that ‘sub-block’, but since b is declared at the same scope as a, the assignment cannot be made.

    For question 2, the assignments between c and d also result in undefined behavior (in both cases).

    The relevant bit from the standard (for both questions) is:

    If P is assigned the value of a
    pointer expression E that is based on
    another restricted pointer object P2,
    associated with block B2, then either
    the execution of B2 shall begin before
    the execution of B, or the
    execution of B2 shall end prior to
    the assignment.

    Since the restricted pointers are associated with the same block, it’s not possible for block B2 to begin before the execution of B, or for B2 to end prior to the assignment (since B and B2 are the same block).

    The standard gives an example that makes this pretty clear (I think – the clarity of the restrict definition’s 4 short paragraphs is on par with C++’s name resolution rules):

    EXAMPLE 4:

    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.

    {
        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
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know there's a lot of other questions out there that deal with this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I don't have much knowledge about the IPv6 protocol, so sorry if the question
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.