The different between #define and const for a constant in C
2012-01-18 16:54
441 查看
这应该算一个经常争论的话题,而且往往没有个结果。
虽然他们在使用中感觉“差不多”,但是他们在实现上有什么区别?
请看下面的代码:
3: #define T 13 /*用#define定义的一个常量T*/
4:
5: int main()
6: {
00401000 push ebp
00401001 mov ebp,esp
00401003 sub esp,4Ch
00401006 push ebx
00401007 push esi
00401008 push edi
00401009 lea edi,[ebp-4Ch]
0040100C mov ecx,13h
00401011 mov eax,0CCCCCCCCh
00401016 rep stos dword ptr [edi] /*以上均是初始化(这个词不太准确,在我没有想到更好的之前先用他吧^_^)操作,不管他。*/
7:
8: const int t=13;/*用const定义的一个常量*/
00401018 mov dword ptr [ebp-4],0Dh /*const常量t在堆栈中分配了一块空间保存其值0xD=13*/
9:
10: int n=t;/*把t赋值给n*/
0040101C movsx eax,dword ptr [ebp-4] /*把储存在堆栈中的const常量t取出,传递到eax*/
00401020 mov dword ptr [ebp-8],eax /*把eax的值赋给变量t([edp-8]是t的空间的起始地址,又是4 dword,VC就是粗犷啊:P)*/
11: int m=T;/*把T赋值给m*/
00401023 mov dword ptr [ebp-0Ch],0Dh /*把0xD(13)赋值到变量m([edp-0Ch]是他的起始空间)*/
[以上代码来自VC++ 6.0]
从上面这段代码可以看出,const与#define最大的差别在于:前者在堆栈分配了空间,而后者只是把具体数值直接传递到目标变量罢了。或者说,const的常量是一个Run-Time的概念,他在程序中确确实实的存在可以被调用、传递。而#define常量则是一个Compile-Time概念,它的生命周期止于编译期:在实际程序中他只是一个常数、一个命令中的参数,没有实际的存在。
const常量存在于程序的数据段,#define常量存在于程序的代码段。
至于两者的优缺点,要看具体的情况了。一般的常数应用,我个人认为#define是一个更好的选择:
i.从run-time的角度来看,他在空间上和时间上都有很好优势。
ii.从compile-time的角度来看,类似m=t*10的代码不会被编译器优化,t*10的操作需要在run-time执行。而#define的常量会被合并(在上例中T*10将被0x82取代)。
但是:如果你需要粗鲁的修改常数的值,那就的使用const了,因为后者在程序中没有实际的存在。(其实应该说修改数据段比代码段要简单^_^)。
http://kaby.blogdriver.com/kaby/171539.html
虽然他们在使用中感觉“差不多”,但是他们在实现上有什么区别?
请看下面的代码:
3: #define T 13 /*用#define定义的一个常量T*/
4:
5: int main()
6: {
00401000 push ebp
00401001 mov ebp,esp
00401003 sub esp,4Ch
00401006 push ebx
00401007 push esi
00401008 push edi
00401009 lea edi,[ebp-4Ch]
0040100C mov ecx,13h
00401011 mov eax,0CCCCCCCCh
00401016 rep stos dword ptr [edi] /*以上均是初始化(这个词不太准确,在我没有想到更好的之前先用他吧^_^)操作,不管他。*/
7:
8: const int t=13;/*用const定义的一个常量*/
00401018 mov dword ptr [ebp-4],0Dh /*const常量t在堆栈中分配了一块空间保存其值0xD=13*/
9:
10: int n=t;/*把t赋值给n*/
0040101C movsx eax,dword ptr [ebp-4] /*把储存在堆栈中的const常量t取出,传递到eax*/
00401020 mov dword ptr [ebp-8],eax /*把eax的值赋给变量t([edp-8]是t的空间的起始地址,又是4 dword,VC就是粗犷啊:P)*/
11: int m=T;/*把T赋值给m*/
00401023 mov dword ptr [ebp-0Ch],0Dh /*把0xD(13)赋值到变量m([edp-0Ch]是他的起始空间)*/
[以上代码来自VC++ 6.0]
从上面这段代码可以看出,const与#define最大的差别在于:前者在堆栈分配了空间,而后者只是把具体数值直接传递到目标变量罢了。或者说,const的常量是一个Run-Time的概念,他在程序中确确实实的存在可以被调用、传递。而#define常量则是一个Compile-Time概念,它的生命周期止于编译期:在实际程序中他只是一个常数、一个命令中的参数,没有实际的存在。
const常量存在于程序的数据段,#define常量存在于程序的代码段。
至于两者的优缺点,要看具体的情况了。一般的常数应用,我个人认为#define是一个更好的选择:
i.从run-time的角度来看,他在空间上和时间上都有很好优势。
ii.从compile-time的角度来看,类似m=t*10的代码不会被编译器优化,t*10的操作需要在run-time执行。而#define的常量会被合并(在上例中T*10将被0x82取代)。
但是:如果你需要粗鲁的修改常数的值,那就的使用const了,因为后者在程序中没有实际的存在。(其实应该说修改数据段比代码段要简单^_^)。
http://kaby.blogdriver.com/kaby/171539.html
相关文章推荐
- what is the different between function and method in python
- the different between "in" and "exist "
- The differences between Define and Inline
- Programming Basic--what's the diff between const and #define?
- The different between Char in C#.Net and in C++.Net(MFC)
- the different between call and apply in javascript
- the different between System.exit(0) and System.exit(1)
- the different between crash recovery and media recovery
- The article discusses a couple of new features introduced for assemblies and versioning in Visual Studio 2005.
- The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified.
- [文章摘要]The parallel coordinate plot in action: design and use for geographic visualization
- Get the content of an Iframe in Javascript – crossbrowser solution for both IE and Firefox
- Cannot resolve the collation conflict between "SQL_Latin1_General_CP1_CI_AS" and "Chinese_PRC_CI_AS" in the equal to operation.
- OWIN support for the Web API 2 and MVC 5 integrations in Autofac
- What is the difference between @staticmethod and @classmethod in Python?
- What is the difference between a soft reference and a weak reference in Java?
- Linking different libraries for Debug and Release builds in Cmake on windows?
- How do I change the schedule for replication between two domain controllers in a site?
- 错误整理:No plugin found for prefix 'jetty' in the current project and in the plugin groups
- No plugin found for prefix ‘war’ in the current project and in the plugin groups