基础备忘:结构体、指针所占的内存大小

结构体按最长的数据类型对齐,所占内存的大小为结构体的所占内存最大的成员数据类型的整数倍。而各种数据类型的指针,所占的内存是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");
}






  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值