boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - A question about class size
相关主题
看下这个小程序
C 语言,初学者,简单问题
关于C++中一个Class的大小 (转载)
请问c++里empty class的问题
STL map变量的实际memory usage估算
指针的大小是 4 byte还是有赖于系统?
关于 big/little endian,为什么对char 有影响?
奇怪的问题:关于一个简单的malloc()小程序 (转载)
关于buffer overflow
数组问题
相关话题的讨论汇总
话题: class话题: n1话题: char话题: given话题: address
进入Programming版参与讨论
1 (共1页)
e******d
发帖数: 310
1
Given class A and class B, class A is an empty class and class B has one data member whose type is char; their definitions are as follows:
============================
class A
{
};
class B
{
private:
char c;
};
int n1 = sizeof(A); //n1 = 1;
int n2 = sizeof(B); //n2 = 1;
================================
The question is why n1 is equal to 1.
Thank you a lot.
z****e
发帖数: 2024
2
an naive answer is:
for every empty class, it has to have a dummy byte to hold the unique
address for an object to be instantiated.
e******d
发帖数: 310
3
Thank you for your input.
But I am not quite clear about your statement. My understanding is like this: if an address should be kept, then the size should be 4 bytes instead of 1 given the code is compiled in 32-bit mode.
z****e
发帖数: 2024
4
think again,
1. what's the size of a char?
2. can you take address of a char?

this: if an address should be kept, then the size should be 4 bytes instead
of 1 given the code is compiled in 32-bit mode.

【在 e******d 的大作中提到】
: Thank you for your input.
: But I am not quite clear about your statement. My understanding is like this: if an address should be kept, then the size should be 4 bytes instead of 1 given the code is compiled in 32-bit mode.

z****e
发帖数: 2024
5
actually, i think my previous answer is a little bit misleading.
the byte is not going to "hold" the address, but to make it possible that
the instantiation can have an unique address. the smallest cost is 1 byte of
memory to do so.
clear?

this: if an address should be kept, then the size should be 4 bytes instead
of 1 given the code is compiled in 32-bit mode.

【在 e******d 的大作中提到】
: Thank you for your input.
: But I am not quite clear about your statement. My understanding is like this: if an address should be kept, then the size should be 4 bytes instead of 1 given the code is compiled in 32-bit mode.

1 (共1页)
进入Programming版参与讨论
相关主题
数组问题
怎么得到char *分配空间的大小?
在帮忙看看这个吧 C: int->char*
help about convert a char array in c
请教C的类型转换问题
请问如何对付error C2148问题:陣列的總大小不能超過 0x7fffffff 位元組
C puzzle 一日一题
scala最烦人的是build太不容易了
两个面世题
C++ Q 108: swap
相关话题的讨论汇总
话题: class话题: n1话题: char话题: given话题: address