<legend id='R9qa5'><style id='R9qa5'><dir id='R9qa5'><q id='R9qa5'></q></dir></style></legend><tfoot id='R9qa5'></tfoot>
    <bdo id='R9qa5'></bdo><ul id='R9qa5'></ul>

    <small id='R9qa5'></small><noframes id='R9qa5'>

  1. <i id='R9qa5'><tr id='R9qa5'><dt id='R9qa5'><q id='R9qa5'><span id='R9qa5'><b id='R9qa5'><form id='R9qa5'><ins id='R9qa5'></ins><ul id='R9qa5'></ul><sub id='R9qa5'></sub></form><legend id='R9qa5'></legend><bdo id='R9qa5'><pre id='R9qa5'><center id='R9qa5'></center></pre></bdo></b><th id='R9qa5'></th></span></q></dt></tr></i><div id='R9qa5'><tfoot id='R9qa5'></tfoot><dl id='R9qa5'><fieldset id='R9qa5'></fieldset></dl></div>
    1. sizeof空结构在C中为0,在C++中为1,为什么?

      sizeof empty structure is 0 in C and 1 in C++ why?(sizeof空结构在C中为0,在C++中为1,为什么?)
      <tfoot id='cbOc9'></tfoot>
      • <i id='cbOc9'><tr id='cbOc9'><dt id='cbOc9'><q id='cbOc9'><span id='cbOc9'><b id='cbOc9'><form id='cbOc9'><ins id='cbOc9'></ins><ul id='cbOc9'></ul><sub id='cbOc9'></sub></form><legend id='cbOc9'></legend><bdo id='cbOc9'><pre id='cbOc9'><center id='cbOc9'></center></pre></bdo></b><th id='cbOc9'></th></span></q></dt></tr></i><div id='cbOc9'><tfoot id='cbOc9'></tfoot><dl id='cbOc9'><fieldset id='cbOc9'></fieldset></dl></div>

            • <bdo id='cbOc9'></bdo><ul id='cbOc9'></ul>
                <tbody id='cbOc9'></tbody>

            • <small id='cbOc9'></small><noframes id='cbOc9'>

                <legend id='cbOc9'><style id='cbOc9'><dir id='cbOc9'><q id='cbOc9'></q></dir></style></legend>

              1. 本文介绍了sizeof空结构在C中为0,在C++中为1,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

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

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

                I read somewhere that size of an empty struct in C++ is 1. So I thought of verifying it. Unfortunately I saved it as a C file and used <stdio.h> header and I was surprised to see the output. It was 0.

                这意味着

                struct Empty {
                
                };
                
                int main(void)
                {
                  printf("%d",(int)sizeof(Empty));
                }
                

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

                was printing 0 when compiled as a C file and 1 when compiled as a C++ file. I want to know the reason. I read that sizeof empty struct in C++ is not zero because if the size were 0 then two objects of the class would have the same address which is not possible. Where am I wrong?

                推荐答案

                C 中不能有空结构.这是违反语法约束的.然而,gcc 允许 C 中的空结构作为扩展.此外,如果结构没有任何命名成员,则行为 未定义,因为

                You cannot have an empty structure in C. It is a syntactic constraint violation. However gcc permits an empty structure in C as an extension. Furthermore the behaviour is undefined if the structure does not have any named member because

                C99 说:

                如果结构声明列表不包含命名成员,则行为未定义.

                If the struct-declaration-list contains no named members, the behavior is undefined.

                所以

                struct Empty {}; //constraint violation
                
                struct Empty {int :0 ;}; //no named member, the behaviour is undefined.
                

                是的,空结构的大小是 C++ 不能为零 :)

                And yes size of an empty struct is C++ cannot be zero :)

                这篇关于sizeof空结构在C中为0,在C++中为1,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                How to print vector#39;s data(如何打印矢量的数据)
                Visual C++ appends 0xCC (int3) bytes at the end of functions(Visual C++ 在函数末尾附加 0xCC (int3) 字节)
                How to use a variable inside a _T wrapper?(如何在 _T 包装器中使用变量?)
                MSVC++ warning flags(MSVC++ 警告标志)
                How to read file which contains uxxxx in vc++(如何在vc++中读取包含uxxxx的文件)
                stack overflow error in C++ program(C++程序中的堆栈溢出错误)

                      <tbody id='9Ywqg'></tbody>
                    <i id='9Ywqg'><tr id='9Ywqg'><dt id='9Ywqg'><q id='9Ywqg'><span id='9Ywqg'><b id='9Ywqg'><form id='9Ywqg'><ins id='9Ywqg'></ins><ul id='9Ywqg'></ul><sub id='9Ywqg'></sub></form><legend id='9Ywqg'></legend><bdo id='9Ywqg'><pre id='9Ywqg'><center id='9Ywqg'></center></pre></bdo></b><th id='9Ywqg'></th></span></q></dt></tr></i><div id='9Ywqg'><tfoot id='9Ywqg'></tfoot><dl id='9Ywqg'><fieldset id='9Ywqg'></fieldset></dl></div>
                  • <legend id='9Ywqg'><style id='9Ywqg'><dir id='9Ywqg'><q id='9Ywqg'></q></dir></style></legend>

                          <bdo id='9Ywqg'></bdo><ul id='9Ywqg'></ul>

                          <small id='9Ywqg'></small><noframes id='9Ywqg'>

                        • <tfoot id='9Ywqg'></tfoot>