C ++中的对齐方式 [英] alignment in C++

查看:92
本文介绍了C ++中的对齐方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有:

16,32,64位机器的对齐规则是什么?

肯定会欣赏实用的全面参考?对于

示例,这些示例的布局是什么?

a)

代码:

struct

{

short int a;

int b;


};

b)

代码:

struct {

short int a;

int b;

int somefunc();

virtual int func1();

};

c)非常棘手(提示:它很棘手虚拟功能(或者我应该给
说vtable的指示......你应该知道关于vtable的一些内容在
最后但聪明的情况下))


代码:

struct {

short int a;

int b;

virtual int func_1 ();

virtual int func_2();





virtual int func_n();


};

最后,是否有一组算法特别是谜语类型,

(问题 - 比如查找积极和

负整数或竞赛类型中最大的子阵列...也哪里有一点点体面的算法分析...感谢一百万!!!!

解决方案

< blockquote> puzzlecracker写道:

16,32,64位机器的对齐规则是什么 - 肯定会赞赏一个脚踏实地的全面参考?对于
示例,这些示例的布局是什么?


对齐不是由语言规则定义的,除了简单的某些

架构可能需要填充对象的字节数。


如果您需要了解特定机器的对齐要求

体系结构,那么最好不要向编译器的作者询问

该体系结构。 IOW,它是实现定义的。

a)

代码:
struct
{
short int a;
int b;

};


所有空白的内容是什么?它会以某种方式增加你的

帖子的重要性吗?如果没有,请尝试使用最少的必要量。

b)
代码:
struct {
short int a;
int b;
int somefunc();
virtual int func1();


虚函数_usually_为每个对象添加一个[hidden]指针,

,无论函数的数量是多少。常规功能没有。


};

c)非常棘手(提示:虚拟功能很棘手(或者我应该指点) vtable ...


指向vtable的指针(单数)。

你应该知道关于vtable的内容,但最后但很聪明的情况) )

代码:
struct {
short int a;
int b;
virtual int func_1();
virtual int func_2( );


虚拟int func_n();

};

最后,是否有一组算法特别是一个谜语类型,(问题 - 例如找到一个正数和/或负整数数组中最大的子数组,或竞争类型)...也是对算法进行一些有点像样的分析地方...感谢一百万!!!!



www .google.com


V


thx,你能举例说明16 o 32位机器所以

我可以从那里延伸:让我们说短的int是2个字节int是

4.

我知道指针的数量与int相同,那么普通的

函数定义怎么样?


是的,带填充!

无法找到任何通用解释的指南...这是我在一次采访中觉得非常难以理解的部分...


再次感谢。


" puzzlecracker" < IR ********* @ gmail.com>在消息中写道

news:11 ********************** @ f14g2000cwb.googlegr oups.com ...

这就是:

16,32,64位机器的对齐规则是什么


这不是由C ++定义的语言,但是这些机器的架构是



- 肯定会赞赏一个脚踏实地,全面的参考?


您需要在

问题中找到每台机器的参考。即使是不同的机器也可以用相同的数字(例如32位)调用,但是b $ b可以有不同的对齐要求。


这是例如为什么C ++(和C)使用struct / class类型对象提供''padding''字节

。这有助于语言

保持平台中立。


对于
示例,这些示例的布局是什么?
a)

代码:
struct
{
short int a;
int b;


取决于实施和主机和操作系统。

};

b)
代码:
struct {
short int a;
int b;
int somefunc();
virtual int func1();

};


取决于实现和主机和操作系统。

c)非常棘手(提示:虚拟功能很棘手(或者我应该
)说vtable的指示......你应该知道关于vtable的最后一点但是聪明的情况))


C ++没有定义''vtable''。这只是一种可能的实现可以实现多态的方式,而不是

要求或禁止的。


代码:
struct {
short int a;
int b;
virtual int func_1();
virtual int func_2();


virtual int func_n();

};

最后,是否有一组算法特别是谜语类型,
(问题 - 如此至于找到一个正数和/或负整数数组中最大的子阵列,或竞赛类型)......还有一些对算法进行了一些不错的分析......感谢一百万!!!!




