top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is use of #pragma inline directive in c language?

+3 votes
1,512 views
What is use of #pragma inline directive in c language?
posted Dec 4, 2013 by Deepak Raijada

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote

Padding is adding extra bits at the end, so that the structure completes the word boundary. Padding is done to do memory alignment. Using
-> #pragma ,
->__attrubute__((_packed_)) ,the strucutre padding can be avoided.

answer Dec 9, 2013 by Giri Prasad
0 votes

Lets first understand the use of #pragma:
'#pragma' is for compiler directives that are machine-specific or operating-system-specific, i.e. it tells the compiler to do something, set some option, take some action, override some default, etc. that may or may not apply to all machines and operating systems. Implementation vary from one compiler to the next and if some compiler does not support than it comes out without error/warning.

Now coming to second point, it is about the inline behavior -
The following preprocessor directives control function inlining:

#pragma  inline (id, ...) 
#pragma  noinline (id, ...)

Where id is a function identifier:

  • If a function is named in a #pragma inline directive, calls to that function are expanded as inline code, if possible.
  • If a function is named in a #pragma noinline directive, calls to that function are not expanded as inline code.
  • If a function is named in both a #pragma inline and a #pragma noinline directive, an error message is issued.

If a function is to be expanded inline, you must place the function definition in the same module as the function call. The definition can appear either before or after the function call.

The cc command options -O3, -O4, -inline size, -inline speed, or -inline all cause the compiler to attempt to expand calls to functions named in neither a #pragma inline nor a #pragma noinline directive as inline code whenever appropriate, as determined by the following function characteristics:

  • Size
  • Number of times the function is called
  • Conformance to the following restrictions:

    • The function does not take a parameter's address.
    • A field of a struct argument. An argument that is a pointer to a struct is not restricted.
    • The function does not use the varargs or stdarg package to access the function's arguments because they require arguments to be in adjacent memory locations, and inline expansion may violate that requirement.

For optimization level -O2, the C compiler inlines small static routines only.

The #pragma inline directive causes inline expansion regardless of the size or number of times the specified functions are called.

answer Dec 4, 2013 by Kumar Mitrasen
...