Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1753662
  • 博文数量: 335
  • 博客积分: 4690
  • 博客等级: 上校
  • 技术积分: 4341
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-08 21:38
个人简介

无聊之人--除了技术,还是技术,你懂得

文章分类

全部博文(335)

文章存档

2016年(29)

2015年(18)

2014年(7)

2013年(86)

2012年(90)

2011年(105)

分类: C/C++

2011-10-08 18:33:21

  1. #include <iostream>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #define OK 0
  5. using namespace std;
  6. void stackone();
  7. void stacktwo();
  8. int main(int argc ,char ** argv){
  9. cout<<"注意栈中变量的地址分配方式:变量按定义的顺序从高地址(栈底)像低地址(栈顶)分配"<<endl;
  10. cout<<"这个两个程序都是有问题的,一个隐式地址越界,一个显示地址越界"<<endl;
  11. stackone();
  12. stacktwo();
  13. system("pause");
  14. return OK;
  15. }
  16. void stackone(){
  17. char s[]="123456789";
  18. char d[]="123";
  19. strcpy(d,s);
  20. cout<<" the string d is\t"<<d<<endl;
  21. cout<<" the string s is\t"<<s<<endl;
  22. }
  23. void stacktwo(){
  24. char d[]="123";
  25. char s[]="123456789";
  26. strcpy(d,s);
  27. cout<<" the string d is\t"<<d<<endl;
  28. cout<<" the string s is\t"<<s<<endl;
  29. }

注意: 在给参数分配地址时,自由向左分配的,而变量是按定义的顺序
栈空间一般是从高地址向低地址扩展,至少在我的测试平台上,winxp
这两个程序的都是有问题的,主要原因是目的地址分配的空间太少,而导致地址越界
阅读(1076) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~