ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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?
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
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.
No. Although I should've said "empty" instead of "zero-sized"
Quote:
Originally Posted by Anisha Kaul
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.