尝试comp.programming,也许数学小组可能会有所帮助。


-Mike


Here it goes:
What are the alignment rules for 16, 32, 64 bit machine - would
certainly appreciate a down-to-earth, comprehensive reference? For
example, what is the layout for these examples?
a)
Code:
struct
{
short int a;
int b;

};
b)
Code:
struct{
short int a;
int b;
int somefunc();
virtual int func1();
};
c) Very Tricky (Hint: it is tricky with virtual function (or should I
say pointers to vtable...you should know something about vtables in
last but clever case))

Code:
struct{
short int a;
int b;
virtual int func_1();
virtual int func_2();
:
:
virtual int func_n();

};
And lastly, is there a group for algorithms particularly a riddle type,
(problems - such as to find a largest subarray in array of positive and
negative integers, or contests type)... also where a somewhat decent
analysis of algorithms takes place... thanks a million!!!!

解决方案

puzzlecracker wrote:

What are the alignment rules for 16, 32, 64 bit machine - would
certainly appreciate a down-to-earth, comprehensive reference? For
example, what is the layout for these examples?
Alignment is not defined by the language rules beyond simple "certain
architectures may require padding bytes for objects".

If you need to know the alignment requirements for a particular machine
architecture, you''d be better off asking the authors of the compiler for
that architecture. IOW, it''s implementation-defined.
a)
Code:
struct
{
short int a;
int b;

};
What''s with all the whitespace? Does it add to the significance of your
post somehow? If not, try to use the minimal amount necessary.


b)
Code:
struct{
short int a;
int b;
int somefunc();
virtual int func1();
Virtual functions _usually_ add one [hidden] pointer to each object,
regardless of the number of functions. Regular functions do not.


};
c) Very Tricky (Hint: it is tricky with virtual function (or should I
say pointers to vtable...
"pointer to vtable" (singular).
you should know something about vtables in
last but clever case))

Code:
struct{
short int a;
int b;
virtual int func_1();
virtual int func_2();
:
:
virtual int func_n();

};
And lastly, is there a group for algorithms particularly a riddle type,
(problems - such as to find a largest subarray in array of positive and
negative integers, or contests type)... also where a somewhat decent
analysis of algorithms takes place... thanks a million!!!!



www.google.com

V


thx, can you illustrate an example for either 16 or 32 bit machine so
that I can extend from there on: let''s say short int is 2 bytes int is
4.
I know that pointers takes same amount as int, what about a regular
functions definition?

yes, with padding!

couldnt find any guide for a generic explanation... this is the part i
felt very uncomftable on one of my interviews...

many thanks again.


"puzzlecracker" <ir*********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...

Here it goes:
What are the alignment rules for 16, 32, 64 bit machine
That''s not defined by the C++ language, but by
the architectures of those machines.
- would
certainly appreciate a down-to-earth, comprehensive reference?
You''ll need to find a reference for each machine in
question. Even different machines which could all
be called by the same ''number'' (e.g. 32-bit) could
have different alignment requirements.

This is e.g. why C++ (and C) provide for ''padding'' bytes
withing struct/class type objects. This helps the languages
remain platform-neutral.

For
example, what is the layout for these examples?
a)
Code:
struct
{
short int a;
int b;
Depends upon the implementation and host machine and OS.
};
b)
Code:
struct{
short int a;
int b;
int somefunc();
virtual int func1();
};
Depends upon the implementation and host machine and OS.
c) Very Tricky (Hint: it is tricky with virtual function (or should I
say pointers to vtable...you should know something about vtables in
last but clever case))
C++ does not define ''vtable''. That''s just one possible
way an implementation could implement polymorphism, not
required or prohibited.


Code:
struct{
short int a;
int b;
virtual int func_1();
virtual int func_2();
:
:
virtual int func_n();

};
And lastly, is there a group for algorithms particularly a riddle type,
(problems - such as to find a largest subarray in array of positive and
negative integers, or contests type)... also where a somewhat decent
analysis of algorithms takes place... thanks a million!!!!



Try comp.programming, and perhaps a math group might help.

-Mike


这篇关于C ++中的对齐方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