473,404 Members | 2,137 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,404 software developers and data experts.

c size 0 question

chip_init.c

1 #include <stdio.h>
2 struct a {
3 char buf[0];
4 }b;
5 struct c {
6 char buf[1];
7 }d;
8
9 int main ()
10 {
11 printf("%u %u\n", sizeof(b), sizeof(d));
12 printf("%u %u", sizeof(struct a), sizeof(struct c));
13 return 0;
14 }

the output is
0 1
0 1

can someone explain why sizeof(b) and sizeof(struct a) is zero,
if there is no memory allocated how can i use this variable
and why gcc allows this.

thanks
~
Nov 21 '08 #1
11 1716
sinbad wrote:
chip_init.c

1 #include <stdio.h>
2 struct a {
3 char buf[0];
4 }b;
5 struct c {
6 char buf[1];
7 }d;
8
9 int main ()
10 {
11 printf("%u %u\n", sizeof(b), sizeof(d));
12 printf("%u %u", sizeof(struct a), sizeof(struct c));
13 return 0;
14 }

the output is
0 1
0 1

can someone explain why sizeof(b) and sizeof(struct a) is zero,
if there is no memory allocated how can i use this variable
and why gcc allows this.
Most propbably because you didn't incoke gcc in a (mostly) conforming mode
(-ansi or -std=c99 plus -pedantic)

Bye, Jojo

Nov 21 '08 #2
sinbad <si***********@gmail.comwrites:
chip_init.c

1 #include <stdio.h>
2 struct a {
3 char buf[0];
4 }b;
5 struct c {
6 char buf[1];
7 }d;
8
9 int main ()
10 {
11 printf("%u %u\n", sizeof(b), sizeof(d));
12 printf("%u %u", sizeof(struct a), sizeof(struct c));
13 return 0;
14 }

the output is
0 1
0 1

can someone explain why sizeof(b) and sizeof(struct a) is zero,
if there is no memory allocated how can i use this variable
and why gcc allows this.
It's a GCC extension, not part of standard C. You can read about it in
the GCC manual at
http://gcc.gnu.org/onlinedocs/gcc-4....ro-Length.html . That
page also has an explanation of C99 flexible array members, which are
similar and standard.

Note that your program has a bug; the "%u" format specifier for printf()
expects you to pass an unsigned int, but sizeof(b) has type size_t,
which might be different. You should ideally use the C99-standard "%z"
format specifier if your library supports it. Otherwise, cast sizeof(b)
to unsigned int, and hope that it fits.

Also, the correctness of `int main()' is controversial. You should
really use `int main(void)' which is unambiguously correct.
Nov 21 '08 #3
Nate Eldredge wrote:
....
Note that your program has a bug; the "%u" format specifier for printf()
expects you to pass an unsigned int, but sizeof(b) has type size_t,
which might be different. You should ideally use the C99-standard "%z"
format specifier if your library supports it. Otherwise, cast sizeof(b)
to unsigned int, and hope that it fits.
It's safer to use unsigned long; that's more likely to fit.
Nov 21 '08 #4
sinbad wrote:
chip_init.c

1 #include <stdio.h>
2 struct a {
3 char buf[0];
It's a constraint violation to declare an array with a length of 0. gcc
allows this as an extension, but it's entirely up to gcc to determine
how it can be used.
4 }b;
5 struct c {
6 char buf[1];
7 }d;
8
9 int main ()
10 {
11 printf("%u %u\n", sizeof(b), sizeof(d));
The "%u" specifier expects an unsigned int argument; the result of the
sizeof operator is size_t, which is unsigned, but might be an unsigned
type larger than unsigned int (it could also be smaller, though that's a
lot less likely, and not a problem in this context).

In C90, you should write
printf("%lu %lu\n",
(unsigned long)sizeof b, (unsigned long)sizeof d);

In C99, you should write
printf("%z %z\n", sizeof b, sizeof d);
Nov 21 '08 #5
On 21 Nov, 08:13, Nate Eldredge <n...@vulcan.lanwrote:

<snip>
Also, the correctness of `int main()' is controversial. *You should
really use `int main(void)' which is unambiguously correct
I'm not sure if "controversial" is the right word.

int main()

in a function definition is correct and unambiguous.

int main (void)

is more of a style thing

--
Nick Keighley

Nov 21 '08 #6
Nate Eldredge <na**@vulcan.lanwrites:
[...]
Note that your program has a bug; the "%u" format specifier for printf()
expects you to pass an unsigned int, but sizeof(b) has type size_t,
which might be different. You should ideally use the C99-standard "%z"
format specifier if your library supports it.
You mean "%zu".
Otherwise, cast sizeof(b)
to unsigned int, and hope that it fits.
Even better, cast to unsigned long and use "%lu" -- though unsigned
int is fine if you're sure the size doesn't exceed 32767.
[...]

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Nov 21 '08 #7
Nick Keighley <ni******************@hotmail.comwrites:
On 21 Nov, 08:13, Nate Eldredge <n...@vulcan.lanwrote:
<snip>
>Also, the correctness of `int main()' is controversial. *You should
really use `int main(void)' which is unambiguously correct

I'm not sure if "controversial" is the right word.

int main()

in a function definition is correct and unambiguous.

int main (void)

is more of a style thing
It is controversial; we've discussed it here at considerable length a
couple of times. In case you missed it, the argument is that
int main() { /* ... */ }
is not "equivalent" to
int main(void) { /* ... */ }
because the latter causes a call main(42) to be a constraint
violation.

See, for example,
http://groups.google.com/group/comp....84baf9899c90a6
http://preview.tinyurl.com/6h76rs
starting at the 10th article.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Nov 21 '08 #8
James Kuyper <ja*********@verizon.netwrites:
[...]
The "%u" specifier expects an unsigned int argument; the result of the
sizeof operator is size_t, which is unsigned, but might be an unsigned
type larger than unsigned int (it could also be smaller, though that's
a lot less likely, and not a problem in this context).

In C90, you should write
printf("%lu %lu\n",
(unsigned long)sizeof b, (unsigned long)sizeof d);

In C99, you should write
printf("%z %z\n", sizeof b, sizeof d);
printf("%zu %zu\n", sizeof b, sizeof d);

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Nov 21 '08 #9
On Nov 21, 11:01*am, Keith Thompson <ks***@mib.orgwrote:
Nick Keighley <ni******************@hotmail.comwrites:
On 21 Nov, 08:13, Nate Eldredge <n...@vulcan.lanwrote:
<snip>
Also, the correctness of `int main()' is controversial. *You should
really use `int main(void)' which is unambiguously correct
I'm not sure if "controversial" is the right word.
* *int main()
in a function definition is correct and unambiguous.
* *int main (void)
is more of a style thing

It is controversial; we've discussed it here at considerable length a
couple of times. *In case you missed it, the argument is that
* * int main() { /* ... */ }
is not "equivalent" to
* * int main(void) { /* ... */ }
because the latter causes a call main(42) to be a constraint
violation.
Why? In the link to the first thread you posted below, Richard
Heathfield posted a quote from C89 about this. In 6.7.5.3 from C99,
the quote is:

"14 An identifier list declares only the identifiers of the parameters
of the function. An empty list in a function declarator that is part
of a definition of that function specifies that the
function has no parameters. The empty list in a function declarator
that is not part of a
definition of that function specifies that no information about the
number or types of the
parameters is supplied.126)"

