문제

"Advanced Programming in the UNIX Environment, 3rd Edition", page 151:

The difference between getc and fgetc is that getc can be implemented as a macro, whereas fgetc cannot be implemented as a macro. This means three things:

  • The argument to getc should not be an expression with side effects.
  • Since fgetc is guaranteed to be a function, we can take its address. This allows us to pass the address of fgetc as an argument to another function.
  • Calls to fgetc probably take longer than calls to getc, as it usually takes more time to call a function.

What "expression with side effects" can occur for the function signatures with stream pointer as a parameter?

#include<stdio.h>
int getc(FILE* stream);
int fgetc(FILE* stream);
도움이 되었습니까?

해결책

There are probably hundreds of ways to pass an expression with side effects, but a "credible" one would be something like:

FILE *files[NUM_FILES];
...
int rc = getc(files[counter++]);

If getc is implemented poorly as a macro, the expression files[counter++] could be evaluated more than once, leading to unexpected behavior.

다른 팁

As an example, don't write

FILE* foo() { puts( "Bah!\n" ); return stdout; }

void advance() { getc( foo() ); }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top