实验报告: 一、源代码: #include <stdio.h>class Counter { public: Counter() { s_counter++; printf(" - New one\r\n"); } ~Counter() { s_counter--; printf(" - Lost one\r\n"); } static void Print() { printf("Now counter is: %d\r\n", s_counter); }protected: static int s_counter; }; int Counter::s_counter = 0;int main() { int i = 6; while(i) { Counter cnt; cnt.Print(); i--; } }二、输出: - New one Now counter is: 1 - Lost one - New one Now counter is: 1 - Lost one - New one Now counter is: 1 - Lost one - New one Now counter is: 1 - Lost one - New one Now counter is: 1 - Lost one - New one Now counter is: 1 - Lost one三、while循环改为for循环,结果一样
我想到这个for(int i=0; i<n; i++)怎么不是每次都清零呢? 绕道走吧,管它呢。
VS2005就已经不允许在for语句内声明变量了
while(1)
{
char bufTemp[256];
_tcscat_s(bufTemp,256,TEXT("abc"));
}
是不是等价于
char bufTemp[256];
while(1)
{
_tcscat_s(bufTemp,256,TEXT("abc"));
}
呢?也就是说变量声明编译后会自动被扔到前面,但是写可以写在循环里面,有时可读性更好?话说俺也真是懒,貌似可以看反汇编之类的..还是先问问吧,麻烦大家下.
http://hi.baidu.com/05104106/blog/item/df3958cb52d93818bf09e6fe.html欢迎指正
在while{}外申明,出入while程序块不分配堆栈,当然在申明所属程序块结束时出栈。
清楚吗?
你可以试试声明类对象,在构造函数里,把类的静态int成员自加一次,循环N次后,看是不是构造N次。。
(当然,我也没试过……)
一、源代码:
#include <stdio.h>class Counter
{
public:
Counter()
{
s_counter++;
printf(" - New one\r\n");
}
~Counter()
{
s_counter--;
printf(" - Lost one\r\n");
} static void Print()
{
printf("Now counter is: %d\r\n", s_counter);
}protected:
static int s_counter;
};
int Counter::s_counter = 0;int main()
{
int i = 6;
while(i)
{
Counter cnt;
cnt.Print();
i--;
}
}二、输出:
- New one
Now counter is: 1
- Lost one
- New one
Now counter is: 1
- Lost one
- New one
Now counter is: 1
- Lost one
- New one
Now counter is: 1
- Lost one
- New one
Now counter is: 1
- Lost one
- New one
Now counter is: 1
- Lost one三、while循环改为for循环,结果一样
while会每次都调用
比如是一个int x放在while(1)中呢?
因为觉得有时放循环外,隔得太远,找起来不方便,看起代码来也麻烦..
以函数为基本单位,函数里所有的局部变量全部在函数开始时候ESP+offset分配,哪来的那么多声明
你程序的汇编代码,好好看看,我就纳闷,一些人在哪看的就说出来了
-----------------------------------------------------------------
.text:00401010 main proc near ; CODE XREF: _mainj
.text:00401010
.text:00401010 var_44 = byte ptr -44h
.text:00401010 var_4 = dword ptr -4
.text:00401010
.text:00401010 push ebp
.text:00401011 mov ebp, esp
.text:00401013 sub esp, 44h //这是给你的函数中所有的变量分配空间
.text:00401016 push ebx
.text:00401017 push esi
.text:00401018 push edi
.text:00401019 lea edi, [ebp+var_44]
.text:0040101C mov ecx, 11h
.text:00401021 mov eax, 0CCCCCCCCh //这是Debug版本的反汇编,为了调试进行的初始化
.text:00401026 rep stosd
.text:00401028
.text:00401028 loc_401028: ; CODE XREF: main+31j
.text:00401028 mov eax, 1
.text:0040102D test eax, eax //while(1)
.text:0040102F jz short loc_401043 //看看要不要退出循环
.text:00401031 mov [ebp+var_4], 0 { //ebp上第一个offset就是你的i,在函数开始分配的
.text:00401038 mov ecx, [ebp+var_4] //找找你的所谓的分配在哪的,你给我个编译器看看是不是在里面分配
.text:0040103B add ecx, 1 //i++
.text:0040103E mov [ebp+var_4], ecx
.text:00401041 jmp short loc_401028 //}//while(1)
.text:00401043 ; ---------------------------------------------------------------------------
.text:00401043
.text:00401043 loc_401043: ; CODE XREF: main+1Fj
.text:00401043 xor eax, eax
.text:00401045 pop edi
.text:00401046 pop esi
.text:00401047 pop ebx
.text:00401048 mov esp, ebp
.text:0040104A pop ebp
.text:0040104B retn
.text:0040104B main endp
WDK中根本不允许在函数代码已经开始的地方再声明变量
gcc中当成括号内声明的也就是你说的块