So if the function declarator is part of the definition of the
function (as was the case for main() in the OP's code), then calling
main(42) *is* a constraint violation, isn't it?

Sebastian

Nov 21 '08 #10
James Kuyper wrote:
sinbad wrote:
>chip_init.c

1 #include <stdio.h>
2 struct a {
3 char buf[0];

It's a constraint violation to declare an array with a length of
0. gcc allows this as an extension, but it's entirely up to gcc
to determine how it can be used.
You can define the last element of a struct as a zero sized array,
in C99 only. This allows special games to set the buf (above) to
an appropriate size on malloc of an instance of that struct.

Other possibilities are off-topic extensions.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
Nov 22 '08 #11
CBFalconer <cb********@yahoo.comwrites:
James Kuyper wrote:
>sinbad wrote:
>>chip_init.c

1 #include <stdio.h>
2 struct a {
3 char buf[0];

It's a constraint violation to declare an array with a length of
0. gcc allows this as an extension, but it's entirely up to gcc
to determine how it can be used.

You can define the last element of a struct as a zero sized array,
in C99 only. This allows special games to set the buf (above) to
an appropriate size on malloc of an instance of that struct.
[...]

No, C99's flexible array member syntax doesn't use "[0]"; it uses
"[]".

So this:

struct a {
size_t len;
char buf[1];
};

is likely to be a C90-style struct hack (whose legality has been
questioned); and this:

struct b {
size_t len;
char buf[0];
};

is a constraint violation in either C90 or C99, and is likely to be
dependent on a gcc extension; and this:

struct c {
size_t len;
char buf[];
};

is a struct containing a C99 flexible array member, intended to
replace the struct hack.

See also question 2.6 in the comp.lang.c FAQ, <http://c-faq.com/>
(which unfortunately doesn't mention the phrase "struct hack" except
in its URL).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Nov 22 '08 #12

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

Similar topics

25
by: Matthias | last post by:
Hi, I am just reading that book by Scott Meyers. In Item 4 Meyers suggests to always use empty() instead of size() when probing for emptyness of STL containers. His reasoning is that size()...
9
by: Dr John Stockton | last post by:
Assuming default set-ups and considering all reasonable browsers, whatever that may mean, what should an author expect that his readers in general will see (with visual browsers) for a page with...
1
by: Erik Hendrix | last post by:
Hi, I have a question here regarding setting the prefetch size. So far we took the rule that for OLTP, prefetchsize = extent size and for DSS prefetchsize = extent size * number. However,...
2
by: Anurag | last post by:
This simple one beats me all ends up(sincerely). I have been doing DB2 UDB for some time now, reading a lot of good discussions in this forum, writing some answers, asking a lot more but this...
27
by: Deephay | last post by:
Greetings all, I have a program that used the realloc() function to change the allocated size of a buffer, the program works with some arguments, but with some other arguments, it will show me...
12
by: Laphan | last post by:
Hi All Strange question this, but could somebody tell me what the old size=1, size=2 .... to size=7 font parameters are in pixels and points. I thought it was: size 1 = 7 pt size 2 = 14...
45
by: charles.lobo | last post by:
Hi, I have recently begun using templates in C++ and have found it to be quite useful. However, hearing stories of code bloat and assorted problems I decided to write a couple of small programs...
71
by: Mark | last post by:
Sorry if the question is a bit basic. I generally express my font sizes in pixels, originally to handle the Macintosh/Windows font size differences (though I believe that they both now treat...
9
by: Jensen Somers | last post by:
Hi, Is it possible to change the stack size of a DLL from within the source code? When using threads you can specify the size of the stack you want the thread to use, but since my application...
4
by: Zytan | last post by:
This may be the dumbest question of all time, but... When I set the packet size, does it mean ALL packets are that size, no matter what? Let's say the packet size is 8KB, and I send a 5 byte...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
0
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,...
0
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...

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.