Function definitions that are qualified with the inline keyword act like macros, that is, the
code is inserted at each invocation of the function. However, unlike preprocessor macros,
inline functions are much safer. Copies of the parameters are still taken and type
checking of the parameters is performed. Compilers are at liberty to ignore an inline
instruction, and many will do so for functions that are too long or complicated (each
compiler will have its own idea about what this means!). Also, if the address of the function
is ever used (perhaps assigned to a pointer), then the compiler will have to generate a normal
function (it is difficult to take the address of a function that does not exist!), though this may
be in addition to the insertion of the code wherever the function is called.
inline int strlen(char *str)
{
int i = 0;
while (str++ != '\0')
i++;
return i;
}
code is inserted at each invocation of the function. However, unlike preprocessor macros,
inline functions are much safer. Copies of the parameters are still taken and type
checking of the parameters is performed. Compilers are at liberty to ignore an inline
instruction, and many will do so for functions that are too long or complicated (each
compiler will have its own idea about what this means!). Also, if the address of the function
is ever used (perhaps assigned to a pointer), then the compiler will have to generate a normal
function (it is difficult to take the address of a function that does not exist!), though this may
be in addition to the insertion of the code wherever the function is called.
inline int strlen(char *str)
{
int i = 0;
while (str++ != '\0')
i++;
return i;
}
No comments:
Post a Comment