分为5种:
1.全局或者静态数据区
2.常量数据区
3.代码区
4.栈
5.堆
1.全局/静态数据区存储全局变量及静态变量;
2.常量数据区中存储程序中的常量及字符串等;
3.栈中存储自动变量或者局部变量,以及传递的函数参数,而堆是用户程序控制的存储区,存储动态产生的数据!
#include <cstdio>
#include <iostream>
using namespace std;
int main(int argc,char** argv)
{
char* s1="liyanefng";
s1[1]='a'; //修改了常量存储区中的数据!
//cout<<s1<<endl;
printf("string:%s\n",s1);
return 0;
}
//这段代码运行会出错。。。。。。
int * pNew =new int[20];
char* pChar=(char*)malloc(1);
//在堆上分配内存
//pNew和pChar保存在栈中 但是他们指向的内存是在堆中的
区别:
1.大小: 栈 小;堆 大
2.效率: 栈 快;堆 慢
#include <cstdio>
#include <iostream>
using namespace std;
class A{
public:
int num;
virtual void test(){}
};
class B{
public:
int num;
virtual void test(){}
virtual void test2(){}
};
class C{
public:
int num;
};
int main(int argc,char** argv)
{
cout<<"A: "<<sizeof(A)<<endl;
cout<<"B: "<<sizeof(B)<<endl;
cout<<"c: "<<sizeof(C)<<endl;
return 0;
}