相关疑难解决方法(0)

为什么C++中空类的大小不为零?

可能重复:
C++:空类对象的大小是多少?

为什么以下输出1

#include <iostream>

class Test
{
};

int main()
{
    std::cout << sizeof(Test);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ sizeof

74
推荐指数
4
解决办法
5万
查看次数

sizeof(空结构)和sizeof(结构与空数组)之间的区别?

我有两个结构定义如下:

struct EmptyStruct{

};

struct StructEmptyArr{
    int arr[0];
};

int main(void){
    printf("sizeof(EmptyStruct) = %ld\n", sizeof(EmptyStruct));
    printf("sizeof(StructEmptyArr) = %ld\n", sizeof(StructEmptyArr));

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

在Ubuntu 14.04,x64上用gcc(g ++)4.8.4编译.

输出(对于gcc和g ++):

sizeof(EmptyStruct) = 1
sizeof(StructEmptyArr) = 0
Run Code Online (Sandbox Code Playgroud)

我能理解为什么sizeof(EmptyStruct)等于1但不能理解为什么sizeof(StructEmptyArr)等于0.为什么两者之间存在差异?

c c++ struct sizeof

35
推荐指数
2
解决办法
2075
查看次数

sizeof空结构在C中为0,在C++中为1为什么?

可能重复:
C++中的空类C
中空结构的大小是多少?

我在某处看到C++中空结构的大小为1.所以我想到验证它.不幸的是我把它保存为C文件并使用了<stdio.h>标题,我很惊讶地看到了输出.这是0.

这意味着

struct Empty {

};

int main(void)
{
  printf("%d",(int)sizeof(Empty));
}
Run Code Online (Sandbox Code Playgroud)

在编译为C文件时打印0,在编译为C++文件时打印1.我想知道原因.我读到c ++中的sizeof空结构不为零,因为如果大小为0,则该类的两个对象将具有相同的地址,这是不可能的.我哪里错了?

c c++ struct sizeof

33
推荐指数
3
解决办法
2万
查看次数

C和C++代码之间不兼容

给定的C代码

#include <stdio.h>
int x = 14; 
size_t check()
{
   struct x {};
   return sizeof(x); // which x
}
int main()
{
    printf("%zu",check()); 
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

在我的32位实现上给出4作为C的输出,而在C++中给出代码

#include <iostream>
int x = 14;    
size_t check()
{
   struct x {};
   return sizeof(x); // which x    
}
int main()
{
    std::cout<< check(); 
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出1.为何如此差异?

c c++ incompatibility

17
推荐指数
3
解决办法
809
查看次数

C中的空结构与C++中的空结构

为什么C中的空结构违反约束?为什么在C++中更改此规则?

有历史原因吗?

c c++ struct rationale

7
推荐指数
1
解决办法
2003
查看次数

空结构定义在C中是非法的但不是C++?

struct t_empty {
};
Run Code Online (Sandbox Code Playgroud)

这似乎可以在C++中正确编译,但不能在C中编译.(至少使用TI 28xx DSP编译器,它会发出错误"预期声明")这是在C标准的某处提到的,还是我的编译器坏了?

c c++ struct

6
推荐指数
1
解决办法
2249
查看次数

为什么在uni-processor上sizeof(spinlock_t)大于零?

以下行打印输出为4,而我期待0.

 printk(KERN_INFO "size of spinlock_t  %d\n", sizeof(spinlock_t));
Run Code Online (Sandbox Code Playgroud)

我在一个单CPU的系统上试过这个.构建内核时没有启用调试标志CONFIG_DEBUG_SPINLOCK or CONFIG_DEBUG_LOCK_ALLOC.根据内核头文件,它应该为零,但输出与它不一致,任何猜测?

linux kernel linux-kernel

2
推荐指数
1
解决办法
1227
查看次数

没有成员的结构

虽然在这里引用内核代码,但是struct page;没有成员定义(我猜这不是前向声明).

但是这篇文章中接受的答案是不允许的.

然后我试了一个样本,

#include <stdio.h>

struct page;

struct arm_vmregion 
{
   unsigned long           vm_start;
   unsigned long           vm_end;
   struct page             *vm_pages;
   int                     vm_active;
   const void              *caller;
};

int main()
{
   struct arm_vmregion aa;
   return 0;
}
Run Code Online (Sandbox Code Playgroud)

它编译成功

empty_struct.c: In function ‘main’:
empty_struct.c:15:22: warning: unused variable ‘aa’ [-Wunused-variable]
Run Code Online (Sandbox Code Playgroud)

请在这方面澄清一下.

c struct linux-kernel

2
推荐指数
1
解决办法
159
查看次数

标签 统计

c ×6

c++ ×6

struct ×5

sizeof ×3

linux-kernel ×2

incompatibility ×1

kernel ×1

linux ×1

rationale ×1