Click here to Skip to main content
15,916,019 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: problem about RichEdit Pin
Naveen2-Jul-07 15:42
Naveen2-Jul-07 15:42 
GeneralRe: problem about RichEdit Pin
kcynic2-Jul-07 18:41
kcynic2-Jul-07 18:41 
QuestionKeywords that I don't understand Pin
Alex Cutovoi2-Jul-07 13:26
Alex Cutovoi2-Jul-07 13:26 
AnswerRe: Keywords that I don't understand Pin
Stephen Hewitt2-Jul-07 13:30
Stephen Hewitt2-Jul-07 13:30 
AnswerRe: Keywords that I don't understand [modified] Pin
Jeremy Falcon2-Jul-07 14:12
professionalJeremy Falcon2-Jul-07 14:12 
GeneralRe: Keywords that I don't understand Pin
Stephen Hewitt2-Jul-07 15:03
Stephen Hewitt2-Jul-07 15:03 
GeneralRe: Keywords that I don't understand Pin
Jeremy Falcon2-Jul-07 15:16
professionalJeremy Falcon2-Jul-07 15:16 
GeneralRe: Keywords that I don't understand Pin
Stephen Hewitt2-Jul-07 15:38
Stephen Hewitt2-Jul-07 15:38 
Jeremy Falcon wrote:
We both know the smaller the function the less the increase in executable, so my statement isn't incorrect. But the increase is still substantial relative to the routine.

Here's some code I used to measure:
// InlineSmaller.cpp : Defines the entry point for the console application.
//
 
#include "stdafx.h"
#include <iostream>
 
inline int Add(int l, int r)
{
	return l+r;
}
 
void PrintResult(int r)
{
	using namespace std;
	cout << r << endl;
}
 
int main(int argc, char* argv[])
{
	int res = Add(1, 2);
	PrintResult(res);
 
	return 0;
}


I compiled twice: once with the inline on the Add function and once without.

Here's the code with the inline:
18:   int main(int argc, char* argv[])
19:   {
004010F0 6A 03                push        3
004010F2 E8 89 FF FF FF       call        PrintResult (00401080)
004010F7 83 C4 04             add         esp,4
20:       int res = Add(1, 2);
21:       PrintResult(res);
22:
23:       return 0;
004010FA 33 C0                xor         eax,eax
24:   }
004010FC C3                   ret


As you can see the Add call has been entirely optimised away to a push 3!

Here's the code with the inline commented out:
7:    /*inline*/ int Add(int l, int r)
8:    {
00401080 8B 44 24 08          mov         eax,dword ptr [esp+8]
00401084 8B 4C 24 04          mov         ecx,dword ptr [esp+4]
00401088 03 C1                add         eax,ecx
9:        return l+r;
10:   }
0040108A C3                   ret
 
18:   int main(int argc, char* argv[])
19:   {
00401100 6A 02                push        2
00401102 6A 01                push        1
00401104 E8 77 FF FF FF       call        Add (00401080)
20:       int res = Add(1, 2);
21:       PrintResult(res);
00401109 50                   push        eax
0040110A E8 81 FF FF FF       call        PrintResult (00401090)
0040110F 83 C4 0C             add         esp,0Ch
22:
23:       return 0;
00401112 33 C0                xor         eax,eax
24:   }
00401114 C3                   ret


We have an Add function and main is bigger! My statement is correct: using inline on small functions can and often does make the code smaller (and faster)! We saved 19 bytes of code by adding the inline!

Jeremy Falcon wrote:
Nevertheless, what you just described is really better served as a macro, considering the restrictions on inline functions and compiler issues associated with them.

Macros make debugging harder and are not type safe. Never use a macro when an inline function can be used instead!


Steve

GeneralRe: Keywords that I don't understand Pin
Jeremy Falcon2-Jul-07 16:00
professionalJeremy Falcon2-Jul-07 16:00 
GeneralRe: Keywords that I don't understand Pin
Stephen Hewitt2-Jul-07 16:15
Stephen Hewitt2-Jul-07 16:15 
GeneralRe: Keywords that I don't understand Pin
Jeremy Falcon2-Jul-07 16:24
professionalJeremy Falcon2-Jul-07 16:24 
GeneralRe: Keywords that I don't understand Pin
Jeremy Falcon2-Jul-07 16:08
professionalJeremy Falcon2-Jul-07 16:08 
GeneralRe: Keywords that I don't understand Pin
Stephen Hewitt2-Jul-07 16:22
Stephen Hewitt2-Jul-07 16:22 
GeneralRe: Keywords that I don't understand Pin
Jeremy Falcon2-Jul-07 16:25
professionalJeremy Falcon2-Jul-07 16:25 
GeneralRe: Keywords that I don't understand Pin
User 5838522-Jul-07 16:44
User 5838522-Jul-07 16:44 
GeneralRe: Keywords that I don't understand Pin
Jeremy Falcon2-Jul-07 16:48
professionalJeremy Falcon2-Jul-07 16:48 
GeneralRe: Keywords that I don't understand Pin
User 5838522-Jul-07 16:57
User 5838522-Jul-07 16:57 
GeneralRe: Keywords that I don't understand Pin
Jeremy Falcon2-Jul-07 17:05
professionalJeremy Falcon2-Jul-07 17:05 
GeneralRe: Keywords that I don't understand Pin
User 5838522-Jul-07 17:48
User 5838522-Jul-07 17:48 
GeneralRe: Keywords that I don't understand Pin
Jeremy Falcon2-Jul-07 17:57
professionalJeremy Falcon2-Jul-07 17:57 
GeneralRe: Keywords that I don't understand Pin
User 5838522-Jul-07 18:02
User 5838522-Jul-07 18:02 
GeneralRe: Keywords that I don't understand Pin
Stephen Hewitt2-Jul-07 17:24
Stephen Hewitt2-Jul-07 17:24 
GeneralRe: Keywords that I don't understand Pin
Jeremy Falcon2-Jul-07 17:33
professionalJeremy Falcon2-Jul-07 17:33 
GeneralRe: Keywords that I don't understand Pin
Stephen Hewitt2-Jul-07 17:48
Stephen Hewitt2-Jul-07 17:48 
GeneralRe: Keywords that I don't understand Pin
Jeremy Falcon2-Jul-07 17:54
professionalJeremy Falcon2-Jul-07 17:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.