The Semantics of Data

第三章 前言 讨论 class的大小问题

#include"iostream"

using namespace std;

class X{};

class Y:public virtual X{};
class Z:public virtual X{};

class A:public Y,public Z{};

int main()
{
	cout<<"X:"<<sizeof(X)<<endl;
	cout<<"Y:"<<sizeof(Y)<<endl;
	cout<<"Z:"<<sizeof(Z)<<endl;
	cout<<"A:"<<sizeof(A)<<endl;
}

其继承关系为:


结果:

xiaobo@xiaobo-desktop:~/桌面/c++$ g++ model3.1.cpp -o model3.1
xiaobo@xiaobo-desktop:~/桌面/c++$ ./model3.1
X:1
Y:4
Z:4
A:8

以上结果不同的编译器会有不同,有的编译器会产生 1 ,8 , 8 , 12的结果

首先讨论第一种结果 VC和g++上的结果均为1,4,4,8,

X的大小为1,是因为编译器安插进去一个char,这样会使得这个class的两个object 在内存中的配置独一无二,

例如:

X a, b;
if(&a!=&b)cout << "different";

Y Z分别拥有一个指向virtual base class X subobject的指针 所以大小为4

A的大小为

内存布局如下图:


这类编译器把一个empty virtual base class视为derived class object的开头部分,不花费任何额外空间 

这也证实了一个virtual base class subobject只会在derived class中存在一份实体,不管它在继承体系中出现了多少次。

有一类编译器会得出 1,8,8,12的结果 主要是因为它给空的Y,Z,A均加上了char ,这样Y,Z,A 分别为5,5,9,又编译器有Alignment机制会将数值调整到整数倍(31位机为4bytes的整数倍)所以为8,8,12。


3.2 Data Member Layout


using namespace std;

class X{

	public:
	void getAddress() const {cout<<&x1<<endl;
				  cout<<&m<<endl;
				   cout<<&x3<<endl;}
        int  getChunk() const {return chunkSize;}
	private:
	int x1;  //
	static int m;
	static const int chunkSize = 10;
	double x3;
};

#include"iostream"
#include"stdio.h"

using namespace std;

class X{

	public:
		void setA(int x){x1 = x;}
		void setB(int x){ m = x ;}
		void getAddress() const {cout<<&x1<<endl;
				  cout<<&m<<endl;
				   cout<<&x3<<endl;}
		int  getChunk() const {return chunkSize;}
	private:
	int x1;
	static int m;
	static const int chunkSize = 10;
	double x3;
};

class Y:public virtual X{};
class Z:public virtual X{};

class A:public Y,public Z{};
int X::m = 1;
int main()
{
	//X::m = 3;
	cout<<"X:"<<sizeof(X)<<endl;
	cout<<"Y:"<<sizeof(Y)<<endl;
	cout<<"Z:"<<sizeof(Z)<<endl;
	cout<<"A:"<<sizeof(A)<<endl;
	X a,b;
	a.setA(10);
	a.setB(12);
	
	a.setB(12);
	if(&a!=&b)cout << "different"<<endl;
	a.getAddress();
	cout<<&a<<endl;
	printf("---%p----",&a);
}

X:12
Y:16
Z:16
A:20
different
0xbf9bbfc4
0x804a038
0xbf9bbfc8
0xbf9bbfc4
---0xbf9bbfc4----x






请按以下描述,自定义数据结构,实现一个circular bufferIn computer science, a circular buffer, circular queue, cyclic buffer or ring buffer is a data structure that uses a single,fixed-size buffer as if it were connected end-to-end. This structure lends itself easily to bufering data streams. There were earlycircular buffer implementations in hardware. A circular buffer first starts out empty and has a set length. In the diagram below is a 7-element buffeiAssume that 1 is written in the center of a circular buffer (the exact starting locatiorimportant in a circular buffer)8Then assume that two more elements are added to the circulal2buffers use FIFOlf two elements are removed, the two oldest values inside of the circulal(first in, first out) logic. n the example, 1 8 2 were the first to enter the cremoved,leaving 3 inside of the buffer.If the buffer has 7 elements, then it is completely full6 7 8 5 3 4 5A property of the circular buffer is that when it is full and a subsequent write is performed,overwriting the oldesdata. In the current example, two more elements - A & B - are added and theythe 3 & 475 Alternatively, the routines that manage the buffer could prevent overwriting the data and retur an error or raise an exceptionWhether or not data is overwritten is up to the semantics of the buffer routines or the application using the circular bufer.Finally, if two elements are now removed then what would be retured is not 3 & 4 but 5 8 6 because A & B overwrote the 3 &the 4 yielding the buffer with:
最新发布
05-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值