基础备忘:结构体、指针所占的内存大小
2012-08-26 22:00
344 查看
结构体按最长的数据类型对齐,所占内存的大小为结构体的所占内存最大的成员数据类型的整数倍。而各种数据类型的指针,所占的内存是4字节。示例如下:
#include<iostream>
using namespace std;
typedef struct t
{
bool a;
int b;
bool c;
struct t*next;
};
struct t1
{
bool a;
short b;
bool c;
};
struct t2
{
bool a;
char b;
bool c;
};
int main()
{
cout << "sizeof(t) "<<sizeof(t) << endl <<"sizeof(t1) "<< sizeof(t1) << endl <<"sizeof(t2) "<< sizeof(t2) << endl;
cout<<"int "<<sizeof(int)<<endl<<"bool "<<sizeof(bool)<<endl<<"short "<<sizeof(short)<<endl<<"char "<<sizeof(char)<<endl;
struct t *tttt;
cout<<"tttt "<<sizeof(tttt)<<endl;
char **a[3][4];
cout<<"sizeof(a) "<<sizeof((a))<<" sizeof(&a)"<<sizeof (&a)<<endl;//&a大小是4
double *p;
cout<<"sizeof(*double) "<<sizeof(p)<<endl;//大小是4
int *q;
cout<<"sizeof(*int) "<<sizeof(q)<<endl; //大小是4
char *s;
cout<<"sizeof(*char) "<<sizeof(s)<<endl;
char b[10];
cout<<"sizeof(b) "<<sizeof(b)<<endl;
char *c[10];
cout<<"sizeof(c) "<<sizeof(c)<<" sizeof(*c) "<<sizeof(*c)<<" sizeof(&c) "<<sizeof(&c)<<endl;
int x;
int *r=&x;
int **rr=&r;
cout<<"sizeof(rr) "<<sizeof(rr)<<endl;//大小是4 ,rr是二级指针,是指向指针的指针,本质是还是指针。
system("pause");
}
#include<iostream>
using namespace std;
typedef struct t
{
bool a;
int b;
bool c;
struct t*next;
};
struct t1
{
bool a;
short b;
bool c;
};
struct t2
{
bool a;
char b;
bool c;
};
int main()
{
cout << "sizeof(t) "<<sizeof(t) << endl <<"sizeof(t1) "<< sizeof(t1) << endl <<"sizeof(t2) "<< sizeof(t2) << endl;
cout<<"int "<<sizeof(int)<<endl<<"bool "<<sizeof(bool)<<endl<<"short "<<sizeof(short)<<endl<<"char "<<sizeof(char)<<endl;
struct t *tttt;
cout<<"tttt "<<sizeof(tttt)<<endl;
char **a[3][4];
cout<<"sizeof(a) "<<sizeof((a))<<" sizeof(&a)"<<sizeof (&a)<<endl;//&a大小是4
double *p;
cout<<"sizeof(*double) "<<sizeof(p)<<endl;//大小是4
int *q;
cout<<"sizeof(*int) "<<sizeof(q)<<endl; //大小是4
char *s;
cout<<"sizeof(*char) "<<sizeof(s)<<endl;
char b[10];
cout<<"sizeof(b) "<<sizeof(b)<<endl;
char *c[10];
cout<<"sizeof(c) "<<sizeof(c)<<" sizeof(*c) "<<sizeof(*c)<<" sizeof(&c) "<<sizeof(&c)<<endl;
int x;
int *r=&x;
int **rr=&r;
cout<<"sizeof(rr) "<<sizeof(rr)<<endl;//大小是4 ,rr是二级指针,是指向指针的指针,本质是还是指针。
system("pause");
}
相关文章推荐
- 【基础备忘】结构体、指针所占的内存大小
- 你必须知道的指针基础-6.内存的初始化及结构体的使用
- 指针组合(如指针数组)的区分及其结构体内存大小的计算
- [C++][基础概念](一)结构体所占内存大小的判断
- 结构体在内存中所占空间大小的计算
- 基础备忘之c++内存分布:代码段、全局数据区、栈、堆
- 使用list保存数据内存指针,在LIST大小大于5000后,push_back效率很低
- C++ 内存对齐 (计算结构体的大小)
- Delphi 的内存操作函数(3): 给结构体指针分配内存
- 黑马程序员——C语言基础——结构体以及结构体指针实战
- 结构体所占内存大小
- 从内存角度深入看结构体(window/linux)大小
- 指针(内存大小和数据类型无关,和系统操作位数有关),对象,地址,和系统操作位数的关系
- 基础备忘之c++内存分布:代码段、全局数据区、栈、堆
- 结构体内存大小总结
- struct结构体占内存大小计算
- 内存字节对齐---对结构体非常重要。不能用memcpy简单的移动指针进行赋值
- C语言之struct大小、首地址与内存对齐—由结构体成员地址得到结构体首地址
- 结构体中的一维,二维指针和内存的释放
- c语言—指针基础(7:静态分配内存)