473,799 Members | 3,858 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sizeof(struct emp) = 0

Dear all,

why the following program is giving o/p as

sizeof(struct emp) = 0

#include<stdio. h>
#include<stdlib .h>

struct emp
{ };

int main(void)
{
printf("\n sizeof(struct emp) = %d", sizeof(struct emp));

puts ("");
return (EXIT_SUCCESS);
}

i have seen this same code giving o/p as 1 in other compilers
Feb 10 '08 #1
7 3200
On Sun, 10 Feb 2008 01:30:37 -0800, sophia.agnes wrote:
Dear all,

why the following program is giving o/p as

sizeof(struct emp) = 0

#include<stdio. h>
#include<stdlib .h>

struct emp
{ };
This isn't valid C. C requires at least one member in every structure.
That said, ...
i have seen this same code giving o/p as 1 in other compilers
....why would you expect any bytes to be required to store nothing?
Shouldn't you be asking why other compilers make sizeof(struct emp)
anything other than zero? Or do you already know why?
Feb 10 '08 #2
so**********@gm ail.com wrote:
Dear all,

why the following program is giving o/p as

sizeof(struct emp) = 0

#include<stdio. h>
#include<stdlib .h>
Some white space might improve readability
>
struct emp
{ };
Gives me an error "expected a declaration"
int main(void)
{
printf("\n sizeof(struct emp) = %d", sizeof(struct emp));
sizeof() returns size_t, %d expects int.
>
puts ("");
Not needed if the printf string would end with a \n
return (EXIT_SUCCESS);
() not needed, return is not a function
}

i have seen this same code giving o/p as 1 in other compilers
Which compiler and where they called in conforming mode? Conforming to which
version of the standard?

Bye, Jojo
Feb 10 '08 #3

<so**********@g mail.comschreef in bericht
news:bb******** *************** ***********@s13 g2000prd.google groups.com...
Dear all,

why the following program is giving o/p as

sizeof(struct emp) = 0

#include<stdio. h>
#include<stdlib .h>

struct emp
{ };

int main(void)
{
printf("\n sizeof(struct emp) = %d", sizeof(struct emp));

puts ("");
return (EXIT_SUCCESS);
}

i have seen this same code giving o/p as 1 in other compilers
empty structs is illegal C
It is legal C++ though, there the size of an empty struct is always 1. Maybe
you were confused with that?

Feb 10 '08 #4
Harald van Dijk <tr*****@gmail. comwrites:
On Sun, 10 Feb 2008 01:30:37 -0800, sophia.agnes wrote:
>Dear all,

why the following program is giving o/p as

sizeof(struct emp) = 0

#include<stdio .h>
#include<stdli b.h>

struct emp
{ };

This isn't valid C. C requires at least one member in every structure.
That said, ...
>i have seen this same code giving o/p as 1 in other compilers

...why would you expect any bytes to be required to store nothing?
Shouldn't you be asking why other compilers make sizeof(struct emp)
anything other than zero? Or do you already know why?
Padding.

--
Keith Thompson (The_Other_Keit h) <ks***@mib.or g>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Feb 10 '08 #5
On Sun, 10 Feb 2008 12:36:05 -0800, Keith Thompson wrote:
Harald van Dijk <tr*****@gmail. comwrites:
>On Sun, 10 Feb 2008 01:30:37 -0800, sophia.agnes wrote:
>>Dear all,

why the following program is giving o/p as

sizeof(struct emp) = 0

#include<stdi o.h>
#include<stdl ib.h>

struct emp
{ };

This isn't valid C. C requires at least one member in every structure.
That said, ...
>>i have seen this same code giving o/p as 1 in other compilers

...why would you expect any bytes to be required to store nothing?
Shouldn't you be asking why other compilers make sizeof(struct emp)
anything other than zero? Or do you already know why?

Padding.
In C, padding is not allowed at the immediate start of a structure.

More seriously, padding is used to ensure the structure's members are
properly aligned, and there's no problem with alignment of an empty
structure's members. There's also no problem with an array of zero-byte
objects. They all have the same address, so they are all identically
aligned.

