什么是无效的大小? [英] What is the size of void?

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

问题描述

你会这种说法产生?

void * p = (void*) malloc(sizeof(void));



编辑:一个扩展的问题。

An extension to the question.

如果的sizeof(无效)的GCC编译器产生1,那么内存的1个字节分配和指针p指向该字节,并将p ++递增到0x2346?假设p是0x2345。我说的是P和不* P

If sizeof(void) yields 1 in GCC compiler, then 1 byte of memory is allocated and the pointer p points to that byte and would p++ be incremented to 0x2346? Suppose p was 0x2345. I am talking about p and not *p.

推荐答案

类型无效没有大小;这将是一个编译错误。出于同样的原因,你不能这样做:

The type void has no size; that would be a compilation error. For the same reason you can't do something like:

void n;

编辑。
令我惊讶的是,这样做的sizeof(无效)实际上确实的编译GNU C:

EDIT. To my surprise, doing sizeof(void) actually does compile in GNU C:

$ echo 'int main() { printf("%d", sizeof(void)); }' | gcc -xc -w - && ./a.out 
1

不过,在C ++中它没有:

However, in C++ it does not:

$ echo 'int main() { printf("%d", sizeof(void)); }' | gcc -xc++ -w - && ./a.out 
<stdin>: In function 'int main()':
<stdin>:1: error: invalid application of 'sizeof' to a void type
<stdin>:1: error: 'printf' was not declared in this scope

这篇关于什么是无效的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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