c++ - Why do we say #define Processor creates multiple copies in program? -


i reading book effective c++, third edition there in item 2: prefer const, enum , inline #define 1 of reason given while using #define, pre-processor creates multiple copies of #define in our object code. didn't why preprocessor create multiple copies of #define? because know replaces ever finds macro.

also when created assembly code code, got same assembly code?

for example:

int main()                             {    const int magicnum 5;          int i=magicnum*1;    int j=magicnum*2;    int k=magicnum*3;    int l=magicnum*4;    int m=magicnum*5; }  

corresponding assembly code:

0004 c745e805       movl    $5, -24(%rbp)      000000 000b c745ec05       movl    $5, -20(%rbp)      000000 0012 c745f00a       movl    $10, -16(%rbp)      000000 0019 c745f40f       movl    $15, -12(%rbp)      000000 0020 c745f814       movl    $20, -8(%rbp)      000000 0027 c745fc19       movl    $25, -4(%rbp)      000000 

i got same same code while used #define. assembly code generated using assembly.ynh.io.

if use #define create named contant of primitive type int that's ok because preprocessor "know" int is, een in situation idea use const (or constexpr in c++11) variable compiler figure out type , further optimise code. things different if create class object in #define because if use in translation unit object constructed.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -