问题:1.sizeof;2.重载覆盖隐藏;3.内存管理

1-1、问:"char a[10]; int len = strlen(a);",len等于多少?

解析:strlen计算从数组a的第一个元素开始到'\0'总共有多少个字符(不包含'\0')。

由于字符数组没有初始化,数组元素的值是未知的,所以len的值也是未定的。

1-2


  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 
  5 void fun(char str[100])
  6 {
  7         printf("%d\n", sizeof(str));
  8         // Note: 数组作为函数形参,char str[100]等价于char *str
  9         // 此处为 指针的大小,结果为8字节 (64bits系统)
 10 }
 11 
 12 int main(void)
 13 {
 14         char str[] = "hello";
 15         char *p1 = str;
 16         int n = 10;
 17         char *p2= (char *)malloc(100);
 18 
 19         printf("%d\n", sizeof(str));
 20         printf("%d\n", strlen(str));
 21 
 22         printf("%d\n", sizeof(p1));     // Note: 指针变量的长度
 23         printf("%d\n", strlen(p1));
 24 
 25         printf("%d\n", sizeof(n));      // sizeof(int)
 26         printf("%d\n", sizeof(p2));
 27 
 28         fun(p2);
 29 
 30         printf("\n");
 31         return 0;
 32 }

1-3、结构体struct、共用体union 的sizeof

附:字节对齐

eg. struct A{ short s; int i; char c; };

       sizeof(A) = 12  

       = (2 + 2)+ 4 +(1 + 3)


eg. struct A{ char c; short s; int i; };

      sizeof(A) = 8

      = (1 + 2 + 1)+ 4


eg. union B{ double d; char c; }

       sizeof(B) = 8


eg. union B{double d; char c[13]; }

        sizeof(B) = 16;


2、

重载:函数名相同,参数不同

覆盖:基类的函数为虚函数(virtual),派生类的成员函数覆盖基类中的同名同参函数

隐藏:函数名相同--若 参数不同,无论基类中是否为虚函数,都将隐藏;若 参数相同,不为虚函数,则隐藏

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值