Beginner C/C++

Moderators: None (Apply to moderate this forum)
Number of threads: 5432
Number of posts: 16953

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Inline Functions question Posted by red888 on 28 Dec 2009 at 1:16 PM
Learning about inline functions and very confused.

When I use an inline function am I not pushing another activation record onto the stack? If not then how does the inline function know how to return to the function that called it? And where is it exactly if its not on the stack? When the function is copied where is it copied to, RAM? I don't understand this.

If I declare a function inline and then call it multiple times in my program then I will have multiple copies of the function code in my program- I really have no idea what this means. Where does the compiler put all of these copies when it builds the program?

Thanks for any responses.
Report
Re: Inline Functions question Posted by AsmGuru62 on 29 Dec 2009 at 5:09 AM
Compiler simply inserts the code (copies it) into the place of a function call. A simple example:
int SUM (int a, int b)
{
	return a+b;
}
//
// Without INLINE
//
int main ()
{
	int n1,n2;

	printf ("ENTER TWO INTEGER VALUES: ");
	scanf ("%d %d", &n1, &n2);
	printf ("\nSUM OF THESE VALUES: %d", SUM (n1, n2));
	return 0;
}
//
// With INLINE (a simple replacement)
//
int main ()
{
	int n1,n2;

	printf ("ENTER TWO INTEGER VALUES: ");
	scanf ("%d %d", &n1, &n2);
	printf ("\nSUM OF THESE VALUES: %d", n1+n2);
	return 0;
}

Also, inline keyword works mostly in RELEASE build - compiler does it on RELEASE build and not all is inlined. Compiler measures the SIZE of functions and inlines only the functions, which are not large. Also, if a function is very SMALL (like SUM in my example) - you do not need to write inline for it - it will be inlined automatically, by size. All small functions are inlined this way, just by looking at the size of a function.
Report
Re: Inline Functions question Posted by red888 on 30 Dec 2009 at 12:43 PM
Hmm maybe my question comes from a deeper ignorance of how a program is compiled.

If I have:

inline int SUM (int a, int b)
{
	return a+b;
}

int main ()
{
	int n1,n2;

	printf ("ENTER TWO INTEGER VALUES: ");
	scanf ("%d %d", &n1, &n2);
	printf ("\nSUM OF THESE VALUES: %d", SUM (n1, n2));
	return 0;
}


The compiler takes "SUM (n1, n2)" and replaces it with "n1+n2"? How is that replacement made? And how does it make my program bigger?


Report
Re: Inline Functions question Posted by red888 on 30 Dec 2009 at 12:45 PM
Hmm maybe my question comes from a deeper ignorance of how a program is compiled.

If I have:

inline int SUM (int a, int b)
{
	return a+b;
}

int main ()
{
	int n1,n2;

	printf ("ENTER TWO INTEGER VALUES: ");
	scanf ("%d %d", &n1, &n2);
	printf ("\nSUM OF THESE VALUES: %d", SUM (n1, n2));
	return 0;
}


The compiler takes "SUM (n1, n2)" and replaces it with "n1+n2"? How is that replacement made? And how does it make my program bigger?


Report
Re: Inline Functions question Posted by AsmGuru62 on 31 Dec 2009 at 5:44 AM
The replacement is made when compiler links all together - only at this moment it can see the function size. Then the Assembly code of the function is placed instead of a function call. When code runs - it is already been replaced. The program gets larger in the case you are using a lot of calls to SUM (inlined) function. Just one call will not make it much larger - maybe a few bytes. But with inline functions you get faster code, because there is no CALL/RETURN codes. However, modern CPUs are very fast on first 16 levels of CALL/RET instructions and if code goes deeper in calls, then it will slow down, but not in very significant way - few CPU clocks (and they come 3.2 billion per second!).
Report
Re: Inline Functions question Posted by red888 on 31 Dec 2009 at 6:58 AM
Well that makes things a bit clearer, but it seems like I still have to study this a bit more myself to fully understand it.

Thanks.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.