restrict and arrays

This is a discussion on restrict and arrays within the C Programming forums, part of the General Programming Boards category; int a[3], * restrict ip = &a[1]; *ip = 1; does a; invoke undefined behavior?...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129

    restrict and arrays

    int a[3], * restrict ip = &a[1]; *ip = 1; does a; invoke undefined behavior?

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    No. There is no undefined behavior. Maybe there might be a compile error.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    why do you think that?

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Not sure what the question is? Clarify the question and and the code snippet.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Does it violate the guarantees of restrict? Is a; considered an access that conflicts with the modification of a[1] through a restrict qualified pointer?

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by robwhit View Post
    Does it violate the guarantees of restrict? Is a; considered an access that conflicts with the modification of a[1] through a restrict qualified pointer?
    Nope! as long as it is compiled in c99 mode.

    [Edit] If you don't know how to compile in c99 mode then replace restrict with __restrict__
    Last edited by itCbitC; 06-02-2010 at 05:38 PM.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    how do you know?

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,659
    The way I see it, a restrict pointer means that only that pointer will be used to access the object(s) which it points to. In this case the object is the integer at a[1]. So if a[1] is accessed while ip is in scope, then behavior is undefined. Same thing goes for a[2] if *(ip+1) is accessed. You can access a[0] during the lifetime of ip, as long as you don't access *(ip-1).

    gg

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    yes, I agree. but can you do 'a;'? note the difference between that and 'a[0];'. does 'a;' count as an access to a[0], a[1], and a[2]?

  10. #10
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,659
    a is just an address (and the compiler can already track it's accesses). It would need to be dereferenced to be an "integer object" access.

    gg

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    but before it's an address, it's an array object, correct? only then would it undergo array to pointer conversion.

    let me put it another way...

    struct S { int i; char c; };
    S s = { 0 }, * restrict sp = &s; sp->i = 1;

    would s; invoke undefined behavior? sure, it wouldn't need the values of any of its members in that statement, but what about this statement?

    S s2 = s;

    surely that would be an access of s, and violating sp's restrict qualifier, right? if "S s2 = s;" invokes undefined behavior, why wouldn't "s;"? why wouldn't 'a;' invoke undefined behavior if "s;" invokes undefined behavior?
    Last edited by robwhit; 06-02-2010 at 10:20 PM. Reason: ;

  12. #12
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,659
    >> would s; invoke undefined behavior?
    I would say no. Because the compiler is not obligated to generate any real "access" (load or store) of "s".

    The ip/sp examples aren't exactly the same:
    The object that "sp" points to is "s".
    The object that "ip" points to is "a[1]".

    Access to the object "a[1]" is restricted only through "ip". So a, *a, *(a+2) are all ok and defined while ip is in scope.

    Access to the object "s" is restricted only through "sp", but the expression "s;" doesn't constitute an access.

    >> S s2 = s;
    >> surely that would be an access of s
    Yep.

    gg

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    ah yes, I meant

    struct S { int i; char c; };
    S s = { 0 };
    char * restrict cp = &s.c; *cp = 1;

    your reasoning seems sane. thanks.

Popular pages Recent additions subscribe to a feed

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