Object Layout in Memory

Object Layout in Memory

Preface

Teacher mentioned this in class, but I don’t understand at all about these stuff at that time.

So I read the Game Engine Architecture page 121 – 128, which helps me a lot. Here is the reading notes.

Rules

Put larger data type on the top in struct to fit the alignment. Add explicit padding to the end of the struct manually.

Alignment and Packing

When small data members are interspersed with larger members, it will leave “holes” in the layout.

inse

Many modern CPU can only read and write aligned block of data.Every data type has a natural alignment which must be respected in order to permit the CPU to read and write memory effectively.

Good to do

Re-arrange the members by putting largest member on the top.

Add explicit padding to the end of the struct manually, to make the wasted space visible and explicit.

e

It’s better to make the padding visible by manually add them to the end._pad[0] and _pad[1] will be add automatically by compiler in to struct anyway in order to guarantees all subsequent element in an array will be aligned properly.

Memory Layout of C++ Classes

Inheritance

v

When B inherits from class A, B’s data member simply appear immediately after A’s in memory.

Each new derived class tacks its data member on at the end.

Game programmer usually prefer to avoid multiple inheritance altogether.

Virtual functions

If a class contains or inherits one or more virtual functions, then four additional bytes are added to the class layout, typically at the very beginning of the class’ layout.

These four pointers are called the virtual table pointer or vpointer, because they contain a pointer to a data structure known as the virtual function table or vtable.

The vtable contains pointers to all the virtual functions that it declares or inherits. Every concrete class has its own virtual table, every instance of that class has a pointer to it, stored in its vpointer.

The heart of polymorphism

Example of a Circle class inherits the Shape class

Graph

graph

Code Example

 

class Shape
{
public:
   virtual void SetId(int id) { m_id = id; }
   int          GetId() const { return m_id;}
   virtual void Draw() = 0; //pure virtual - no impl.

privite:
   int m_id;
};

class Circle : public Shape
{
public:
   void     SetCenter (const Vector3& c) { m_center=c; }
   Vector3  GetCenter() const { return m_center; }
   void     SetRadius(float r) { m_radiuis = r; }
   float    GetRadius() const { return m_radius;}

   virtual void Draw()
   {
      // code to draw a circle
   }

private:
   vector3 m_center;
   float m_radius;
};

 

Jin Han

February 17, 2013

PCS Tree Summary

PCS Tree Summary

Preface

For the first assignment, I learn how to deal with the test. Actually I just took Data structure class last quarter, so everything here is not that unfamiliar with me. It just complex a lit bit from the stuff we learnt from data structure class. I haven’t spent much time on the assignment. But this is still a good recall of my understanding of data structure.

What is a PCS Tree?

Definition

Parent – Child –Sibling Tree

PCS Tree is a data structure used to store a hierarchical tree in form of a set of linked nodes. Each node in the tree can be a Parent, a Child or a Sibling of another node. Each parent node has one link to its first child only. The child nodes of a node are connected as a one-way linked list.

P for Parent

A pointer link to the parent

C for Child

A pointer link to the child

S for Sibling

A pointer link to siblings

Application

There are maybe various applications of PCS Tree. You may want to use it to store a directory structure inside an archive. Or it can be used to store data in a GUI control like QTreeView. In games, PCS Tree can be used to store a hierarchy of 3D objects. Each node will represent one object so that transformation applied to an object will affects all its child objects and grandchild objects.

Structures

PCSNode Graph:

1

PCSNode Method:

// constructor
PCSNode();
   
// copy constructor
PCSNode(const PCSNode &in );

// Specialize Constructor
PCSNode( PCSNode * const inParent, PCSNode * const inChild, 
         PCSNode * const inSibling, const char * const inName);
PCSNode( const char * const inName );


// destructor
~PCSNode();

// assignment operator
PCSNode &operator = (const PCSNode &in);

// accessors
void setParent( PCSNode * const in );
void setChild( PCSNode * const in );
void setSibling( PCSNode * const in );
PCSNode *getParent( void ) const;
PCSNode *getChild( void ) const;
PCSNode *getSibling( void ) const;

// name
PCSNodeReturnCode setName(const char * const inName );
PCSNodeReturnCode getName(char * const outBuffer, int sizeOutBuffer ) const;

// dump
void print() const;
void dumpChildren() const;
void dumpSiblings() const;
void dumpTree() const;

// get number of children/siblings
int getNumSiblings() const;
int getNumChildren() const;       
int getNumLevels() const;
int getNumNodes() const;
int getNumNodesRecursion() const;

// get level
int getLevel() const;

// remove
void removeAllNodes();
void removeAllNodesRecursion();

PCSTree Graph:

PCSTree

PCSTree Method:

// constructor
PCSTree();

// destructor
~PCSTree();

// get Root
PCSNode *getRoot( void ) const;

// insert
void insert(PCSNode * const inNode, PCSNode * const parent);

// remove
void remove(PCSNode * const inNode);

// get info
void getInfo( PCSTreeInfo &infoContainer );
void dumpTree( ) const;

 

Features

Every node has hierarchy.

Adding node is very fast.

Easy to use and implement.

Seeking and removing speed is OK.

Can get parents and children fast.

Good for storing objects.

 

Jin Han
February 16, 2013

Game Engine Study – Object System Summary

Object System Summary

Preface

