Message388740
For me, one of the my worst issue is when a macro evalutes an argument twice.
Example:
---
#include <stdio.h>
#define DOUBLE(value) ((value) + (value))
int main()
{
int x = 1;
// expanded to: ((++x) + (++x))
int y = DOUBLE(++x);
printf("x=%i y=%i\n", x, y);
return 0;
}
---
Surprising output:
---
$ gcc x.c -o x; ./x
x=3 y=6
---
Expected output:
---
x=2 y=4
---
Fixed code using a static inline function:
---
#include <stdio.h>
static inline int DOUBLE(int value) {
return value + value;
}
int main()
{
int x = 1;
// expanded to: DOUBLE(++x)
int y = DOUBLE(++x);
printf("x=%i y=%i\n", x, y);
return 0;
}
---
Output:
---
$ gcc x.c -o x; ./x
x=2 y=4
--- |
|
Date |
User |
Action |
Args |
2021-03-15 14:12:30 | vstinner | set | recipients:
+ vstinner, corona10, erlendaasland |
2021-03-15 14:12:30 | vstinner | set | messageid: <1615817550.47.0.575241129661.issue43502@roundup.psfhosted.org> |
2021-03-15 14:12:30 | vstinner | link | issue43502 messages |
2021-03-15 14:12:29 | vstinner | create | |
|