C - 分配中的内存大小 [英] size of memory in C -- allocation

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

问题描述

在面试记忆问题的采访中感到困惑...请

帮助 - 下面需要多少字节的内存才能占用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();

};

Got Confused on the interview with memory alligment questions... PLEASE
HELP -- How much bytes of memory will structs below take on 32 bit
machine? What about 64 bit machine? Why is it different? (if it''s
relevent, use standard size of datatypes)
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();
};

推荐答案

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

news:11 ********************** @ c13g2000cwb.googlegr oups.com ...
"puzzlecracker" <ir*********@gmail.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
对内存调优问题的采访感到困惑...请帮助 - 在32位
机器下面将占用多少字节的内存?
64位怎么样机?


这完全取决于编译器。语言

只要求内存足以存储所有成员的


为什么会有所不同?


因为机器不同(因此编译器为

他们会以不同的方式做事)。

(如果它是'
相关,使用标准大小的数据类型)


数据类型(字符类型除外)没有

a''标准大小'',只是''最小尺寸''。根据定义,角色

类型都有一个字节的大小。

a)

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

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

c)非常棘手(提示:它虚拟功能很棘手(或者我应该说vtable的指示......你应该知道最后但最聪明的情况下的vtables))
Got Confused on the interview with memory alligment questions... PLEASE
HELP -- How much bytes of memory will structs below take on 32 bit
machine?
What about 64 bit machine?
This depends entirely upon the compiler. The language
only requires that the memory is sufficient to store
all the members.
Why is it different?
Because machines are different (and thus compilers for
them will do things differently).
(if it''s
relevent, use standard size of datatypes)
Data types (except for the character types) don''t have
a ''standard size'', only a ''minimum size''. The character
types all have a size of one byte, by definition.
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))




C没有虚函数。 C ++没有。

但是,''vtable''不是C ++语言的一部分,

但是实现细节,不需要是
根本使用。


你是否知道C和C ++是两种独立的,不同的
语言,虽然它们有相似的语法,但

有显着差异吗?面试官

是否知道这个?


-Mike



C does not have virtual functions. C++ does.
However, ''vtables'' are not part of the C++ language,
but an implementation detail, not required to be
used at all.

Are you aware that C and C++ are two separate, distinct
languages, and that while they share similar syntax,
there are significant differences? Does the interviewer
know this?

-Mike


puzzlecracker写道:
puzzlecracker wrote:
虚拟功能




尝试新闻:comp.lang.c ++


-

pete



Try news:comp.lang.c++

--
pete


puzzlecracker写道:
puzzlecracker wrote:
在面试记忆问题的采访中感到困惑...请...
HELP - 32位
机器下面会占用多少字节的内存? 64位机器怎么样?它为什么不同? (如果它是相关的,请使用标准大小的数据类型)


[snip]

你自己不能这样做?

您是否阅读了类似帖子的回复?


a)

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


short int有多大?

在32位平台上,有些有16位短路,有些有

有32位短整数。


我们假设处理器只获取32位地址的

地址位置,即每4个字节位

常用符号。我们还假设一个短的int

是16位(2字节)。


为了让生活更轻松,我们很好地声明一个变量

以上结构并在地址0x00处分配。

让我们将值0x1616分配给''a''成员和

0x32323232到''b''成员。我们还将使用Big

Endian表示法。


地址值

------- --- -

0100 1616 - 会员''a''

0102 * 3232323232 - 会员''b''

0106 * 0000 - 内存中的未知值。


由于此处理器仅在4字节边界处获取,因此它将在地址0x100和0x104处获取。地址

的0x102无法直接访问,并且没有

对齐。 ''a''和'b''的实际值可能是

be:a == 0x16163232,b == 0x32320000


编译器是允许在成员之间添加填充

,以便将值放置在

处理器可以正确获取它们的位置:


0x100 1616 - 成员''a''

