0
#include<iostream> 
using namespace std; 
class A
{
    int a[];
};

int main()
{
    cout<<sizeof(A);
    return 0;
}
5
Contributors
6
Replies
53
Views
4 Years
Discussion Span
Last Post by mike_2000_17
0
class A
{
    int a[]; // zero sized array
};

You should receive a warning when compiling this due to int a[] being a zero sized array.

0

When I compile this I get warning: ISO C++ forbids zero-size array 'a' although I am using the 2003 standard.

You have declared a zero size array in the class therefore the class has no data and is zero-sized. Since the array has members it appears that the compiler (mine at least) does not automatically make sure it's size is non-zero, which is normally required to ensure all objects have a different memory address which is a requirement of the standard.

Declare an array of these and the all have the same address, this appears to be non-conforming, however since you have done something that is forbidden I guess that is not unexpected, I'm slightly surprised that it isn't an error.

0

Many compilers have an "extension" to allow for zero-size arrays, I don't understand why, but they do. According to standard, this code should not compile. And those compilers that implement this zero-size array extension, end up producing a class with size of 0, as a quirk. In my opinion, this extension should not exist, but it does.

0

Supporting void main() is acceptable in the sense that the compiler can just silently insert an implicit int return value of 0, and it's all the same.

Allowing zero-sized arrays implies that some objects can be a size of 0, which violates some core assumptions in the C++ memory model. I see it as much more brazen than most other extentions that generally just slightly bend a few corner-cases of the standard or allow for some older syntax traditions.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.