Size of empty structure

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kittu
    New Member
    • Nov 2014
    • 1

    Size of empty structure

    Hi All,

    I have a doubt related Size of empty structure. I know the answer of empty structure is "0".

    Could Any one please let me know the reason, How and why the Result got ZERO..??
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    Please show us what this empty structure looks like.

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Are you talking C or C++ because the size of an empty structure in C++ is 1 and while the size of an empty structure in C is 0 there is no point in having one as it serves no purpose; it produces a warning from my compiler.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        In C++ you are always allowed to create a variable using the struct. That variable must exist in memory. The smallest allocation is typically one byte. Hence sizeof the struct is 1.

        As far as C goes, you are required to have at least one member in a struct. If not, you get a compile error. Therefore, in C a struct does not have a size 0 because that code won't even compile.

        Comment

        Working...