What happens when the following program is validated? void main() { void ptr = (void) malloc(sizeof(void)); ptr++ ; }
Reveal answer
Fill a bubble to check yourself
What happens when the following program is validated? void main() { void ptr = (void) malloc(sizeof(void)); ptr++ ; }
CompilationFails
Will Throw RunTime Error But Code will get Compiled
Throws Linker Error
All the above statements are invalid
The code uses sizeof(void) which is invalid in C/C++ - you cannot determine the size of void type. This causes a compilation error. Void pointers exist, but void itself is an incomplete type with no defined size. Additionally, incrementing a void pointer (ptr++) requires knowing the size of the pointed-to type, which is impossible for void* without a cast.
sizeof(void) is not valid standard C/C++ — void is an incomplete type with no defined size, so evaluating sizeof on it is a compile-time error (some compilers like GCC allow it only as a non-standard extension treating it as 1). Because the code fails at compilation, it never reaches the malloc call or the pointer arithmetic at runtime.