当前位置:   article > 正文

C++ 关于指针变量所占内存空间大小的问题_整型指针在内存中的大小

整型指针在内存中的大小
  1. #include <iostream>
  2. #include <stdlib.h>
  3. using namespace std;
  4. /**************************************
  5. C++ 关于指针变量所占内存空间大小的问题
  6. https://blog.csdn.net/cool_oyty/article/details/8078632
  7. C++ string、char*、char[],sizeof不是函数,更像宏
  8. https://blog.csdn.net/lanchunhui/article/details/50738498
  9. ***************************************/
  10. int main()
  11. {
  12. int* a = (int *)malloc(10);
  13. std::cout << sizeof(a) << endl;
  14. std::cout << sizeof((int *)malloc(10)) << endl;
  15. std::cout << sizeof((double *)malloc(10)) << endl;
  16. std::cout << sizeof((char *)malloc(10)) << endl;
  17. //答案都是4
  18. //即指针的内存大小只有CPU的位数有关,32位的都是4个字节,与指针类型无关
  19. //这个空间只是用来寻找和类型有关的内存
  20. string s = "";
  21. string ss = "Hello";
  22. char* css = "Hello";
  23. char ccss[] = "Hello";
  24. string sss = "Hlo";
  25. cout << sizeof (string) << endl; //4
  26. cout << sizeof (s) << endl; //4
  27. cout << sizeof (ss) << endl; //4
  28. cout << sizeof (css) << endl; //4
  29. cout << sizeof (ccss) << endl; //6
  30. cout << sizeof (sss) << endl; //4
  31. cout << sizeof (new string()) << endl; //4
  32. //此时的sizeof里面传的其实是char *'\0'也算一个字符
  33. //字符串中的"\0"问题 https://blog.csdn.net/jesse621/article/details/8033183
  34. cout << sizeof ("Hello") << endl; //6
  35. cout << sizeof ("He") << endl; //3
  36. cout << sizeof ("H") << endl; //2
  37. int a = 0;
  38. cout<<sizeof(a=3)<<endl;
  39. cout<<a<<endl;
  40. return 0;
  41. }

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/305900
推荐阅读
相关标签
  

闽ICP备14008679号