English 中文(简体)
64位与32位环境中C++阵列的大小
原标题:Size of a C++ array in 64 vs 32 bit environment

假设我有一个定义如下的类

class foo
{
    char [10] bar;
}

假设没有填充/打包,在64位环境中与32位环境中相比,这个类的大小会有什么不同。

我相信该类的64位版本的长度将增加4个字节,因为:

  1. The class must contain a char* in order to point to the start of the array bar
  2. an char* is 8 bytes in a 64 bit environment vs 4 bytes in a 32 bit environment

我说得对吗?

谢谢

A further question about how arrays actually work If there is no pointer stored when you declare an array, how come you can get an address out of the array name and do things like bar[0], bar[1], etc?

最佳回答

不,你不是char[]栏甚至不会编译,char-bar[10]是正确的语法,不,不存储指针,sizeof(char)始终为1,sizeof bar将为10,而与体系结构无关。

Now for your additional question: You must understand the notion of lvalues and rvalues and lvalue-to-rvalue conversions. Usually lvalue-to-rvalue conversions "do nothing". An excetion is that an lvalue-to-rvalue conversion for array of T is conversion to a pointer to T which points to the first element of the array.

此外,按值获取T数组的函数声明等效于获取指向T的指针的函数声明。

问题回答

sizeof(char)在C++标准中被定义为1,无论您的体系结构是什么。您的结构是一个由10个字符组成的数组,而不是指向数组的指针。

此外,正确的声明应该是char-bar[10]

您可能正在考虑<code>char*bar</code>-现在,在32位和64位中,这将是4个字节和8个字节。

您可能会看到差异的一种方式是,如果编译器决定在数组后面插入一些填充,那么如果您要创建这些对象的数组,每个对象都会很好地对齐。例如,在32位目标上,它可能会将其填充为12个字节,但在64位目标上可能会填充为16个字节。

然而,如果没有填充,两者的大小将相同——您没有定义指针,因此指针大小的差异在这里没有影响。

您给出的示例在64位环境中不会变得更大。

class foo
{
    char bar[10];
};

然而,它也很无用,因为它没有公众成员。

许多类看起来更像这样:

class foo2
{
public:
    char bar[10];
    virtual ~foo2() {}
};

具有任何虚拟成员的类(如foo2-has)将随着指针大小的增长而增长。然而,对于大多数编译器,无论有多少虚拟成员,都只有一个指针。

无论哪种方式,这都只是您创建大量(>;比如20000个实例)的类的问题。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签