Cookie
BAN USER- 0of 0 votes
Answers1. Suppose a dll allocates memory using malloc or new and user of dll releases that memory.Is this a good practice.If not then why?
- Cookie
2.An application runs fine with a debug version of a dll but crashes with a release version of same dll.
What can be the possible causes of this.| Report Duplicate | Flag | PURGE
RSA Software Engineer / Developer Data Structures - 0of 0 votes
Answer1.Difference between GetMessage and PeekMessage?How are where would you prefer to use onre over the other.
- Cookie
2.Address of a function is passed as a parameter to CreateThread().What would happen?
3.How can you see all the thread running in an application in Visual Studio?
4.Interthread and Intrathread communication using sendmessage and reply message.
5.Discuss ThreadInfo structure.
6.Say there are multiple thread running in an application.How do you stop one thread and execute other and come back to original thread?| Report Duplicate | Flag | PURGE
RSA Software Engineer / Developer Threads - 0of 0 votes
Answers1. Difference between GetMessage and PeekMessage?How are where would you prefer to use onre over the other.
- Cookie
2.Can you use GetMessage to retrive message of thread running in different process?If yes then how?| Report Duplicate | Flag | PURGE
RSA Software Engineer / Developer Threads
Here is what Cormen has to say about the above problem but i quite well did not understand the solution..
The optimal strategy is the obvious greedy one. Starting will a full tank of gas,
Professor Midas should go to the farthest gas station he can get to within n miles
of Newark. Fill up there. Then go to the farthest gas station he can get to within n
miles of where he Þlled up, and Þll up there, and so on.
Looked at another way, at each gas station, Professor Midas should check whether
he can make it to the next gas station without stopping at this one. If he can, skip
this one. If he cannot, then Þll up. Professor Midas doesnt need to know how
much gas he has or how far the next station is to implement this approach, since at
each Þllup, he can determine which is the next station at which hell need to stop.
This problem has optimal substructure. Suppose there are m possible gas stations.
Consider an optimal solution with s stations and whose Þrst stop is at the kth gas
station. Then the rest of the optimal solution must be an optimal solution to the
subproblem of the remaining m − k stations. Otherwise, if there were a better
solution to the subproblem, i.e., one with fewer than s −1 stops, we could use it to
come up with a solution with fewer than s stops for the full problem, contradicting
our supposition of optimality.
This problem also has the greedy-choice property. Suppose there are k gas stations
beyond the start that are within n miles of the start. The greedy solution chooses
the kth station as its Þrst stop. No station beyond the kth works as a Þrst stop,
since Professor Midas runs out of gas Þrst. If a solution chooses a station j < k as
its Þrst stop, then Professor Midas could choose the kth station instead, having at
least as much gas when he leaves the kth station as if hed chosen the j th station.
Therefore, he would get at least as far without Þlling up again if he had chosen the
kth station.
If there are m gas stations on the map, Midas needs to inspect each one just once.
The running time is O(m).
@John
Myclass myclass[10]; will not work.This statement would give a compile time error as the class lacks a default constructor.
To construct the array on stack it would be done like below
int i[] = {1,2,3,4,5,6,7,8,9,10};
MyClass myClass[10] = {1,2,3,4,5,6,7,8,9,10};
1. By default class has private access specifier and structure has public in C++.
2. MyClass(const MyClass &)
{
}
3. To create a copy of existing object.
4. By default = does a shallow copy so if your class has pointers to some other data type except inbuilt one then a shallow copy would be done and it may result in dangling pointers.
5. const MyClass& operator = (const MyClass &)
{
}
6. So that chaining can be possible like below
x = y = z = 10;
8. The class destructor should never throw an exception. A destructor can be called in 2 ways.One when an object goes out of scope and 2nd when stack unwinding happens.So if a destructor is called due to stack unwinding and it throws an exception then C++ unexpected is called which calles terminate which terminates the whole application.Nothing is destroyed not even local objects.
9. Make class Singleton.
10. Use Mutex to warp the code where instantiation happens.
11. Operator new allocates raw memory only.It is similar to malloc() in C.
new first allocates raw memory using operator new and then calls constructor for the class.
12. bad_alloc exception is thrown.
A number would be a power of two if there is exactly one bit that is equal to 1 in its bit pattern (even 1 is a power of 2. 2 raised to the power zero is 1). Now we could use the method that we used to count the number of bits that are 1 in a given number and check if it is equal to 1. However that approach needs many steps and hence won’t suffice here.
In a number which is an exact power of 2 only 1 bit is set and all others are zero. Let the position of this 1 bit be MSB. Mathematics rules for binary numbers tells us that if we subtract 1 from this number then the number that we would get would have all its bit starting from the bit position MSB+1 set to 1. For example if the given number num is 8(00001000) then num-1 would be 7 (00000111). Now we notice that these two bit patterns dont have a 1 in the same bit position. Further observation suggests that if we bitwise and (&) both these numbers we would get zero.
bool isPowerOf2(int num)
{
return ((num>0) && (num & (num-1))==0);
}
So that the constructor do not change the object from which copy is being made.
- Cookie May 29, 2009