码迷,mamicode.com
首页 > 编程语言 > 详细

c++中堆、栈内存分配

时间:2015-02-02 19:26:08      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:

http://blog.sina.com.cn/s/blog_75b0e2ad01013afr.html

http://blog.csdn.net/qingtingchen1987/article/details/7698415

http://blog.chinaunix.net/uid-11959329-id-2797040.html

http://www.cnblogs.com/daocaoren/archive/2011/06/29/2092957.html

 

 

 

【参见】http://blog.csdn.net/wudaijun/article/details/8135205
#include<iostream> using namespace std; int main() { char p[] ="123456"; // char s[10]; // 正常复制: 123456 -- 123456 char s[4]; // 栈溢出(目标栈空间不够大), output: 56 -- 123456 char *ptr = p + 3; strcpy(s, p); cout<< p << " -- " << s << " --- " << ptr << endl; return 0; } // 栈 内存分配方式 (地址:高(左)->低(右); 数据写入方向:低(右)->高(左)) // ‘/0‘ ‘6‘ ‘5‘ ‘4‘ ‘3‘ ‘2‘ ‘1‘(p) ‘‘ ‘‘ ‘‘ ‘‘(s) // char p[] ="123456";//char s[4]; // ‘/0‘ ‘6‘ ‘5‘ ‘4‘(ptr) ‘/0‘ ‘6‘ ‘5‘(p) ‘4‘ ‘3‘ ‘2‘ ‘1‘(s) //strcpy(s, p); // output : 56 -- 123456 --- 456 // 解析:在这里我们可以知道p=s+4; 然后我们对s进行写入"123456" s所在的四个字节不够用 所以"56"(包括后面的/0)均被写入了p地址 因此输出p将输出56

 

c++中堆、栈内存分配

标签:

原文地址:http://www.cnblogs.com/yyxt/p/4268304.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!