Categories:
.NET (357)
C (330)
C++ (183)
CSS (84)
DBA (2)
General (7)
HTML (4)
Java (574)
JavaScript (106)
JSP (66)
Oracle (114)
Perl (46)
Perl (1)
PHP (1)
PL/SQL (1)
RSS (51)
Software QA (13)
SQL Server (1)
Windows (1)
XHTML (173)
Other Resources:
Will the following program execute? C++
Will the following program execute? C++
✍: Guest
Will the following program execute?
void main()
{
void *vptr = (void *) malloc(sizeof(void));
vptr++;
}
Answer1
It will throw an error, as arithmetic operations cannot be performed on void pointers.
Answer2
It will not build as sizeof cannot be applied to void* ( error “Unknown size” )
Answer3
How can it execute if it won’t even compile? It needs to be int main, not void main. Also, cannot increment a void *.
Answer4
According to gcc compiler it won’t show any error, simply it executes. but in general we can’t do arthematic operation on void, and gives size of void as 1
Answer5
The program compiles in GNU C while giving a warning for “void main”.
The program runs without a crash. sizeof(void) is “1? hence when vptr++, the address is incremented by 1.
Answer6
Regarding arguments about GCC, be aware that this is a C++ question, not C. So gcc will compile and execute, g++ cannot. g++ complains that the return type cannot be void and the argument of sizeof() cannot be void. It also reports that ISO C++ forbids incrementing a pointer of type ‘void*’.
Answer7
in C++
voidp.c: In function `int main()’:
voidp.c:4: error: invalid application of `sizeof’ to a void type
voidp.c:4: error: `malloc’ undeclared (first use this function)
voidp.c:4: error: (Each undeclared identifier is reported only once for each function it appears in.)
voidp.c:6: error: ISO C++ forbids incrementing a pointer of type `void*’
But in c, it work without problems
2012-03-23, 2934👍, 0💬
Popular Posts:
What's difference between HashTable and ArrayList ? You can access array using INDEX value of array,...
What is the output of printf("%d")? 1. When we write printf("%d",x); this means compiler will print ...
How To Merge Values of Two Arrays into a Single Array? - PHP Script Tips - PHP Built-in Functions fo...
How Is the Width a Parent Element Related to Child Elements? - CSS Tutorials - Understanding Multipl...
What is the result of using Option Explicit? When writing your C program, you can include files in t...