mardi 5 septembre 2017

Why sizeof class object zero? [duplicate]

This question already has an answer here:

In C++, Empty class size always 1. But If I write zero size array in class like this:

#include<iostream>
using namespace std;

class A 
{
    private:
        int arr[0]; // zero size array
};

int main()
{
    A obj;
    cout<<"Size of a class :"<<sizeof(obj)<<" byte"<<endl;
    return 0;
}

Output:

0

So, Why output 0? Is it compiler bug?

Note: I have used g++ (version - 5.4.0) compiler.

Aucun commentaire:

Enregistrer un commentaire