I was looking over the sizeof operator in C++. I came across an unexpected semantic error. I had read that void data types has no memory size. But, here in case of my program, sizeof operator returns 1 for a void data type. How is this possible?
#include<iostream>
int main()
{
std::cout<<"Void: "<<sizeof(void)<<" bytes\n";
return 0;
}
I am using CodeBlocks for writing code and my OS is Windows 10 x64.
This is a GCC extension:
In GNU C, addition and subtraction operations are supported on pointers to
void
and on pointers to functions. This is done by treating the size of avoid
or of a function as 1.A consequence of this is that
sizeof
is also allowed onvoid
and on function types, and returns 1.The option
-Wpointer-arith
requests a warning if these extensions are used.