c/c++面试12-18------关与sizeof那些事儿
12 使用sizeof计算普通变量所占空间大小
(1)不同数据类型所占字节数不同(32位 64位系统不同)
int----->4
double----->8
char------->1
(2)代码
#include<stdio.h>
#include <stdlib.h>
void Func(char str[])
{
printf("sizeof(str)=%d\n", sizeof(str));
}
int main()
{
char str[] = "hello";
char *p = str;
int n = ;
printf("sizeof(str)=%d\n", sizeof(str));//
printf("sizeof(p)=%d\n", sizeof(p));//
printf("sizeof(n)=%d\n", sizeof(n)); //
void *pp = malloc();
printf("sizeof(pp)=%d\n", sizeof(pp));//
Func(str);
getchar();
}
注意:
(1)数组作为实际参数传给函数的时候,当作指针来看待,不是绝对的。
(2)sizeof不是函数 是一个运算符
13 使用sizeof计算类对象所占空间大小
(1)代码
#include<stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
class A
{
public:
int i;
};
class B
{
public:
char ch;
};
class C
{
public:
int i;
short j;
}; class D
{
public:
int i;
short j;
char ch;
};
class E
{
public:
int i;
int ii;
short j;
char ch;
char chr;
}; class F
{
public:
int i;
int ii;
int iii;
short j;
char ch;
char chr;
}; int main()
{
cout << "sizeof(int)=" << sizeof(int) << endl;
cout << "sizeof(short)=" << sizeof(short) << endl; cout << "sizeof(char)=" << sizeof(char) << endl; cout << "sizeof(A)=" << sizeof(A) << endl; cout << "sizeof(B)=" << sizeof(B) << endl; cout << "sizeof(C)=" << sizeof(C) << endl; cout << "sizeof(D)=" << sizeof(D) << endl; cout << "sizeof(E)=" << sizeof(E) << endl;
cout << "sizeof(F)=" << sizeof(F) << endl; getchar();
return ;
}
(2)吃惊的代码结果
(3)分析分析
a:32位win操作系统 char-->1个字节 int---->4个字节 short----->2字节
sizeof(A)=sizeof(int)=4
sizeof(B)=sizeof(char)=1
sizeof(C)=sizeof(int)+sizeof(short)=4+2=6
sizeof(D)=sizeof(int)+sizeof(short)+sizeof(char)=7
sizeof(E)=2*sizeof(int)+2*sizeof(char)+sizeof(short)=12
sizeof(F)=3+sizeof(int)+2*sizeof(char)+sizeof(short)=15
------------>这和运行的结果有出入呀
(b)这就是字节对齐造成的,结论如下;
------------>结构体每个成员相当于结构体的首地址偏移量都是成员大小的整数倍
------------>结构体总大小为结构体最宽基本类型成员大小的整数倍
举个例子:
struct A
{
char c1;
int i;
char c2;
};--------------------->c1的偏移量是0,i的偏移量是4,那么c1和i之间就有3个字节的填充,再加上i的四个字节,所以c2的偏移量就是8,那么总共1+3+4+1=9;但是这个结构体最宽的基本类型是int,为4字节,要是它整数,那么就需要填充3字节 9+3=12,所以为12字节。也不知道清楚了没!!
15 使用sizeof计算含有虚函数类对象的空间大小
(1)代码
#include <iostream> using namespace std; class Base
{
public:
Base(int x) :a(x)
{ }
void print()
{
cout << "base" << endl;
}
private:
int a;
};
class Drived :public Base
{
public:
Drived(int x) :Base(x - ), b(x)
{ }
void print()
{
cout << "derived" << endl;
}
private:
int b;
};
class A
{
public:
A(int x) :a(x)
{ }
virtual void print()
{
cout << "A" << endl;
}
private:
int a;
};
class B :public A
{
public:
B(int x):A(x - ),b(x)
{ }
virtual void print()
{
cout << "b" << endl;
}
public:
int b;
}; int main()
{
Base obj1();
cout << "size of base obj is" << sizeof(obj1) << endl;
Drived obj2();
cout << "size of base obj is" << sizeof(obj2) << endl; A a();
cout << "size of A obj is" << sizeof(a) << endl;
B b();
cout << "size of B obj is" << sizeof(B) << endl;
getchar();
return ;
}
(2)解析
a;A是空类,编译器会安插一个char给空雷用来标记它的每一个对象
b:类D是虚继承A,编译器会为该类一个指向父类的指针,4字节
c:类E虚继承AB 8
16 sizeof和strlen哪些区别
(1)sizeof是操作符 strlen是函数
(2)sizeof可以用类型做参数,strlen参数必须是char*,而且必须以'\0'结尾
(3)在计算 字符串数组长度有区别
a:char str[20]="0123456789"
int a = strlen(str);//以ox00结束的字符串长度 10
int b = sizeof(str)//分配str[20]所占内存空间的大小 20
17 sizeof有哪些用途
(1) 与分配和IO系统那样的例程进行通信
void* malloc(size_t size)
size_t fread(void* ptr,size_t size,size_t nmemb,FILE* stream)
(2)动态分配对象 让系统知道分配多大的内存
(3)如果操作数是函数中的数组形参或者函数类型的形参,那么sizeof给出的是指针大小
18 使用sizeof计算联合体的大小
(1)注意
a:联合体的大小取决于它所有成员中占用空间大小最大的成员的大小
(2)代码
#include <iostream>
using namespace std;
union u
{
double a;
int b;
};
union u2
{
char a[];
int b;
};
union u3
{
char a[];
char b;
};
int main()
{
cout << sizeof(u) << endl;//
cout << sizeof(u2) << endl;//
cout << sizeof(u3) << endl;//
getchar();
}
(3)分析
a:对于u,大小最大为double 所以sizeof(u)=sizeof(double)=8
b:对于u2,最大是char buf[13],但是还有int,4字节,所以对齐方式变为4,所以占用空间变为最接近13的16
c:对于u3,最大的空间是char[13]数组,所以13
---------->其实对齐的方案是可以更改的,对于c++固有对齐取编译器对齐方式与自身大小较小的一个,看下面的例子
d(代码2):
#include <iostream>
#pragma pack(2) union u
{
char buf[];
int a;
};
int main()
{
cout<<sizeof(u)<<endl;//10 因为对齐从4变化为2 9最近且满足2倍数10
return ;
}
好了 加油加油!!!
最新文章
- String
- js实现页面局部弹窗打印
- C语言通用双向循环链表操作函数集
- css3旋转倾斜3d小动画
- Hibernate入门5持久化对象关系和批量处理技术
- CSS的sprite和单位
- 传智springMVC笔记
- STL六大组件之——分配器(内存分配,好深奥的东西)
- devexpress表格控件gridcontrol图片列,按钮列,时间列等特殊列的实现
- JavaScript(五)语句
- 配置安装nginx
- python学习之旅(三)
- 使用Github进行代码管理
- Nodejs--url模块
- jQuery UI API - 可拖拽小部件(Draggable Widget)(转)
- zookeeper_process内存泄露问题
- MongoDB简易
- 常州day1p5
- PHP Libxml函数
- grpc gateway 使用以及docker compose 集成