1

I'm using mingw32-gcc, with the C99 standard. I pasted below code with a few edits from an article about the restrict keyword - http://wr.informatik.uni-hamburg.de/_media/teaching/wintersemester_2013_2014/epc-1314-fasselt-c-keywords-report.pdf. According to the author, "Result One" and "Result Two" should be different, but when I run it, they're the same. I'm not getting any compiler warnings. Are there any settings that I'm missing?

#include <stdio.h>

void update(int* a, int* b, int* c)
{
    *a += *c;
    *b += *c;
}

void update_restrict(int* a, int* b, int* restrict c)
{
    printf("*c = %d\n",*c);
    *a += *c;
    printf("\n*c = %d - ",*c);
    printf("shouldn't this have stayed the same?\n\n");
    *b += *c;
}

int main()
{
    int a = 1, b = 2;

    update(&a, &b, &a);

    printf("Result One: a, b =  %d, %d\n", a, b);

    a = 1; b = 2; // reset values

    update_restrict(&a, &b, &a);
    printf("Result Two: a, b = %d, %d\n", a, b);
    getchar();
    return 0;
}
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Nathan Schmidt
  • 374
  • 1
  • 4
  • 16
  • 2
    `restrict` doesn't have to affect your output. It's only for compiler to do possible optimizations. The results may be the same, and may be different. – cshu Apr 28 '15 at 05:12
  • I thought that in `update_restrict` the value of *c would stay the same after *a is changed, since it only gets loaded once. I don't see how this can't effect the results. – Nathan Schmidt Apr 28 '15 at 05:15
  • 1
    You didn't say how you are compiling (compiler version, command-line flags, etc). Gcc has a weak interpretation of the meaning of restrict: marking a single variable restrict is essentially useless with gcc, it only assumes that 2 variables that are **both** restrict don't alias. Also, the compiler can very well inline the functions, which could make the issue disappear. Use `__attribute__((noinline,noclone))` on update_restrict to avoid that. – Marc Glisse Apr 28 '15 at 05:29
  • I'm using version 4.8.1, with -std=c99. – Nathan Schmidt Apr 28 '15 at 05:36
  • I added `__attribute__((noinline,noclone))`, but still got same results. I'll try some examples with multiple restricts. – Nathan Schmidt Apr 28 '15 at 05:44
  • Note that with gcc 4.8.2 I get the result that you're looking for when all three arguments to `update_restrict()` are marked with the `restrict` keyword. Note that as @Griddoor mentions, undefined behavior doesn't mean that the problem symptom must occur, only that it can occur. – Michael Burr Apr 28 '15 at 05:56
  • If you aren't using any optimization flag (like `-O3`), of course the compiler isn't optimizing... – Marc Glisse Apr 28 '15 at 12:07
  • forgot to to mention, I was using -03 also. – Nathan Schmidt Apr 28 '15 at 17:29

1 Answers1

1

About the usage of restrict

From wikipedia.org:

If the declaration of intent is not followed and the object is accessed by an independent pointer, this will result in undefined behavior.

This line update_restrict(&a, &b, &a); results in undefined behavior.

The results might be the same, and it might not.

cshu
  • 5,654
  • 28
  • 44