What is the size of void?

C++CLanguage LawyerSizeof

C++ Problem Overview


What would this statement yield?

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

Edit: An extension to the question.

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.

C++ Solutions


Solution 1 - C++

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

void n;

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

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

Solution 2 - C++

If you are using GCC and you are not using compilation flags that remove compiler specific extensions, then sizeof(void) is 1. GCC has a nonstandard extension that does that.

In general, void is a incomplete type, and you cannot use sizeof for incomplete types.

Solution 3 - C++

Although void may stand in place for a type, it cannot actually hold a value. Therefore, it has no size in memory. Getting the size of a void isn’t defined.

A void pointer is simply a language construct meaning a pointer to untyped memory.

Solution 4 - C++

void has no size. In both C and C++, the expression sizeof (void) is invalid.

In C, quoting N1570 6.5.3.4 paragraph 1:

> The sizeof operator shall not be applied to an expression that > has function type or an incomplete type, to the parenthesized name of > such a type, or to an expression that designates a bit-field member.

(N1570 is a draft of the 2011 ISO C standard.)

void is an incomplete type. This paragraph is a constraint, meaning that any conforming C compiler must diagnose any violation of it. (The diagnostic message may be a non-fatal warning.)

The C++ 11 standard has very similar wording. Both editions were published after this question was asked, but the rules go back to the 1989 ANSI C standard and the earliest C++ standards. In fact, the rule that void is an incomplete type to which sizeof may not be applied goes back exactly as far as the introduction of void into the language.

gcc has an extension that treats sizeof (void) as 1. gcc is not a conforming C compiler by default, so in its default mode it doesn't warn about sizeof (void). Extensions like this are permitted even for fully conforming C compilers, but the diagnostic is still required.

Solution 5 - C++

Solution 6 - C++

sizeof() cannot be applied to incomplete types. And void is incomplete type that cannot be completed.

Solution 7 - C++

In C, sizeof(void) == 1 in GCC, but this appears to depend on your compiler.

In C++, I get:

In function 'int main()':
Line 2: error: invalid application of 'sizeof' to a void type
compilation terminated due to -Wfatal-errors.

Solution 8 - C++

To the 2nd part of the question: Note that sizeof(void *)!= sizeof(void). On a 32-bit arch, sizeof(void *) is 4 bytes, so p++, would be set accordingly.The amount by which a pointer is incremented is dependent on the data it is pointing to. So, it will be increased by 1 byte.

Solution 9 - C++

Most C++ compilers choosed to raise a compile error when trying to get sizeof(void).

When compiling C, gcc is not conforming and chose to define sizeof(void) as 1. It may look strange, but has a rationale. When you do pointer arithmetic adding or removing one unit means adding or removing the object pointed to size. Thus defining sizeof(void) as 1 helps defining void* as a pointer to byte (untyped memory address). Otherwise you would have surprising behaviors using pointer arithmetic like p+1 == p when p is void*. Such pointer arithmetic on void pointers is not allowed in c++ but works fine with when compiling C with gcc.

The standard recommended way would be to use char* for that kind of purpose (pointer to byte).

Another similar difference between C and C++ when using sizeof occurs when you defined an empty struct like:

struct Empty {
} empty;

Using gcc as my C compiler sizeof(empty) returns 0. Using g++ the same code will return 1.

I'm not sure what states both C and C++ standards on this point, but I believe defining the size of some empty structs/objects helps with reference management to avoid that two references to differing consecutive objects, the first one being empty, get the same address. If reference are implemented using hidden pointers as it is often done, ensuring different address will help comparing them.

But this is merely avoiding a surprising behavior (corner case comparison of references) by introduction another one (empty objects, even PODs consume at least 1 byte memory).

Solution 10 - C++

while sizeof(void) perhaps makes no sense in itself, it is important when you're doing any pointer math.

eg.

 void *p;
 while(...)
      p++;

If sizeof(void) is considered 1 then this will work. If sizeof(void) is considered 0 then you hit an infinite loop.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLakshmiView Question on Stackoverflow
Solution 1 - C++reko_tView Answer on Stackoverflow
Solution 2 - C++hrntView Answer on Stackoverflow
Solution 3 - C++Konrad RudolphView Answer on Stackoverflow
Solution 4 - C++Keith ThompsonView Answer on Stackoverflow
Solution 5 - C++pmgView Answer on Stackoverflow
Solution 6 - C++Aleksei PotovView Answer on Stackoverflow
Solution 7 - C++Greg HewgillView Answer on Stackoverflow
Solution 8 - C++user59634View Answer on Stackoverflow
Solution 9 - C++krissView Answer on Stackoverflow
Solution 10 - C++user906752View Answer on Stackoverflow