473,320 Members | 2,189 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

sizeof(EmptyStruct) in C and C++ (was: Base {}; sizeof(Base) == 1?)


"Alf P. Steinbach" <al***@start.nowrote in message news:4k************@individual.net...
* mo********@yahoo.com:
This may be stupid question, but why is sizeof(Base) == 1 in:

int main(int argc, char* argv[])
{
class Base
{
};
cout << sizeof(Base) << endl;
return 0;
}

I guess I want to know what the 1 byte is for? There is no vptr here,
so why 1 byte?I checked FAQ and couldn't find answer.

Needs a unique address.
struct Empty {};

C: sizeof(Empty) == 0
C++: sizeof(Empty) 0

Why doesn't C need a unique address?
--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Aug 14 '06 #1
7 2469
"Alex Vinokur" <al****@users.sourceforge.netwrites:
"Alf P. Steinbach" <al***@start.nowrote in message
news:4k************@individual.net...
>* mo********@yahoo.com:
This may be stupid question, but why is sizeof(Base) == 1 in:

int main(int argc, char* argv[])
{
class Base
{
};
cout << sizeof(Base) << endl;
return 0;
}

I guess I want to know what the 1 byte is for? There is no vptr here,
so why 1 byte?I checked FAQ and couldn't find answer.

Needs a unique address.

struct Empty {};

C: sizeof(Empty) == 0
C++: sizeof(Empty) 0

Why doesn't C need a unique address?
In C,
struct Empty {};

is a syntax error. (Some compilers might support that as an
extension; if so, it's up to the compiler to decide what
sizeof(struct Empty) should be.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Aug 14 '06 #2

Keith Thompson wrote:
"Alex Vinokur" <al****@users.sourceforge.netwrites:
"Alf P. Steinbach" <al***@start.nowrote in message
news:4k************@individual.net...
* mo********@yahoo.com:
This may be stupid question, but why is sizeof(Base) == 1 in:

int main(int argc, char* argv[])
{
class Base
{
};
cout << sizeof(Base) << endl;
return 0;
}

I guess I want to know what the 1 byte is for? There is no vptr here,
so why 1 byte?I checked FAQ and couldn't find answer.

Needs a unique address.
struct Empty {};

C: sizeof(Empty) == 0
C++: sizeof(Empty) 0

Why doesn't C need a unique address?

In C,
struct Empty {};

is a syntax error. (Some compilers might support that as an
extension; if so, it's up to the compiler to decide what
sizeof(struct Empty) should be.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
What is the size of a in
int a[0];

Aug 14 '06 #3
swets wrote:
What is the size of a in
int a[0];
A zero size array is illegal according to ANSI C.
Igmar

Aug 14 '06 #4
What if I use it in a structure like -
struct A {
int a[0];
};

What is the size of such structure?

Thanks,
Sweta

Igmar Palsenberg wrote:
swets wrote:
What is the size of a in
int a[0];

A zero size array is illegal according to ANSI C.
Igmar
Aug 14 '06 #5
Sweta writes:
What if I use it in a structure like -
struct A {
int a[0];
};

What is the size of such structure?
Still not valid. Standard C does not allow 0-sized objects - and that
includes objects that are part of other objects.

--
Hallvard
Aug 14 '06 #6
Sweta wrote:
What if I use it in a structure like -
struct A {
int a[0];
};

What is the size of such structure?
What isn't clear about 'a zero size array is illegal' ? Putting it in a
doesn't make it less of an array.
Igmar
Aug 14 '06 #7
On 2006-08-14 04:55:31 -0400, "Sweta" <sw****@gmail.comsaid:
>
Igmar Palsenberg wrote:
>swets wrote:
>>What is the size of a in
int a[0];

A zero size array is illegal according to ANSI C.
What if I use it in a structure like -
struct A {
int a[0];
};

What is the size of such structure?
As Igmar said, "A zero size array is illegal according to ANSI C."

--
Clark S. Cox, III
cl*******@gmail.com

Aug 14 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Paul | last post by:
In real life situation, do we ever come across a situation where we would need two base objects in an object. A snippet is worth 1000 words (: so... class Base { }; class Derived1:public Base...
3
by: J.J. Feminella | last post by:
(Please disregard the previous message; I accidentally sent it before it was completed.) I have source code similar to the following. public class Vehicle { protected string dataV; // ......
3
by: hazz | last post by:
The following classes follow from the base class ' A ' down to the derived class ' D ' at the bottom of the inheritance chain. I am calling the class at the bottom, "public class D" from a client...
10
by: Peter Oliphant | last post by:
Is there a way of defining a method in a base class such that derived classes will call their own version, EVEN if the derived instance is referred to by a pointer to the base class? Note that the...
6
by: Taran | last post by:
Hi All, I tried something with the C++ I know and some things just seem strange. consider: #include <iostream> using namespace std;
32
by: moleskyca1 | last post by:
This may be stupid question, but why is sizeof(Base) == 1 in: int main(int argc, char* argv) { class Base { }; cout << sizeof(Base) << endl; return 0; }
15
by: Bob Johnson | last post by:
I have a base class that must have a member variable populated by, and only by, derived classes. It appears that if I declare the variable as "internal protected" then the base class *can*...
15
by: Juha Nieminen | last post by:
I'm sure this is not a new idea, but I have never heard about it before. I'm wondering if this could work: Assume that you have a common base class and a bunch of classes derived from it, and...
6
by: Me | last post by:
I need to be able to acces non-virtual members of sublcasses via a base class pointer...and without the need for an explicit type cast. I thought a pure virtual getPtr() that acts as a type cast...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.