Morgan Stanley Interview Question Financial Software Developers

  • morgan-stanley-interview-questions
    0
    of 0 votes
    3
    Answers

    implement shared_ptr in C++

    template <class T>
    class shared_ptr
    {
    private:
    	T* _t;
    	int *count;
    	
    public:
    	shared_ptr(T* t){
    		_t = t;
    		count = new int(1);
    	}
    	
    	template <class D>
    	shared_ptr(shared_ptr<D>& d){
    		_t = d.Get();
    		count = d.GetCount();
    		*count = (*count)+1;
    	}
    
    	~shared_ptr() {
    		*count = (*count)-1;
    
    		if(*count == 0)
    			delete _t;
    	}
    
    	template <class D>
    	shared_ptr<T>& operator= (shared_ptr<D>& d) {
    		if( this->Get() != d.Get() )
    		{
    			*count = (*count)-1;
    			if( *count == 0)
    				delete _t;
    
    			_t = d.Get();
    			count = d.GetCount();
    			*count = *count + 1;
    		}
    
    		return *this;
    	}
    	
    	T* Get() { return _t; }
    
    	int* GetCount() { return count; }
    };

    - Itcecsa on May 11, 2012 in United States Report Duplicate | Flag
    Morgan Stanley Financial Software Developer C++

Country: United States
Interview Type: In-Person


Comment hidden because of low score. Click to expand.
2
of 2 vote

many errors in your code.
for instance. ctor should never return and its parameter must be const reference.

- jianbozhu on May 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

#include<iostream>

template<class T>
class sharedPtr
{
        T* ShrdPtr;
        mutable int* refCount;

public:
        sharedPtr( T* ptr ) { ShrdPtr = ptr; refCount = new int(); *refCount = 1; }
        ~sharedPtr( )
        {
                std::cout << "inside destructor refCount = " << *refCount << "\n";
                if( --(*refCount) == 0 )
                        delete ShrdPtr;
        }
        T& operator=( const sharedPtr& ptr );
        sharedPtr( const sharedPtr& ptr );
        T* operator->( ) { return ShrdPtr; }
        T& operator*() { return *ShrdPtr; }
};

template<class T>
T& sharedPtr<T>::operator=( const sharedPtr& ptr )
{
        std::cout << "inside assignment operator\n";
        this->ShrdPtr = ptr.ShrdPtr;
        (*(ptr.refCount))++;
        this->refCount = ptr.refCount;
        return *this;
}

template<class T>
sharedPtr<T>::sharedPtr( const sharedPtr& ptr )
{
        std::cout << "inside copy constructor\n";
         this->ShrdPtr = ptr.ShrdPtr;
        (*(ptr.refCount))++;
        this->refCount = ptr.refCount;
}


int main( int argc, char* argv[] )
{
        sharedPtr<int> p = new int;
        {
                sharedPtr<int> q = p;
        }
        return 0;
}

- sachin jain on July 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is my Implementation of shared ptr:
// shared_pointer.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <exception>

using namespace std;

template <class T> class sh_ptr
{
private:
T* ptr;
static int count;

public:
sh_ptr() {
ptr = new T;
count++;
cout << "count = " << count << endl;
}
~sh_ptr() {
// check count before deleting the pointer
cout << " count before = " << count << endl;
count--;
cout << " count after = " << count << endl;


if ( !count)
{

if (ptr) delete ptr;
ptr = NULL;
}
}

// implement sssignment and copy operation because shared pointer
// does copy and assignment. So it is named as shared pointer

// make sure that the copy constructor will invoke additional
// pointer to that heap memory and one additional counter
// no deep copy is required. For assignment operator
// we may require a deep copy as they already point to two different memory location.
// or we may not write a assignment operator
// We have to implement operator-> to exhibit the pointer behavior.
sh_ptr(sh_ptr& d){
ptr = d.ptr;
count++ ;

}

sh_ptr& operator=(sh_ptr& d){
// here is a memory leak for new, so first delet the new
// before assigning the pointer to another memory location
delete d.ptr;
ptr = d.ptr;
count++ ;
return *this;

}



void fun()
{
cout << " this is a fun" << endl;
}
sh_ptr<T>* operator->()
{
return (this);
}

};

template < class T> int sh_ptr<T>::count=0;


int main(int argc, char* argv[])
{

{
sh_ptr<int> obj;
obj->fun();
sh_ptr<int> obj2 = obj;
sh_ptr<int> obj7;
obj7 = obj;

}
cout << " new object" << endl;

sh_ptr<float> obj3;
sh_ptr<double> obj4;
return 0;
}

- Sandeep Dutta on December 29, 2012 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book walking you through every aspect of getting a job at a top tech company, while focuses on software engineering interviews.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More