c大小0问题 [英] c size 0 question

查看:86
本文介绍了c大小0问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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返回0;

14}


输出为

0 1

0 1


有人可以解释为什么sizeof(b)和sizeof(struct a)为零,

如果没有内存分配我怎么能用这个变量

以及为什么gcc允许这个。


谢谢

~

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
~

推荐答案

sinbad写道:
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}


输出是

0 1

0 1


有人可以解释为什么sizeof(b)和sizeof(struct a)为零,

如果没有分配内存我怎么能使用这个变量

和为什么gcc允许这个。
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.



最可能的原因是你没有在(大部分)符合模式中使用gcc

(-ansi或-std = c99 plus -
再见,Jojo

Most propbably because you didn''t incoke gcc in a (mostly) conforming mode
(-ansi or -std=c99 plus -pedantic)

Bye, Jojo


sinbad< si ********* **@gmail.comwrites:
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返回0;

14}


输出是

0 1

0 1


有人可以解释为什么sizeof(b)和sizeof(struct a)为零,

如果有的话没有内存分配我如何使用这个变量

以及为什么gcc允许这样做。
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.



这是GCC扩展,不是标准C的一部分。你可以在

中阅读GCC手册 http://gcc.gnu .org / onlinedocs / gcc-4 .... ro-Length.html 。那个

页面也有C99灵活阵列成员的解释,这是类似和标准的



请注意你的程序有一个bug; %u printf()的格式说明符

希望你传递一个unsigned int,但sizeof(b)的类型是size_t,

可能是不同的。理想情况下,如果您的库支持,您应该使用C99标准的%z

格式说明符。否则,将sizeof(b)

转换为unsigned int,并希望它适合。


另外,`int main()''的正确性是争议。你应该

真的使用`int main(void)''这是明确无误的。

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.


Nate Eldredge写道:

....
Nate Eldredge wrote:
....

请注意您的程序有错误; %u printf()的格式说明符

希望你传递一个unsigned int,但sizeof(b)的类型是size_t,

可能是不同的。理想情况下,如果您的库支持,您应该使用C99标准的%z

格式说明符。否则,将sizeof(b)

转换为unsigned int,并希望它适合。
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.



使用unsigned long更安全;这更适合。

It''s safer to use unsigned long; that''s more likely to fit.


这篇关于c大小0问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