0x102 0000 - 来自编译器的填充。

0x104 32323232 - 成员''b''


上面的布局允许处理器在位置0x100处获取

值0x1616,在位于$ 010 $位置0x104处获取0x32323232。

64位机器的运行留给了

阅读器。提示:64位机器喜欢每8个字节取一个
;并假设一个短整数是

16或32位。


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


与上述类似的问题。但是,这次使用

指针而不是上面的函数。你可以

假设一个32位处理器有一个32位指针,

和一个64位处理器有一个64位指针。


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

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


virtual int func_n();
};
Got Confused on the interview with memory alligment questions... PLEASE
HELP -- How much bytes of memory will structs below take on 32 bit
machine? What about 64 bit machine? Why is it different? (if it''s
relevent, use standard size of datatypes)
[snip]
And you can''t do this yourself?
Have you read the replies to a similar post?

a)

Code:
struct
{
short int a;
int b;
};
How big is a "short int"?
On 32-bit platforms, some have 16-bit shorts and some
have 32-bit short ints.

Let us assume that the processor only fetches at
32-bit address locations, that is every 4 bytes in
common notation. We''ll also assume that a short int
is 16-bits (2 bytes).

To make life easier, we well declare a variable of
the above structure and allocate it at address 0x00.
Let us assign the value 0x1616 to the ''a'' member and
0x32323232 to the ''b'' member. We''ll also use Big
Endian notation.

Address Value
------- -----
0100 1616 -- member ''a''
0102* 3232323232 -- member ''b''
0106* 0000 -- unknown value in memory.

Since this processor only fetches at 4 byte boundaries,
it will fetch at addresses 0x100 and 0x104. The address
of 0x102 cannot be accessed directly and is out of
alignment. The actual values for ''a'' and ''b'' might
be: a == 0x16163232, b == 0x32320000

The compiler is allowed to add padding between members
so that the values are placed at locations where the
processor can fetch them correctly:

0x100 1616 -- member ''a''
0x102 0000 -- padding from the compiler.
0x104 32323232 -- member '' b''

The above layout allows the processor to fetch the
value 0x1616 at location 0x100 and 0x32323232 at
location 0x104.

The exercise of a 64-bit machine is left to the
reader. Hint: a 64-bit machine likes to fetch
at every 8 bytes; and assume a short int is either
16 or 32 bits.


b)
Code:
struct{
short int a;
int b;
int somefunc();
virtual int func1();
};
Similar problem as above. However, this time use
pointers instead of the functions above. You could
assume that a 32-bit processor has a 32-bit pointer,
and a 64-bit processor has a 64-bit pointer.

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();
};




每个不需要VTables标准。所以这个

的问题可以被抛弃。但是根据

问题的精神,可以假设vtable只是一个指针数组。请参阅问题''b''。


对于一些额外的练习,请考虑以下结构:

struct A

{

char c;

short int si;

int j;

char d

int i;

};


struct B

{

int i;

short int si;

char c;

char d;

int j;

};


-

Thomas Matthew


C ++新闻组欢迎辞:
http://www.slack.net/~shiva/welcome.txt

C ++常见问题: http://www.parashift .com / c ++ - faq-lite

C常见问题: http://www.eskimo.com/~scs/c-faq/top.html

alt.comp.lang.learn。 c-c ++ faq :
http://www.comeaucomputing.com/learn/faq/

其他网站:
http:// www .josuttis.com - C ++ STL图书馆书籍
http: //www.sgi.com/tech/stl - 标准模板库



VTables are not required per the standard. So this
question can be thrown out. But in the spirit of the
question, one could assume that the vtable is just an
array of pointers. See question ''b''.

For some extra practice, consider these structs:
struct A
{
char c;
short int si;
int j;
char d
int i;
};

struct B
{
int i;
short int si;
char c;
char d;
int j;
};

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library


这篇关于C - 分配中的内存大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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