- Will the following program execute?
void main()
{
void *vptr = (void *) malloc(sizeof(void));
vptr++;
} - How about this one?
void main()
Will it execute or not?
{
char *cptr = 0?2000;
long *lptr = 0?2000;
cptr++;
lptr++;
printf(” %x %x”, cptr, lptr);
} - When the processor wakes up after power on, it goes to a particular memory location. What is that memory location called?
- What is the difference between Mutex and Binary semaphore?
- Write a program to set 2nd bit in a 32 bit register with memory location 0×2000?
Posted in: C++ |
May 8th, 2006 at 7:54 am
1. It will throw an error, as arithmetic operations cannot be performed on void pointers.
4. semaphore is used to synchronize processes. where as mutex is used to provide synchronization between threads running in the same process.
May 22nd, 2006 at 12:03 pm
1. it will not build as sizeof cannot be applied to void* ( error “Unknown size” )
4. the statement in the comment above is not correct, a mutex can be used for inter-process syncronization ( for syncronization inside the same process CriticalSection can be used )
From Multithreaded Applications in Win32, by Jim Beveridge ( page 93 ):
“…it turns out that a mutex is a degenerated form of a semaphore. If you create a semaphore and set the maximum count to one, you get mutual exclusion behavior. therefore a mutex is often refered to as a binary semaphore…In Win32, the ownership semantics of how the two operate are quite different, so they cannot be used interchangeably…”
May 23rd, 2006 at 7:59 am
Help…
Qs: Write a ‘grow’ function for a C++ vector class?
May 28th, 2006 at 7:56 am
for Q. 3
processor goes to MBR (master boot record) then i takes the address of boot loader form thr and execute this boot loader program
May 29th, 2006 at 5:06 am
for 4.
Mutex is efficent than semaphore. Also unlocking of mutex can be done by the thread which owns the lock. Not the case with semaphore.
June 12th, 2006 at 11:51 am
For Q1: How can it execute if it won’t even compile? It needs to be int main, not void main. Also, cannot increment a void *.
For Q2: As above, won’t compile because main must return int. Also, 0×2000 cannot be implicitly converted to a pointer (I assume you meant 0×2000 and not 0?2000.)
For Q3: Wossit got to do with C++?
For Q5: By “2nd bit” are you counting from most significant or least significant bit? Anyway, the answer is platform-dependent and compiler-dependent. Assuming you have a typedef “uint32″ that is exactly 32 bits long, then try something like *reinterpret_cast(0×2000) = 0×40000000 (for 2nd msb) or = 2 (for 2nd lsb)
June 12th, 2006 at 11:55 am
My previous comment has been garbled by this web site. It should read
*reinterpret_cast <> (0×2000)
and 0×2000 should contain the letter x and not some funny symbol.
June 19th, 2006 at 8:57 am
Q1)
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
July 27th, 2006 at 2:13 am
GIVE SOME INFORMATION ON FILE MAINTAING IN C++.
GIVE SOME SUGGETION TO MAKE THE SOLVE HARD PROGRAM BASED ON ‘INHERITENCE’ IN C++.
August 2nd, 2006 at 6:48 am
for Q 1
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.
October 8th, 2006 at 7:19 pm
Q2: Not Excute.
Compile with VC7 results following errors:
error C2440: ‘initializing’ : cannot convert from ‘int’ to ‘char *’
error C2440: ‘initializing’ : cannot convert from ‘int’ to ‘long *’
October 8th, 2006 at 7:30 pm
Further for Q2: Not Excute if it is C++, but Excute in C.
The printout:
2001 2004
compiled with some warnings:
warning C4047: ‘initializing’ : ‘char *’ differs in levels of indirection from ‘int’
warning C4047: ‘initializing’ : ‘long *’ differs in levels of indirection from ‘int’
October 24th, 2006 at 10:00 pm
Q2:
won’t compile since it’s an invalid asignment. Casting needs to be done. The format has to be:
int *a = (int *)2000;
November 1st, 2006 at 3:39 pm
Q1: 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*’.
November 2nd, 2006 at 12:11 pm
Question 1.
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
November 2nd, 2006 at 12:26 pm
Question 2:
In C++
[$]> g++ point.c
point.c: In function `int main()’:
point.c:4: error: invalid conversion from `int’ to `char*’
point.c:5: error: invalid conversion from `int’ to `long int*’
in C
———————————–
[$] etc > gcc point.c
point.c: In function `main’:
point.c:4: warning: initialization makes pointer from integer without a cast
point.c:5: warning: initialization makes pointer from integer without a cast
[$] etc > ./a.exe
2001 2004
November 15th, 2006 at 2:55 pm
Mani Chauhan is completely wrong for Q3.
The correct answer is it starts executing code from address 0xFFFF`FFF0. That is the locaiton of the jump vector.
March 4th, 2007 at 6:23 pm
So power hits the system, the CPU wakes up and tells the chipset to look at the top of the memory stack. It gets redirected to the top of the first megabyte and executes the code that it finds there. That code activates enough of the CPU to enable it to decompress the BIOS code. Once that happens, the BIOS code identifies the system, setting values in certain chipset registers, such as the number of CPUs present, the CPU stepping and brand (among others).