Advertisement
alansam

Array index UB issue.

Feb 12th, 2022
1,380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.70 KB | None | 0 0
  1. $ cat indexudef.c
  2.  
  3. #include <stdio.h>
  4.  
  5. #define MAX 10
  6.  
  7. int main() {
  8.   int a[MAX];
  9.  
  10.   for (size_t i = 0; i < MAX; ++i) {
  11.     a[i] = i++;
  12.     printf("%3d", a[i]);
  13.   }
  14.   putchar('\n');
  15. }
  16.  
  17. $ gcc-11 -Wall -pedantic -std=c11 -o ../bin/indexudefG indexudef.c
  18. indexudef.c: In function 'main':
  19. indexudef.c:10:11: warning: operation on 'i' may be undefined [-Wsequence-point]
  20.    10 |   a[i] = i++;
  21.       |          ~^~
  22. $ ../bin/indexudefG
  23.   0  2  4  6  8
  24. $ clang -Wall -pedantic -std=c11 -o ../bin/indexudefC indexudef.c
  25. indexudef.c:10:11: warning: unsequenced modification and access to 'i' [-Wunsequenced]
  26.   a[i] = i++;
  27.     ~     ^
  28. 1 warning generated.
  29. $ ../bin/indexudefC
  30.   0  2  4  6  8
  31.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement