基地{}; sizeof(Base)== 1? [英] Base {}; sizeof(Base) == 1?

查看:99
本文介绍了基地{}; sizeof(Base)== 1?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是个愚蠢的问题,但为什么sizeof(Base)== 1 in:


int main(int argc,char * argv [])

{

类基地

{

};

cout<< sizeof(Base)<< endl;

返回0;

}


我想我想知道1字节的用途是什么?这里没有vptr,

为什么1个字节呢?我检查了常见问题,但找不到答案。

This may be stupid question, but why is sizeof(Base) == 1 in:

int main(int argc, char* argv[])
{
class Base
{
};
cout << sizeof(Base) << endl;
return 0;
}

I guess I want to know what the 1 byte is for? There is no vptr here,
so why 1 byte?I checked FAQ and couldn''t find answer.

推荐答案

* mo********@yahoo.com

这可能是个愚蠢的问题,但为什么sizeof(Base)== 1 in:


int main(int argc,char * argv [])

{

类底座

{

};

cout<< sizeof(Base)<< endl;

返回0;

}


我想我想知道1字节的用途是什么?这里没有vptr,

为什么1个字节呢?我检查了常见问题,但找不到答案。
This may be stupid question, but why is sizeof(Base) == 1 in:

int main(int argc, char* argv[])
{
class Base
{
};
cout << sizeof(Base) << endl;
return 0;
}

I guess I want to know what the 1 byte is for? There is no vptr here,
so why 1 byte?I checked FAQ and couldn''t find answer.



需要一个独特的地址。


-

答:因为它搞砸了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门发布。

问: usenet和电子邮件中最烦人的事情是什么?

Needs a unique address.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


文章< 11 **************** *****@m73g2000cwd.googlegroups。 com>,
mo********@yahoo.com 写道:
In article <11*********************@m73g2000cwd.googlegroups. com>,
mo********@yahoo.com wrote:

这可能是个愚蠢的问题,但为什么sizeof(Base)== 1 in:


int main(int argc, char * argv [])

{

class base

{

};

cout<< sizeof(Base)<< endl;

返回0;

}


我想我想知道1字节的用途是什么?这里没有vptr,

为什么1个字节呢?我检查了常见问题,但找不到答案。
This may be stupid question, but why is sizeof(Base) == 1 in:

int main(int argc, char* argv[])
{
class Base
{
};
cout << sizeof(Base) << endl;
return 0;
}

I guess I want to know what the 1 byte is for? There is no vptr here,
so why 1 byte?I checked FAQ and couldn''t find answer.



class Base {};


int main(){

基数[2 ];

断言(& bases [0]!=& bases [1]);

}


如何如果sizeof(

Base)为0,编译器是否可以确保上述断言为真?

class Base { };

int main() {
Base bases[2];
assert( &bases[0] != &bases[1] );
}

How could the compiler ensure the above assertion is true if sizeof(
Base ) was 0?


moleskyca1发布:
moleskyca1 posted:

这可能是个愚蠢的问题,但为什么sizeof(Base)== 1 in:


int main(int argc,char * argv [] )

{

等级基础

{

};

cout< < sizeof(Base)<< endl;

返回0;

}


我想我想知道1字节的用途是什么?这里没有vptr,

为什么1个字节呢?我检查了常见问题,但找不到答案。
This may be stupid question, but why is sizeof(Base) == 1 in:

int main(int argc, char* argv[])
{
class Base
{
};
cout << sizeof(Base) << endl;
return 0;
}

I guess I want to know what the 1 byte is for? There is no vptr here,
so why 1 byte?I checked FAQ and couldn''t find answer.



在很大程度上,C ++可以用好像来实现。办法。这是一个打印整数0到9的
示例程序:


#include< iostream>


使用std :: cout;


int main()

{

for(unsigned i = 0; i! = 10; ++ i)

{

cout<< i<< ''\\ n'';

}

}


根据C ++标准,此程序必须打印整数0

到9 ...然而它在如何实现这一目标方面有很大的自由度,因此该方案的工作原理是好像。它以原始方式编码。对于你知道的所有

,编译器可能会将其更改为:


cout<< 0U<< ''\ n'';

cout<< 1U<< ''\ n'';

cout<< 2U<< ''\ n'';

cout<< 3U<< ''\ n'';

cout<< 4U<< ''\ n'';

cout<< 5U<< ''\ n'';

cout<< 6U<< ''\ n'';

cout<< 7U<< ''\ n'';

cout<< 8U<< ''\ n'';

cout<< 9U<< ''\ n'';


或者甚至可能:


cout<< 0 \ nn \ nn2 \ n3 \ n4 \ n5 \ n6 \ n7 \ n8 \ nn9" ;;


这个好像原则给编译器很大的自由。


每个对象(有效且尚未销毁)必须有一个

唯一地址。例如:


struct MyStruct {};


int main()

{

MyStruct obj1;

MyStruct obj2;


断言(& obj1!=& obj2);

}


如果每个对象必须有一个唯一的地址,那么该地址的字节(或者可能是

字)不能用于其他任何目的。 />

根据这个,sizeof可能会返回1,或者甚至可能是4.


但是,按照好像原则上,如果你从不以任何形式获取对象的

地址,那么就没有理由为什么它必须

预留内存。例如,编译器可能会更改以下代码:


struct A {};

struct B {};


void Func(A){}

void Func(B){}


int main()

{

A a; B b;


Func(a); Func(b);

}


简单地说:


void FuncA(){}

void FuncB(){}


int main()

{

FuncA(); FuncB();

}


最后,sizeof永远不会产生零。


-


Frederick Gotham


To a large extent, C++ can be implemented in an "as if" way. Here''s a
sample program which prints the integers 0 through 9:

#include <iostream>

using std::cout;

int main()
{
for(unsigned i = 0; i != 10; ++i)
{
cout << i << ''\n'';
}
}

In accordance with the C++ Standard, this program must print the integers 0
through 9... however it has much freedom in how it achieves this, just so
long as the program works "as if" it were coded the original way. For all
you know, the compiler may change it into:

cout << 0U << ''\n'';
cout << 1U << ''\n'';
cout << 2U << ''\n'';
cout << 3U << ''\n'';
cout << 4U << ''\n'';
cout << 5U << ''\n'';
cout << 6U << ''\n'';
cout << 7U << ''\n'';
cout << 8U << ''\n'';
cout << 9U << ''\n'';

Or perhaps even:

cout << "0\n1\n2\n3\n4\n5\n6\n7\n8\n9";

This "as if" principle gives compilers great freedom.

Every object (which is valid and has yet to be destroyed) must have a
unique address. For example:

struct MyStruct {};

int main()
{
MyStruct obj1;
MyStruct obj2;

assert(&obj1 != &obj2);
}

If every object must have a unique address, then the byte (or perhaps the
word) at that address cannot be used for anything else.

In accordance with this, "sizeof" might return 1, or maybe even 4.

However, in accordance with the "as if" principle, if you never take the
address of an object in any form, then there''s no reason why it must
reserve memory. For instance, the compiler might change the following code:

struct A {};
struct B {};

void Func(A) {}
void Func(B) {}

int main()
{
A a; B b;

Func(a); Func(b);
}

into simply:

void FuncA() {}
void FuncB() {}

int main()
{
FuncA(); FuncB();
}

Lastly, "sizeof" shall never yield zero.

--

Frederick Gotham


这篇关于基地{}; sizeof(Base)== 1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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