Effective C++第三版中文版(候捷譯)第二條“盡量以const, enum, inline 替換#define”中講到 此外雖然優(yōu)秀的編譯器不會為“整數(shù)型const對象”設(shè)定另外的內(nèi)存空間(除非你創(chuàng)建一個pointer或reference指向該對象),不夠優(yōu)秀的編譯器卻可能如此,而這可能是你不想要的。Enum和#defeine一樣絕不會導(dǎo)致非必要的內(nèi)存分配。 于是驗證了一下,環(huán)境是Fedora9,gcc4.3。 代碼一: #include <stdio.h> int NotAConst = 10; const int MonthsInYear = 12; const int AverDaysInMonth = 30; const float PI = 3.1415; int main() { const int *p; p = &AverDaysInMonth; printf("%d, %d, %f/n", MonthsInYear, AverDaysInMonth, PI); printf("address of AverDaysInMonth is %d", p); printf("%d %d %f/n", MonthsInYear, AverDaysInMonth, PI); return 0; }
編譯命令:gcc -c constVar.c ,然后使用objdump查看生成的二進(jìn)制文件,objdump -s constVar.o ,顯示如下:
constVar.o: file format elf64-x86-64
Contents of section .text: 0000 554889e5 4883ec10 48c745f8 00000000 UH..H...H.E..... 0010 f30f1005 00000000 0f14c00f 5ac08b15 ............Z... 0020 00000000 8b350000 0000bf00 000000b8 .....5.......... 0030 01000000 e8000000 00488b75 f8bf0000 .........H.u.... 0040 0000b800 000000e8 00000000 f30f1005 ................ 0050 00000000 0f14c00f 5ac08b15 00000000 ........Z....... 0060 8b350000 0000bf00 000000b8 01000000 .5.............. 0070 e8000000 00b80000 0000c9c3 ............ Contents of section .data: 0000 0a000000 .... //全局初始化變量int NotAConst = 10 Contents of section .rodata: 0000 0c000000 1e000000 560e4940 25642c20 ........V.I@%d, 0010 25642c20 25660a00 61646472 65737320 %d, %f..address 0020 6f662041 76657244 61797349 6e4d6f6e of AverDaysInMon 0030 74682069 73202564 00256420 25642025 th is %d.%d %d % 0040 660a00 f.. Contents of section .eh_frame: 0000 14000000 00000000 017a5200 01781001 .........zR..x.. 0010 030c0708 90010000 1c000000 1c000000 ................ 0020 00000000 7c000000 00410e10 8602430d ....|....A....C. 0030 06000000 00000000 ........ Contents of section .comment: 0000 00474343 3a202847 4e552920 342e332e .GCC: (GNU) 4.3. 0010 30203230 30383034 32382028 52656420 0 20080428 (Red 0020 48617420 342e332e 302d3829 00 Hat 4.3.0-8).
上面0c000000, 1e000000, 560e4940分別是程序中定義的 MonthsInYear, AverDaysInMonth和PI。因此可以看出gcc給const變量都非配了內(nèi)存空間,不管是否有pointer或者reference指向該變量。因此,以Scott Meyers的標(biāo)準(zhǔn)看,gcc還不是優(yōu)秀的編譯器。
修改一下代碼,加入enum: #include <stdio.h>
int NotAConst = 10; const int MonthsInYear = 12; const int AverDaysInMonth = 30; const float PI = 3.1415;
enum { Num1 = 1, Num2, Num3};
int main() { const int *p; p = &AverDaysInMonth;
printf("%d, %d, %f/n", MonthsInYear, AverDaysInMonth, PI); printf("address of AverDaysInMonth is %d", p); printf("%d %d %d/n", Num1, Num2, Num3);
return 0; }
同樣編譯并顯示二進(jìn)制文件的內(nèi)容為:
constVar.o: file format elf64-x86-64
Contents of section .text: 0000 554889e5 4883ec10 48c745f8 00000000 UH..H...H.E..... 0010 f30f1005 00000000 0f14c00f 5ac08b15 ............Z... 0020 00000000 8b350000 0000bf00 000000b8 .....5.......... 0030 01000000 e8000000 00488b75 f8bf0000 .........H.u.... 0040 0000b800 000000e8 00000000 b9030000 ................ 0050 00ba0200 0000be01 000000bf 00000000 ................ 0060 b8000000 00e80000 0000b800 000000c9 ................ 0070 c3 . Contents of section .data: 0000 0a000000 .... Contents of section .rodata: 0000 0c000000 1e000000 560e4940 25642c20 ........V.I@%d, 0010 25642c20 25660a00 61646472 65737320 %d, %f..address 0020 6f662041 76657244 61797349 6e4d6f6e of AverDaysInMon 0030 74682069 73202564 00256420 25642025 th is %d.%d %d % 0040 640a00 d.. Contents of section .eh_frame: 0000 14000000 00000000 017a5200 01781001 .........zR..x.. 0010 030c0708 90010000 1c000000 1c000000 ................ 0020 00000000 71000000 00410e10 8602430d ....q....A....C. 0030 06000000 00000000 ........ Contents of section .comment: 0000 00474343 3a202847 4e552920 342e332e .GCC: (GNU) 4.3. 0010 30203230 30383034 32382028 52656420 0 20080428 (Red 0020 48617420 342e332e 302d3829 00 Hat 4.3.0-8).
可以看見,加入enum確實沒有新分配內(nèi)存空間。 |
|
來自: 幸福的樂土 > 《計算機(jī)語言》