C++ 关于指针变量所占内存空间大小的问题




#include <iostream>
#include <stdlib.h>


using namespace std;

/**************************************

    C++ 关于指针变量所占内存空间大小的问题
    https://blog.csdn.net/cool_oyty/article/details/8078632
    C++ string、char*、char[],sizeof不是函数,更像宏
    https://blog.csdn.net/lanchunhui/article/details/50738498

***************************************/
int main()
{
    int* a = (int *)malloc(10);

    std::cout << sizeof(a) << endl;

    std::cout << sizeof((int *)malloc(10)) << endl;
    std::cout << sizeof((double *)malloc(10)) << endl;
    std::cout << sizeof((char *)malloc(10)) << endl;
    //答案都是4
    //即指针的内存大小只有CPU的位数有关,32位的都是4个字节,与指针类型无关
    //这个空间只是用来寻找和类型有关的内存
    
    
    string s = "";
    string ss = "Hello";
    char* css = "Hello";
    char ccss[] = "Hello";
    string sss = "Hlo";
    cout << sizeof (string) << endl; //4
    cout << sizeof (s) << endl;  //4
    cout << sizeof (ss) << endl;  //4
    cout << sizeof (css) << endl;  //4
    cout << sizeof (ccss) << endl;  //6
    cout << sizeof (sss) << endl;  //4
    cout << sizeof (new string()) << endl; //4
    //此时的sizeof里面传的其实是char *,'\0'也算一个字符
    //字符串中的"\0"问题  https://blog.csdn.net/jesse621/article/details/8033183
    cout << sizeof ("Hello") << endl; //6
    cout << sizeof ("He") << endl; //3
    cout << sizeof ("H") << endl;  //2

    int a = 0;

    cout<<sizeof(a=3)<<endl;

    cout<<a<<endl;
    return 0;

}

malloc,alloc,realloc之间的相似与区别

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值