Bloomberg LP Interview Question
Software Engineer / Developers3)In C++ , when you create and object and do not destroy after its scope is over, the memory occupied by the object is not freed(As the allocation of memory in C++ is on stack). THe memory in RAM which is occupied is not available for other application . AS the program is run over and over again .more and more memory becomes captive.. thus leaving no memory to run any further application . This leads to a crash . It is equivalent to memory slowly and steadily being leaked out to this application a phenomenon analogous to water container leakage ..hence the name .. Memory Leak
1. sizeof int can be 4 or 2. Compiler dependent
3. I used lcint to perform static code checking for memory leak
4. When it's created by new(), it's in free store, not in heap.
memory leaks happen for running servers/processes. Whenever a process ends, its address space and allocated memory for it is freed. Say a server process is running, usually it forks other server process to interact with clients. in case every process is having a memory leak, then finally we need to kill the parent process, in order to free up the resources.
You can confirm this on windows using task manager and on linux using top command.
Size of a variable without using sizeof
#include <iostream>
class A
{
public:
int _x;
double _y;
long _z;
};
int main()
{
A a;
//std::cout << sizeof(a) << std::endl;
int diff_x = reinterpret_cast<long>(&a+1) - reinterpret_cast<int>(&a);
int diff_y = (char *)(&a+1) - (char *)(&a);
std::cout << diff_x << std::endl;
std::cout << diff_y << std::endl;
}
double s[2]; //if we want to find the size of double
- Vinod September 09, 2006int diff=((char*)&s[1])-((char*)&s[0]);