Visit our sponsor Five Planet Juices  
Home
Microsoft Interview Process
Microsoft HR Questions
Technical Questions
Puzzles/Riddles
Resume Tips and Template
Discuss
Question to Interviewer
Interview Tips
Term Of Use
Site Feedback


Implement an allocator
I don't post the solution, maybe someone smarter and more diligent will).

 

Comments:

Simple one, a lil added crap
By thephoenics on Wednesday, February 27, 2008 (UMST)
#include <iostream> #include <cstdlib> #include <list> using namespace std; class Allocator { public: Allocator() { cout << "Allocator Initialised" << endl; }; ~Allocator() { cout << "Deallocator Initialised" << endl; DeallocateAll(); } void *Allocate(unsigned int size) { void *newItem = malloc(size); cout << "Allocating (" << size << " bytes) space at " << hex << newItem << endl; allocatedObjects.push_back(newItem); list<void *>::iterator i; int j=0; cout << endl << "Allotted Addresses :" << endl; for (i=allocatedObjects.begin(); i!=allocatedObjects.end(); i++, j++) { cout << j << " : " << hex << *i << endl; } cout << "--- end ---" << endl << endl; return newItem; } char *Allocate(unsigned int size, const char* str) { char *temp = (char *) Allocate(size); strcpy(temp, str); return temp; } int Deallocate(void *ptr) { cout << "Deallocating space for " << (char *) ptr << " at "<< hex << ptr <<endl; free(ptr); allocatedObjects.remove(ptr); return 0; } int DeallocateAll() { list<void *>::iterator i; for (i = allocatedObjects.begin(); i != allocatedObjects.end(); i++) { if (*i != NULL) { cout << "Deallocating space for " << (char *) *i << " at "<< hex << *i <<endl; free(*i); *i = NULL; } } cout << "Deallocation Done" << endl; return 0; } private: list<void *> allocatedObjects; }; int main() { Allocator *newitem = new Allocator; char *a = (char *) newitem->Allocate(20, "hello"); char *b = (char *) newitem->Allocate(20, "hello"); char *c = (char *) newitem->Allocate(20); char *d = (char *) newitem->Allocate(20, "hello"); newitem->Deallocate(d); strcpy(c,"Hello"); cout << c << endl; delete newitem; system("PAUSE"); }

Reply to this Comment
 

@above - need some clarification
By Kusumita on Sunday, October 05, 2008 (UMST)

Hi ,

That was nice reply. But I need some more clarifications .

The output of your program is coming as follows:

 Allocator Initialised
Allocating (20 bytes) space at 00339D60

Allotted Addresses :
0 : 00339D60
--- end ---

Allocating (14 bytes) space at 00339E90

Allotted Addresses :
0 : 00339D60
1 : 00339E90
--- end ---

Allocating (14 bytes) space at 00336668

Allotted Addresses :
0 : 00339D60
1 : 00339E90
2 : 00336668
--- end ---

Allocating (14 bytes) space at 00336700

Allotted Addresses :
0 : 00339D60
1 : 00339E90
2 : 00336668
3 : 00336700
--- end ---

Deallocating space for hello at 00336700
Hello
Deallocator Initialised
Deallocating space for hello at 00339D60
Deallocating space for hello at 00339E90
Deallocating space for Hello at 00336668
Deallocation Done

 

But I am somehow not getting the point that , when the following line  is executed  :

char *b = (char *) newitem->Allocate(20, "hello");

 

How come   we are allocating  memory to allocatedObjects[1]?

where in the code the pointer is incremented by 1 from allocatedObjects[0]

to allocatedObjects[1]?

The same question holds for

char *c = (char *) newitem->Allocate(20);  &  char *d = (char *) newitem->Allocate(20, "hello"); where we are al;locating  memory  to allocatedObjects[2] & allocatedObjects[3] respectively ?

 

Also for allocatedObjects[1] , allocatedObjects[2]  & allocatedObjects[3]

 why the memory allocated is showiung 14 bytes instead od 20 bytes ?

Thanks in advance


 

 

Reply to this Comment
 

Add Your Comment

New Articles
  • Phone screen from MICROSOFT Denmark for SDET
    Technical Phone screen after Escreen

  • Please post some non-tech qs for User Experience and Business Analyst job

    I am a CS Grad applied in non-tech position.Please help!


  • Combinations in a character array.
    Write a program that takes input a char array and outputs all the combinations of the characters in the character array.
    Example: consier char array {'a','b','c'}
    the output shouold be abc,cab,bac,acb,cba,bca that is all the combinations of characters 'a','b','c'.


  •  

    Most Popular Articles
  • Crazy Guy On The Airplane
    A line of 100 airline passengers is waiting to board a plane. they each hold a ticket to one of the 100 seats on that flight.

  • What are your greatest weaknesses
    Beware - this is an eliminator question, designed to shorten the candidate list. Any admission of a weakness or fault will earn you an “A” for honesty, but an “F” for the interview.

  • Question: Tell me about yourself.
    Many candidates, unprepared for the question, skewer themselves by rambling, recapping their life story, delving into ancient work history or personal matters.

  •  

    New Posts

  • male enhancment buy viagra tramadol online
    Posted by SurojattSit on 2008年9月30日 (UMST)

  • penis enlargement viagra cheap tramadol
    Posted by BobeCrobChext on 2008年9月30日 (UMST)

  • penis viagra online Tramadol
    Posted by BobeCrobChext on 2008年9月30日 (UMST)



  •