C语言趣事之内存泄露

来源:互联网 发布:那些年的经典网络歌曲 编辑:程序博客网 时间:2024/05/22 07:02
C语言定义变量,分配内存时采用的是栈模式,于是乎,就有了如此如此,这般这般的效果
#include<iostream>using namespace std;int main(){int a = 66666;int b = 22222;int c[2] = {333333,44444};cout << " 修改前的值 " << endl;cout << " c[2]= " << c[2] << endl;cout << " c[3]= " << c[3] << endl;c[3] = 12345; //修改c[3]的值 cout << "修改后的值:" <<endl;cout << " c[3]= "<< c[3] << endl;cout << "  a= "<< a << endl; return 0;} 

同理,我们可以利用指针这种东西,如下

#include<iostream>using namespace std;int main(){int a = 0;int *p = &a;for(int i=0;i<10;i++){cout << p << " : ";cout << *p++ << endl;}return 0;}


原创粉丝点击