彻底弄清几乎每次必考的sizeof关键字
来源:互联网 发布:windows商店app 编辑:程序博客网 时间:2024/05/20 09:49
下面看看几乎每次笔试都会碰到的sizeof关键字。
- #include<iostream>
- using namespace std;
- struct A
- {};
- struct B
- {
- char c;
- int i;
- };
- struct C
- {
- int i;
- char c;
- };
- struct D
- {
- char c1;
- char c2;
- int i;
- };
- struct E
- {
- int i;
- char c1;
- char c2;
- };
- struct F
- {
- char c1;
- int i;
- char c2;
- };
- union G
- {
- char c1;
- int i;
- char c2;
- double d;
- };
- class H
- {};
- class I
- {
- private:
- int i;
- };
- class J
- {
- public:
- virtual void display();
- };
- class K
- {
- private:
- int i;
- public:
- void display();
- };
- class L
- {
- private:
- int i;
- public:
- virtual void display();
- };
- class M
- {
- private:
- int i;
- int j;
- public:
- virtual void display();
- };
- class N
- {
- private:
- static int i;
- };
- class O
- {
- private:
- static int i;
- int j;
- };
- void fun1(char str[20])
- {
- cout << sizeof(str) << endl;
- }
- void fun2(char a[10][9])
- {
- cout << sizeof(a) << endl;
- }
- int main()
- {
- char *p1;
- cout << sizeof(p1) << endl; // 4
- int *p2 = new int[100];
- cout << sizeof(p2) << endl; // 4
- delete [] p2;
- float *p3;
- cout << sizeof(p3) << endl; // 4
- double ******p4;
- cout << sizeof(p4) << endl; // 4
- char str[100] = "abcdefg";
- fun1(str); // 4 (fun1中的形参str是指针变量)
- cout << sizeof(str) << endl; // 100 (str的容量为100)
- cout << sizeof(*str) << endl; // 1 (*str是一个字符数据)
- cout << strlen(str) << endl; // 7 (字符串str的长度)
- char str1[] = "abcdeofg"; //(里面是字符'o')
- cout << sizeof(str1) << endl; // 9
- cout << strlen(str1) << endl; // 8
- char str2[] = "abcde0fg"; //(里面是字符'0',不等于'\0')
- cout << sizeof(str2) << endl; // 9
- cout << strlen(str2) << endl; // 8
- char str3[] = "abcde\0fg";
- cout << sizeof(str3) << endl; // 9
- cout << strlen(str3) << endl; // 5
- char str4[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g'};
- cout << sizeof(str4) << endl; // 7
- cout << strlen(str4) << endl; // 13 (数值不确定)
- char str5[] = {'a', 'b', 'c', 'd', 'e', 'o', 'f', 'g'};
- cout << sizeof(str5) << endl; // 8
- cout << strlen(str5) << endl; // 21 (数值不确定)
- char str6[] = {'a', 'b', 'c', 'd', 'e', '0', 'f', 'g'};
- cout << sizeof(str6) << endl; // 8
- cout << strlen(str6) << endl; // 29 (数值不确定)
- char str7[] = {'a', 'b', 'c', 'd', 'e', '\0', 'f', 'g'};
- cout << sizeof(str7) << endl; // 8
- cout << strlen(str7) << endl; // 5
- char str8[] = {'a', 'b', 'c', 'd', 'e', 0, 'f', 'g'};
- cout << sizeof(str8) << endl; // 8
- cout << strlen(str8) << endl; // 5
- char str9[] = "";
- cout << sizeof(str9) << endl; // 1
- cout << strlen(str9) << endl; // 0
- char a[10][9];
- cout << sizeof(a) << endl; // 90
- fun2(a); // 4 (fun2中的a为指针变量)
- cout << sizeof(A) << endl; // 1 (编译器实现)
- cout << sizeof(B) << endl; // 8 (内存对齐)
- cout << sizeof(C) << endl; // 8 (内存对齐)
- cout << sizeof(D) << endl; // 8 (内存对齐)
- cout << sizeof(E) << endl; // 8 (内存对齐)
- cout << sizeof(F) << endl; // 12 (内存对齐)
- cout << sizeof(G) << endl; // 8 (内存共用)
- cout << sizeof(H) << endl; // 1 (编译器实现)
- cout << sizeof(I) << endl; // 4
- cout << sizeof(J) << endl; // 4 (虚指针)
- cout << sizeof(K) << endl; // 4
- cout << sizeof(L) << endl; // 8 (虚指针)
- cout << sizeof(M) << endl; // 12 (虚指针)
- cout << sizeof(N) << endl; // 1 (静态成员变量不专属某一对象)
- cout << sizeof(O) << endl; // 4 (静态成员变量不专属某一对象)
- return 0;
- }
- 彻底弄清几乎每次必考的sizeof关键字
- 彻底弄清几乎每次必考的sizeof关键字
- Base64原理彻底弄清
- 彻底弄清Jquery中html(), text(), val() 的区别.
- 让你彻底弄清offset
- 让你彻底弄清offset
- 让你彻底弄清offset
- 让你彻底弄清offset
- 让你彻底弄清offset
- 让你彻底弄清offset
- 让你彻底弄清offset
- 让你彻底弄清offset
- sizeof关键字的使用
- 没有括号的sizeof,sizeof为关键字
- 笔试必考-C语言之sizeof详解
- 行迁移和行链接概念及模拟(彻底弄清两者的区别)
- sizeof()和strlen()最彻底的阐释
- Sizeof ---最冤枉的关键字
- gcc与makefile使用的常见问题和入门实例/GCC使用详解
- js 表格
- ibatis复杂插入配置
- javascript的几种继承方法
- VC同时操作WORD和用ADO操作数据库时产生冲突的解决方法
- 彻底弄清几乎每次必考的sizeof关键字
- 在Ubuntu下编译wpa_supplicant
- 开发方法:深入理解敏捷开发的常见误区
- Java中的Vector,ArrayList,LinkedList
- 【软件工程系列】之第九章:用户界面设计
- spring 3.1.2+struts 2.3.2 + mybatis 3.1.1框架搭建(二)
- Box2d源码学习<六>动态树的实现
- memcache(使用telnet操作)
- 儒林外史