1

我正在使用带有 C99 标准的 mingw32-gcc。restrict我在下面的代码中粘贴了一些关于关键字的文章的编辑- http://wr.informatik.uni-hamburg.de/_media/teaching/wintersemester_2013_2014/epc-1314-fasselt-c-keywords-report.pdf。根据作者的说法,"Result One"应该"Result Two"是不同的,但是当我运行它时,它们是相同的。我没有收到任何编译器警告。有没有我遗漏的设置?

#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;
}
4

1 回答 1

1

关于使用restrict

来自 wikipedia.org:

如果不遵循意图声明并且通过独立指针访问对象,这将导致未定义的行为。

此行update_restrict(&a, &b, &a);导致未定义的行为

结果可能相同,也可能不同。

于 2015-04-28T05:22:06.580 回答