How to make some crt functions like memcpy, memcmp, strcmp etc inline?

In this blog post I’ll be talking about following #pragma compiler directives…

  • #pragma intrinsic
  • #pragma function

Ever wondered how to make function calls for memset, memcpy etc inline. It’s quite easy, you can use #pragma intrinsic directive to tell the compiler to inline calls to above functions. Check out this link for a list of native functions which can be made inline with this compiler directive.

An eg:

[sourcecode language=’cpp’]// These three functions are now inline
#pragma intrinsic( memset, memcpy, memcmp )

void Test()
// memset function is now inlined

Now how to undo intrinsic pragma effect? Use #pragma function directive. #pragma function directive does the opposite of #pragma intrinsic. Sometimes you may want that calls to functions specified in the pragma’s argument list be generated instead of being inlined. So in our above example if we want to prevent memset from being inlined just specify this using #pragma function directive. An example follows…

[sourcecode language=’cpp’]// These functions are no more inline
#pragma function( memset, memcpy )

void Test()
// memset is no more inlined

A bit more comprehensive example on #pragma intrinsic and function can be found in MSDN. #pragma function and intrinsic is particularly useful if we have a global compiler directive turned on/off which tells it to inline/not inline function calls. Also note that making a function call inline will make code generated bit big. Another caveat with inline functions is when fixing bugs and sending patches using dll’s, since the function is inlined, calls to this function cannot be updated, just in case if the bug is in this function. So use with care and have fun programming.

3 thoughts on “How to make some crt functions like memcpy, memcmp, strcmp etc inline?

  1. Thanks Sarath!

    Bumped into this directive sometime back while browsing through MFC source code for CMemFile. So thought of posting this here, just for information.

Appreciate your comments...