LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-21-2011, 02:47 AM   #1
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
C++: Size of an empty class


Code:
#include <iostream>
using namespace std;

class Z
{
};

int main() 
{
  cout << sizeof (Z);
}
The output is 1 here.

What's the compiler's reasoning behind this? (in a layman's language, please.)

Last edited by Aquarius_Girl; 10-21-2011 at 02:49 AM.
 
Old 10-21-2011, 04:40 AM   #2
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,281

Rep: Reputation: 320Reputation: 320Reputation: 320Reputation: 320
Some explanation from google search about size of empty classess In my understanding: thay have zero size, but compiler create dummy field with 1 byte to distinguish one from other,for example in arrays. It is done for convenient.
 
1 members found this post helpful.
Old 10-21-2011, 05:52 AM   #3
SigTerm
Member
 
Registered: Dec 2009
Distribution: Slackware 12.2
Posts: 379

Rep: Reputation: 234Reputation: 234Reputation: 234
Quote:
Originally Posted by Anisha Kaul View Post
The output is 1 here.

What's the compiler's reasoning behind this? (in a layman's language, please.)
Quote:
expr.sizeof
When applied to a reference or a reference type, the result is the size of the referenced type. When applied
to a class, the result is the number of bytes in an object of that class including any padding required for
placing objects of that type in an array. The size of a most derived class shall be greater than zero (1.8).
The result of applying sizeof to a base class subobject is the size of the base class type.70)When applied
to an array, the result is the total number of bytes in the array. This implies that the size of an array of n
elements is n times the size of an element.
In other words, you should be able to put class into array. Since having sizeof()==0 will cause problem with arrays, size is 1. But this isn't guaranteed, since as far as I can tell, sizeof() is not required to return values >= 0.

P.S> I'd advise to get copy of C++ standard. It should be possible to get ANSI ISO IEC 14882 2003 somewhere.
 
1 members found this post helpful.
Old 12-20-2011, 03:13 AM   #4
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731

Original Poster
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
eSelix,
Your link was very helpful in understanding why:
Quote:
Since having sizeof()==0 will cause problem with arrays, size is 1
SigTerm
Thanks for the C++ standard suggestion. Helpful indeed.
This is what I've found: http://www.open-std.org/jtc1/sc22/wg21/
 
Old 12-20-2011, 04:00 AM   #5
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731

Original Poster
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
BTW, if we temporarily forget about the array problem,
won't it be logical to say that if the size of the class
is allowed to be zero, then on what basis would the
memory be allocated to that class?

And if you access something by an object of size 0 (which
will actually mean that zero memory has been allocated),
you'll get seg faults, and therefore a class is not allowed
to have size 0?
 
Old 12-20-2011, 04:24 AM   #6
SigTerm
Member
 
Registered: Dec 2009
Distribution: Slackware 12.2
Posts: 379

Rep: Reputation: 234Reputation: 234Reputation: 234
Quote:
Originally Posted by Anisha Kaul View Post
BTW, if we temporarily forget about the array problem,
won't it be logical to say that if the size of the class
is allowed to be zero, then on what basis would the
memory be allocated to that class?
Not really. It will be logical to say that if the class is empty, then there's no need for you to allocate it/create instances.

C++ allows zero sized empty structures to be used as placeholders for internal types, etc. It is frequently used in libraries that provide some kind of API - it is better than void pointer, since compiler does not allow you to cast between different pointers freely.
Code:
struct A{
};

struct B{
};

int main(int argc, char** argv){
	A* a = 0;
	B* b = a;//<-- compiler error here
	void* c = a;//<-- no compiler error
	return 0;
}
Quote:
Originally Posted by Anisha Kaul View Post
And if you access something by an object of size 0 (which
will actually mean that zero memory has been allocated),
you'll get seg faults, and therefore a class is not allowed
to have size 0?
NOt gonna happen. segfault is OS feature and it happens when program "hits" page that is not available for the program.
Page has minimal size, and although it is OS-dependent AND configuration-dependent, it is quite large - 4kB or 8kB per page. To implement segfault on zero-sized object segfault, you'll need to align all varaibles to pages, and allocate PAGE_SIZE bytes for each. It is very likely that you'll run out of memory very quickly. Or you'll have to turn everything into pointer, which will cause extra problems.
 
Old 12-20-2011, 04:38 AM   #7
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731

Original Poster
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Quote:
Originally Posted by SigTerm View Post
C++ allows zero sized empty structures
Code:
#include <iostream>
using namespace std;

struct A
{
};

int main ()
{
	A a;
	cout << "\nsdf " << sizeof (a);

	return 0;
}
This returns 1. You meant something else?

Quote:
Originally Posted by SigTerm View Post
to be used as placeholders for internal types, etc.
What did you mean here? You mean int, char, etc?

Last edited by Aquarius_Girl; 12-20-2011 at 04:39 AM.
 
Old 12-20-2011, 10:38 AM   #8
SigTerm
Member
 
Registered: Dec 2009
Distribution: Slackware 12.2
Posts: 379

Rep: Reputation: 234Reputation: 234Reputation: 234
Quote:
Originally Posted by Anisha Kaul View Post
This returns 1. You meant something else?
No. Although I should've said "empty" instead of "zero-sized"

Quote:
Originally Posted by Anisha Kaul View Post
What did you mean here? You mean int, char, etc?
No.
Let's say you're making a library that provides "handles" for internal objects whose contents should not be visible from outside. Objects have no methods (not the best example, though, but it is just for demonstration purposes).

Let's say library provides handles for File and Mutex.

Code:
File* createFile(const wchar_t* filename);
void closeFile(File* f);

Mutex* createMutex();
void destroyMutex(Mutex* m);
One way to declare such "handle" type is to use "typedef void".
However this will make Mutex* compatible with File*. So you can assign them to each other.
Now you don't want to pass file to mutex routines by accident and vice versa.
So you declare File and Mutex as
Code:
struct File{
};

struct Mutex{
};
And the problem is solved. Mutex* and File* are not compatible to each other and require explicit cast to assign them to each other.

Of course if you were to properly approach this problem, you could make several abstract classes, add private constructors, etc, but this is just a demonstration.

Aside from that, you should explain why do you need zero-sized structures. C++ have unions .
Code:
#include <iostream>

typedef void File;

union Sample{
    int i; 
    float f;
    char c[4];
};

int main(int argc, char** argv){
	std::cout << sizeof(Sample);
	return 0;
}
... and namespaces. One of those might be what you've been looking for.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ templated Node class: pointers to different instantated class types jhwilliams Programming 3 08-20-2007 06:20 PM
c++ question, how to define a member array, and it's size, outside of the class dec.. Winter Knight Programming 2 01-23-2007 07:28 AM
Does derivated class inherit base class destructor (constructor)? kornerr Programming 2 08-23-2006 08:05 AM
Which C++ editor in Linux has the class view/class browser feature imaginationworks Programming 7 05-21-2006 11:09 PM
copy constructor for class containing array of vaiable size objects ? qwijibow Programming 4 12-21-2005 09:50 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration