473,671 Members | 2,311 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

size of a struct

Consider the following program fragment:

/*************** *********/
struct some_struct {

}a;

printf("%d", sizeof(a));
/*************** **********/

On GCC 4.1.1 the output is 0(zero).
On another compiler {sorry I don't know the compiler :-( } the output
is 1(one).

Is it compiler dependent?
If so how can a variable like "a" have a zero size ( I mean how can a
variable with 0(zero) byte size be stored in memory).

Sep 5 '06 #1
15 2853
chandanlinster wrote:
Consider the following program fragment:

/*************** *********/
struct some_struct {

}a;

printf("%d", sizeof(a));
/*************** **********/

On GCC 4.1.1 the output is 0(zero).
On another compiler {sorry I don't know the compiler :-( } the output
is 1(one).

Is it compiler dependent?
If so how can a variable like "a" have a zero size ( I mean how can a
variable with 0(zero) byte size be stored in memory).
First of all sizeof returns unsigned int so it should be
printf("%u", sizeof(a));
What you wrote evokes undefined behaviour.

Second you are declaring a structure with no members
which means you cannot assign anything to it. So a zero
size doesn't look unreasonable.

Third , I don't know if structures with no members are
allowed by the standard. The GNU compiler gives a
warning while the SUN compiler and lint give syntax
error. All this happens for the following code:

#include <stdio.h>

int main(void) {
struct some_struct { }a;

printf("%u", sizeof(a));
}

Sep 5 '06 #2
"chandanlinster " <ch************ @gmail.comwrite s:
Consider the following program fragment:

/*************** *********/
struct some_struct {

}a;

printf("%d", sizeof(a));
/*************** **********/
Standard C doesn't allow structures with no members.

If gcc allows this, it's a compiler-specific extension. If you're
curious about how it works, check the gcc documentation; if that
fails, try the gnu.gcc.help newsgroup.

--
Keith Thompson (The_Other_Keit h) 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.
Sep 5 '06 #3
Spiros Bousbouras wrote:
chandanlinster wrote:
>Consider the following program fragment:

/*************** *********/
struct some_struct {

}a;

printf("%d", sizeof(a));
/*************** **********/

On GCC 4.1.1 the output is 0(zero).
On another compiler {sorry I don't know the compiler :-( } the output
is 1(one).

Is it compiler dependent?
If so how can a variable like "a" have a zero size ( I mean how can a
variable with 0(zero) byte size be stored in memory).

First of all sizeof returns unsigned int so it should be
printf("%u", sizeof(a));
What you wrote evokes undefined behaviour.
It yields a size_t, which might or might not be
an unsigned int.
printf("%lu", (unsigned long)sizeof(a)) ; is usually recommended for
c89
Sep 5 '06 #4
"chandanlinster " <ch************ @gmail.comwrote :
Consider the following program fragment:

/*************** *********/
struct some_struct {

}a;

printf("%d", sizeof(a));
/*************** **********/

On GCC 4.1.1 the output is 0(zero).
On another compiler {sorry I don't know the compiler :-( } the output
is 1(one).
Neither is correct. An empty struct declaration is syntactically
invalid. Even one which only has unnamed members (which would at least
have a size) invokes undefined behaviour.
Is it compiler dependent?
It shouldn't be; it should be refused outright, rather than give a
random answer.
If so how can a variable like "a" have a zero size
It can't. That is presumably one of the reasons why it's disallowed.

Richard
Sep 5 '06 #5
Nils O. Selåsdal wrote:
Spiros Bousbouras wrote:
chandanlinster wrote:
Consider the following program fragment:

/*************** *********/
struct some_struct {

}a;

printf("%d", sizeof(a));
/*************** **********/

On GCC 4.1.1 the output is 0(zero).
On another compiler {sorry I don't know the compiler :-( } the output
is 1(one).

Is it compiler dependent?
If so how can a variable like "a" have a zero size ( I mean how can a
variable with 0(zero) byte size be stored in memory).
First of all sizeof returns unsigned int so it should be
printf("%u", sizeof(a));
What you wrote evokes undefined behaviour.
It yields a size_t, which might or might not be
an unsigned int.
printf("%lu", (unsigned long)sizeof(a)) ; is usually recommended for
c89
Quote from N1124 , section 6.5.3.4 , paragraph 4:

The value of the result is implementation-defined
and its type (an unsigned integer type) is size_t,
defined in <stddef.h(and other headers).

Sep 5 '06 #6
Spiros Bousbouras wrote:
Nils O. Selåsdal wrote:
>Spiros Bousbouras wrote:
>>chandanlinste r wrote:

Consider the following program fragment:

/*************** *********/
struct some_struct {

}a;

printf("%d ", sizeof(a));
/*************** **********/

On GCC 4.1.1 the output is 0(zero).
On another compiler {sorry I don't know the compiler :-( } the output
is 1(one).

Is it compiler dependent?
If so how can a variable like "a" have a zero size ( I mean how can a
variable with 0(zero) byte size be stored in memory).
First of all sizeof returns unsigned int so it should be
printf("%u" , sizeof(a));
What you wrote evokes undefined behaviour.
It yields a size_t, which might or might not be
an unsigned int.
printf("%lu" , (unsigned long)sizeof(a)) ; is usually recommended for
c89

Quote from N1124 , section 6.5.3.4 , paragraph 4:

The value of the result is implementation-defined
and its type (an unsigned integer type) is size_t,
defined in <stddef.h(and other headers).
Correct.
"an unsigned integer" might not be an unsigned int.
Thus we cast it to an unsigned long which is the largest
unsigned integer C90 guarantees, and print it accordingly.
Sep 5 '06 #7
Nils O. Selåsdal wrote:
Spiros Bousbouras wrote:
Nils O. Selåsdal wrote:
Spiros Bousbouras wrote:
chandanlinst er wrote:

Consider the following program fragment:

/*************** *********/
struct some_struct {

}a;

printf("%d" , sizeof(a));
/*************** **********/

On GCC 4.1.1 the output is 0(zero).
On another compiler {sorry I don't know the compiler :-( } the output
is 1(one).

Is it compiler dependent?
If so how can a variable like "a" have a zero size ( I mean how can a
variable with 0(zero) byte size be stored in memory).
First of all sizeof returns unsigned int so it should be
printf("%u", sizeof(a));
What you wrote evokes undefined behaviour.
It yields a size_t, which might or might not be
an unsigned int.
printf("%lu", (unsigned long)sizeof(a)) ; is usually recommended for
c89
Quote from N1124 , section 6.5.3.4 , paragraph 4:

The value of the result is implementation-defined
and its type (an unsigned integer type) is size_t,
defined in <stddef.h(and other headers).
Correct.
"an unsigned integer" might not be an unsigned int.
Thus we cast it to an unsigned long which is the largest
unsigned integer C90 guarantees, and print it accordingly.
So in C99 we would have to cast it to unsigned long long ?

Sep 5 '06 #8
Spiros Bousbouras wrote:
Nils O. Selåsdal wrote:
>Spiros Bousbouras wrote:
>>Nils O. Selåsdal wrote:

Spiros Bousbouras wrote:
chandanlins ter wrote:
>
>Consider the following program fragment:
>>
>/*************** *********/
>struct some_struct {
>>
>}a;
>>
>printf("%d ", sizeof(a));
>/*************** **********/
>>
>On GCC 4.1.1 the output is 0(zero).
>On another compiler {sorry I don't know the compiler :-( } the output
>is 1(one).
>>
>Is it compiler dependent?
>If so how can a variable like "a" have a zero size ( I mean how can a
>variable with 0(zero) byte size be stored in memory).
First of all sizeof returns unsigned int so it should be
printf("%u" , sizeof(a));
What you wrote evokes undefined behaviour.
It yields a size_t, which might or might not be
an unsigned int.
printf("%lu" , (unsigned long)sizeof(a)) ; is usually recommended for
c89
Quote from N1124 , section 6.5.3.4 , paragraph 4:

The value of the result is implementation-defined
and its type (an unsigned integer type) is size_t,
defined in <stddef.h(and other headers).
Correct.
"an unsigned integer" might not be an unsigned int.
Thus we cast it to an unsigned long which is the largest
unsigned integer C90 guarantees, and print it accordingly.

So in C99 we would have to cast it to unsigned long long ?
C99 provides the 'z' length modifier for the size_t type, so there we
might just do printf("%zu",si zeof a);
Sep 5 '06 #9
"Spiros Bousbouras" <sp****@gmail.c omwrites:
Nils O. Selåsdal wrote:
>Spiros Bousbouras wrote:
Nils O. Selåsdal wrote:

Spiros Bousbouras wrote:
[...]
>>First of all sizeof returns unsigned int so it should be
printf("%u" , sizeof(a));
What you wrote evokes undefined behaviour.
It yields a size_t, which might or might not be
an unsigned int.
printf("%lu" , (unsigned long)sizeof(a)) ; is usually recommended for
c89

Quote from N1124 , section 6.5.3.4 , paragraph 4:

The value of the result is implementation-defined
and its type (an unsigned integer type) is size_t,
defined in <stddef.h(and other headers).
Correct.
"an unsigned integer" might not be an unsigned int.
Thus we cast it to an unsigned long which is the largest
unsigned integer C90 guarantees, and print it accordingly.

So in C99 we would have to cast it to unsigned long long ?
If your runtime library is C99 compliant (which it might not be even
if the compiler is), you can use "%zu".

Or you can just cast to unsigned long and use "%lu". Even if size_t
is bigger than unsigned long, still work as long as the value you're
printing doesn't exceed ULONG_MAX.

--
Keith Thompson (The_Other_Keit h) 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.
Sep 5 '06 #10

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

Similar topics

6
2174
by: MSReddy | last post by:
Hi Group, I have a structure than has other structures in it. I added 3 members to a strucuture and my program started behaving very strangly. When I printed sizes of the strucutures the values were very strange and unexpected. Here is an example. Applogies for not being able to post the exact code. Struct B{ long b1;
7
2180
by: ANaiveProgrammer | last post by:
Hi all I have made the following two structs and size is not according to what is supposed to be, so please ponder over following and identify if im wrong... please also mention what would be the size of "ethernet_frame" struct and why ? typedef struct { //This struct defines Arp--Request.
19
8036
by: junky_fellow | last post by:
Can the size of pointer variables of different type may be different on a particular architecture. For eg. Can the sizeof (char *) be different from sizeof(int *) or sizeof (void *) ? What is the purpose of using a void pointer ? Instead of declaring a pointer variable "void *", can I declare it as "char *" and then later on typcast it to whatever type
3
27224
by: Dirk Reske | last post by:
Hello, I have the following struct: public struct WAVEFORMATEX { public UInt16 wFormatTag; //2 bytes public UInt16 nChannels; //2 bytes public UInt32 nSamplesPerSec; //4 bytes public UInt32 nAvgBytesPerSec; //4 bytes
1
8000
by: Jón Sveinsson | last post by:
Hello everyone I have been trying to read and write struct to binary files, I'm using to functions to convert the struct to bytes and bytes to struct, I always receive the following error C:\Documents and Settings\jon.JONHS-LAP\My Documents\Visual Studio
8
10160
by: redefined.horizons | last post by:
I would like to have an array declaration where the size of the array is dependent on a variable. Something like this: /* Store the desired size of the array in a variable named "array_size". */ unsigned short int array_size = 25; /*Declare an array named "numbers" using the variable initialized above. */ unsigned short int numbers;
11
2817
by: chinu | last post by:
mail hi all.. is it possible to find the size of a structure upto bit size.., where the structure contains only one element and that too with bit specifier..eg.. struct x { char a : 4; } y;
111
20011
by: Tonio Cartonio | last post by:
I have to read characters from stdin and save them in a string. The problem is that I don't know how much characters will be read. Francesco -- ------------------------------------- http://www.riscossione.info/
6
3845
by: marktxx | last post by:
Although the C90 standard only mentions the use of 'signed int' and 'unsigned int' for bit-fields (use 'int' at your own risk) and C99 adds _Bool. It seems that most compilers create the size of the bit-field object from the size used to specify the field. Could this be considered a defacto standard now (at least for 8 bit sized bit-fields)? Any recent compilers not allowing this?
15
2232
by: kris | last post by:
Hi I am writing a small program where I need to obtain the actual size of a structure. The programm is as follows struct abc { int j; char k; int i; }*a;
0
8473
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
8911
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
8819
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
8597
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
8667
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7428
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
6222
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
4402
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1806
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.