sizeof(empty struct) = 1 ?!?!?!? why??

This is a discussion on sizeof(empty struct) = 1 ?!?!?!? why?? within the C++ Programming forums, part of the General Programming Boards category; I was messing around the other day with the sizeof() function(or is it a macro??..shrug), testing the sizes of various ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    16

    sizeof(empty struct) = 1 ?!?!?!? why??

    I was messing around the other day with the sizeof() function(or is it a macro??..shrug), testing the sizes of various types on my machine when I came across something I did not expect. I executed the following code...
    Code:
    struct foo {
    };
    
    cout << sizeof(foo) << endl;
    ...and guess what...the answer was 1!!!

    Now this was not the answer I was expecting and upon further reflection I could think of no obvious reason for this response. Could anyone out there help me out with an explanation?

    Thanks in advance

    If it helps I was using .NET on a P4 machine running XP

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    just discussed here:
    Empty classes
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    16
    I read the thread you suggested and still don't have a firm grasp on why the compiler thinks an empty struct will take up one byte of memory. Be patient with me I can be slow on the uptake

    The fact that the byte of data is probably bogus is interesting but does not really get us anywhere. First I'd like to know why its there in the first place. Hopefully the answer to that question would shed some light on what it contains.

    Also of note, I tried to make an empty struct in a C file and interestingly enough it wouldn't compile. So this is a C++ only phenomena(sp?).

  4. #4
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    it is C++ only. Not sure exactly why they did it, but the one byte is to ensure that there is an actual memory location for the object. Of course there isn't much of a reason to do this.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    If its size was 0, then no memory would be allocated for it, and consequently, it would have no address. The following for example, would not work (be undefined, probably):
    Code:
    struct empty { };
    
    int main()
    {
      empty a; // Create an instance of the empty structure.
      empty *b = &a; // ... but a has no address...
      return 0;
    }
    So, if you ever needed an empty structure (placeholder or something to that effect), then you'd be required to do the following (or something similar):
    Code:
    struct empty { char random_useless_data; };
    Which of course has a size of 1. It seems that C++ saves you from the poor programming practice of putting it in because "you have to for it to compile", and for no other reason.

    I don't know this for sure, but I'd imagine that the actual amount of space it does take up is implementation defined, and not explicitly defined in the standard (if anyone knows otherwise, I'd be interested to hear).
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  6. #6
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    The standard says that sizeof() any instantiable type must be >= 1. There's very little else it says.

  7. #7
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    Check out here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segfault with Linked List Program
    By kbrandt in forum C Programming
    Replies: 1
    Last Post: 06-23-2009, 07:13 AM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  3. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 09:57 AM
  4. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 07:18 AM
  5. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 02:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21