我发现MSVC和GCC编译器每个类实例至少分配一个字节,即使该类是没有成员变量的谓词(或只有静态成员变量).以下代码说明了这一点.
#include <iostream>
class A
{
public:
bool operator()(int x) const
{
return x>0;
}
};
class B
{
public:
static int v;
static bool check(int x)
{
return x>0;
}
};
int B::v = 0;
void test()
{
A a;
B b;
std::cout << "sizeof(A)=" << sizeof(A) << "\n"
<< "sizeof(a)=" << sizeof(a) << "\n"
<< "sizeof(B)=" << sizeof(B) << "\n"
<< "sizeof(b)=" << sizeof(b) << "\n";
}
int main()
{
test();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
sizeof(A)=1
sizeof(a)=1
sizeof(B)=1 …Run Code Online (Sandbox Code Playgroud) 我正在尝试在C中使用64位整数,但我得到的混合信号是否应该是可能的.
当我执行printf时:
printf("Size of long int:%d\nSize of long long int:%d\n\n",(int)sizeof(long int), (int)sizeof(long long int));
Run Code Online (Sandbox Code Playgroud)
我得到的回应是:
long int的大小:4 long long int的大小:8
这让我觉得long long int有8个字节= 64位.
但是,当我尝试声明以下变量时:
long long int a2 = 0x00004444;
long long int b2 = 0x000044440;
long long int c2 = 0x0000444400;
long long int d2 = 0x00004444000;
long long int e2 = 0x000044440000;
long long int f2 = 0x0000444400004;
long long int g2 = 0x00004444000044;
long long int h2 = 0x000044440000444;
long long int i2 = 0x0000444400004444;
Run Code Online (Sandbox Code Playgroud)
最后4个变量(f2,g2,h2,i2)给出了错误信息: …
假设我有一个带有一些成员变量且没有虚函数的基类:
class Base {
int member;
};
Run Code Online (Sandbox Code Playgroud)
以及从非虚拟方式派生Base并且没有新成员变量的派生类,再次没有虚函数:
class Derived : Base {
};
Run Code Online (Sandbox Code Playgroud)
显然sizeof(Derived)不能小于sizeof(Base).
被sizeof(Derived)要求等于sizeof(Base)?
从[5.3.3/1]开始,我发现:
sizeof运算符不应用于具有函数或不完整类型的表达式
从[3.9/5]我发现:
不完全定义的对象类型和cv void是不完整的类型
无论如何,因为sizeof不评估它的操作数,我会说这sizeof(void())是一个合法的表达式(实际上GCC编译它,结果是1).
另一方面,从这里开始,void在讨论sizeof时没有提及,既没有提到具有大小1的类型,也没有提到具有实现定义大小的类型的列表.
因此问题是:是sizeof(void())一种法律表达吗?
是否保证大小等于1?
或者这是一个导致UB的法律表达,这就是全部?
假设您使用的是支持C99(甚至只是stdint.h)的编译器,是否有任何理由不使用固定宽度整数类型,如uint8_t?
我所知道的一个原因是,char在处理字符而不是使用s时使用s 更有意义(u)int8_t,如本问题所述.
但是如果你打算存储一个数字,你何时想要使用一种你不知道它有多大的类型?即在什么情况下你想要在unsigned short不知道它是8,16甚至32位的情况下存储一个数字,而不是使用uint16t?
接下来,使用固定宽度整数或使用普通整数类型并且从不假设任何东西并且使用sizeof它们需要知道它们使用了多少字节时,它被认为是更好的做法吗?
有人可以解释这段代码的工作原理吗 我知道这段代码的目的是获取数组的长度,但我不知道这段代码是如何工作的:
template<typename T, int size>
int GetArrLength(T(&)[size]){return size;}
Run Code Online (Sandbox Code Playgroud)
谢谢.
以下代码
#include <iostream>
using namespace std;
int main()
{
const char* const foo = "f";
const char bar[] = "b";
cout << "sizeof(string literal) = " << sizeof( "f" ) << endl;
cout << "sizeof(const char* const) = " << sizeof( foo ) << endl;
cout << "sizeof(const char[]) = " << sizeof( bar ) << endl;
}
Run Code Online (Sandbox Code Playgroud)
输出
sizeof(string literal) = 2
sizeof(const char* const) = 4
sizeof(const char[]) = 2
Run Code Online (Sandbox Code Playgroud)
在32位操作系统上,使用GCC编译.
sizeof计算字符串文字的长度(所需空间)?sizeof?在64位系统上,sizeof(unsigned long)取决于系统实现的数据模型,例如,它在LLP64(Windows)上为4个字节,在LP64(Linux等)上为8个字节.什么是sizeof(size_t)应该是什么?它是否随数据模型而变化sizeof(long)?如果是这样,怎么样?
参考文献:
sizeof和alignof有什么区别?
#include <iostream>
#define SIZEOF_ALIGNOF(T) std::cout<< sizeof(T) << '/' << alignof(T) << std::endl
int main(int, char**)
{
SIZEOF_ALIGNOF(unsigned char);
SIZEOF_ALIGNOF(char);
SIZEOF_ALIGNOF(unsigned short int);
SIZEOF_ALIGNOF(short int);
SIZEOF_ALIGNOF(unsigned int);
SIZEOF_ALIGNOF(int);
SIZEOF_ALIGNOF(float);
SIZEOF_ALIGNOF(unsigned long int);
SIZEOF_ALIGNOF(long int);
SIZEOF_ALIGNOF(unsigned long long int);
SIZEOF_ALIGNOF(long long int);
SIZEOF_ALIGNOF(double);
}
Run Code Online (Sandbox Code Playgroud)
将输出
1/1 1/1 2/2 2/2 4/4 4/4 4/4 4/4 4/4 8/8 8/8 8/8
我想我不知道对齐是什么......?