Of course, the fact that they all have the same address is why a certain
other language places extra requirements on empty structures, but
extensions by any C compiler don't have to follow rules for other
languages.
Feb 10 '08 #6
On Sun, 10 Feb 2008 11:53:57 +0100, "Serve Laurijssen" <ni@hao.com>
wrote in comp.lang.c:
>
<so**********@g mail.comschreef in bericht
news:bb******** *************** ***********@s13 g2000prd.google groups.com...
Dear all,

why the following program is giving o/p as

sizeof(struct emp) = 0

#include<stdio. h>
#include<stdlib .h>

struct emp
{ };

int main(void)
{
printf("\n sizeof(struct emp) = %d", sizeof(struct emp));

puts ("");
return (EXIT_SUCCESS);
}

i have seen this same code giving o/p as 1 in other compilers

empty structs is illegal C
It is legal C++ though, there the size of an empty struct is always 1. Maybe
you were confused with that?
<off-topic>

The size of an empty struct in C++ is greater than 0. There is
neither requirement nor guarantee that it be exactly equal to 1.

</off-topic>

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Feb 11 '08 #7
On Feb 10, 2:30*pm, sophia.ag...@gm ail.com wrote:
Dear all,

why the following program is giving o/p as

*sizeof(struct emp) = *0

#include<stdio. h>
#include<stdlib .h>

struct emp
{ };
Not allowed in C .
There may be unnamed padding within a structure object, but not at its
beginning.
There may be unnamed padding at the end of a structure or union.
Refer -http://c0x.coding-guidelines.com/6.7.2.1.html
>
int main(void)
{
* printf("\n sizeof(struct emp) = *%d", sizeof(struct emp));

* puts ("");
* return (EXIT_SUCCESS);

}

i have seen this same code giving o/p as 1 in other compilers
Feb 17 '08 #8

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

Similar topics

19
9237
by: Martin Pohlack | last post by:
Hi, I have a funtion which shall compute the amount for a later malloc. In this function I need the sizes of some struct members without having an instance or pointer of the struct. As "sizeof(int)" is legal I assumed "sizeof(struct x.y)" to be legal too. But is is not: #include <dirent.h>
33
2728
by: Chris Fogelklou | last post by:
What is wrong with the above? Don't worry, I already know (learned my lesson last week.) It is for the benefit of our resident compiler guru who seems to think you need the cast. I thought it too, up until I started posting here! Thanks, Chris
7
7203
by: Zero | last post by:
If we have a structure like: struct something{ int *a; int b; }; We allocate mempry for a using malloc or calloc. The question is when we want to know the size of the structure, sizeof(struct something), it will not give us the correct number (i.e. the size of the structure
15
15170
by: ak | last post by:
struct xy{ int x; int y; } _xy; size_of_xy(struct xy * a) { int len = sizeof a; printf( "\sizeof xy: %i\n", len ); }
10
2434
by: Sean | last post by:
I have a struct that I wrote to test a protocol. The idea I had was to just declare the elements of the struct in the order in which they are sent and received as defined by the protocol. However, writing this struct to a file produces unexpected results. Here is a test struct I wrote: struct Tester { unsigned short first; unsigned int second;
10
3415
by: Mark A. Odell | last post by:
Is there a way to obtain the size of a struct element based only upon its offset within the struct? I seem unable to figure out a way to do this (short of comparing every element's offset with <offset>). What I would like to do is create an API something like this: #include <stddef.h> struct MemMap { unsigned char apple; // 8 bits on my platform
8
5920
by: Tony Houghton | last post by:
I'm writing a python program which reads input device events so it needs to know sizeof(struct timeval). By using the struct module I should be able to work out sizeof(long) from python, but I can't think of a way to measure non-fundamental types without including a little bit of C, which I'd rather avoid. How safe would I be assuming that sizeof(struct timeval) == 2 * sizeof(long)
0
9687
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10488
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10237
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9077
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7567
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5467
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3761
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.