Sorry that I was not able to create the blog in time, because I’m kind of stack on some problems at the beginning of this term. I move very slowly because I was still not that familiar with C++. It becomes much better after several weeks. Now I get some time to make up for my missing Blog. I decided to write a summary for each topic we talked in class as a review for the course. The first is the object system.

What is an Object?

Definition

Object is the container to holds data, it associates like object together.

It can hold different types of data. They generally don’t care what type of data to hold.

Object Services

Provide manipulation to the object system.

Creational: Insertion, Removing.

Searching/Retrieval: Walking the list, finding particular object.

Unique Identifiers

Help to identify which object is what.

String Names, performance is low.

Unique numbers, better choice, can be hash from a string name.

Ownership

Think about who should own the data before declaration.

Embedded system should know:

Where every bytes of data are all times.

Number of objects and types.

Shared memory pools. (What is available, used or free?)

Need to be centralized

Ownership in manager.

Group by functionality / types.

Resource managers.

Resource pools.

Object Relationships

Related by:

List, Arrays, Associations, Hierarchy.

Memory, performance usage needs to be finite and understood.

Serialization of objects

For Networking, Load/Save, Debugging.

Unique the type of data be stored.

Make the specific data types.

Cloning

Deep copies with complete sets
Semi static data sets with instance data that allows the data to be specialized

 

Jin Han
February 15, 2013

无限主张

最近寂寞的日子里,循环了将近2000遍的一首曲子

无限主张

蔚蓝色切线
坠落在云天
风穿透刺烈 扩散开
化作七色碎片
崩断下哽咽
模糊了语言
回忆中盘旋 闭上眼
还记得吗?
曾今曾有万份勇敢
全部忘掉吧!
未来的视线
也不曾再次地往返

为何为了过去前进
全部抛进天空吧!

只剩下一个人寂寞的歌唱

—-

彼岸花所在
散落的对白
漆黑的萤火 凝固成
何无有的色彩
呼吸下空气
等待着奇迹
闪烁的至今 还曾说
你记得吗?
一步一次光荣跃进
全部忘掉吧!
怯懦的血液
也不曾再次地流淌

不知不觉一往无前
全都穿透地心吧!

只留下一个人孤独的前进

—-

还记得所在苍白
我真的
勇敢吗?
可否能
全部遗忘
再诉说 你我
都忘掉吧
心意心愿新的确信
再次奔跑吧
神秘的远方
也不能再将我阻挡

迎面迎来疯狂幻境
全部送进宇宙吧!

只有我一个人永远的方向

很久没更新了, 总结一下最近干的傻逼事情

上次更新似乎是上学期期末考试后了。

后来放寒假,拉皮过来玩了2天,帮我搬了家!寂寞的搬去清静的屌丝区好好学习!
SONY DSC

SONY DSC

录了几个DS视频和星际视频 度过了末日 过程很Jio!

然后一起又去纽约那玩了一圈,住了下豪宅,看了911纪念馆和自由女神什么的。

SONY DSC

然后就开学了。。。。

然后换了新的女友!!!!!第一次自己装机,装了我一个周末!上图
SONY DSC

 

新室友翔哥和我女友
SONY DSC

 

新妹子特写

SONY DSC

 

机箱内部 各种灯饰 = =

SONY DSC

 

hohoho我的书桌

SONY DSC

然后就开学了。一直各种作业。

学习Game AI和 游戏引擎开发中。 挺难的,觉得自己基础比较差,一直没心情和机会写Blog。

后来去参加了一次Gloabal Game Jam, 遇到几个坑爹队友,然后3天没睡做了一个坑爹游戏,然后,就没有然后了。

。。。

数日后,收到邮件一封,恭喜你获得了一个Leftover Prize…..过季老游戏一个….

SONY DSC

 

然后midterm了!!

Game AI的一个midterm项目是,做一个足球AI 然后同学们一起比!!

AI

结果我们队的AI一路怒草到冠军决赛!

然后对手开了挂!!!!引发了各种bug!!!进了我们3球!!!

最后以游戏卡死结束,标准结局!

于是,我觉得我们赢了…

好坑!!

My GAM 374 Final Project

Gam 374 final project

BulletStorm

Press Z to open fire.
Hold space to slow down.
Push enter to pause the game.

Use game99.exe with 99 life. =。=

终于忙完了!!!目测本学期要4A。
这个虽然没啥创意很坑爹 但是某种意义上还是挺好玩的。。。

Download Here

最后,我们会一起去海边 We Will Go To The Beach Together At The End

当我感到迷惘/失望/看到希望
那里有海浪
那里有我的梦想
听海浪
拍打在岩石上
所有的过往
淡没在沙滩上

当我们踏着海浪
微笑着谈论死亡
明天是怎样
我们再也不愿去想
永远年轻
永远热泪盈眶
最后的乐章
让我们一起歌唱

∞小时编程音乐会

记得不知是哪天开始的,日夜不分了,写代码不停,直到现在,刚把一个作业做完。

1小时的休息时间,马上去上课,然后会收到下一个作业。

觉得∞这个标记真太适合VisualStudio了。

A programmer’s prospective

我网上老问他编程问题的那个家伙,总觉得他很厉害什么都懂。

问:你这么牛,不月薪2w吗?
答:有p用,这个项目没完成
问:什么项目?
答:dreamxx2(名字已加密)

而dreamxx2是他做的一个游戏外挂,他想做出来虐人。

程序员的想法总是那么严谨的无厘头。
或许独立而优雅的完成一个工程对他的意义已经超过了这个工程的实际意义了和经济意义了。